ここで、GF : 祖父 f1,f2,f3 : 父 c1,c2,c3 : 子供
Q1. このデータ構造を配列に格納する方法 Q2. 上記の配列を使用して、すべての子要素にアクセスする方法のみ?
ここで、GF : 祖父 f1,f2,f3 : 父 c1,c2,c3 : 子供
Q1. このデータ構造を配列に格納する方法 Q2. 上記の配列を使用して、すべての子要素にアクセスする方法のみ?
これでいけると思います
#include <iostream>
using namespace std;
struct Father
{
char* Child1;
char* Child2;
char* Child3;
};
struct Grandfather
{
Father Father1 ;
Father Father2;
Father Father3;
};
const int MAXARRAYSIZE = 20;
Grandfather Grandfathers[MAXARRAYSIZE];
int main()
{
Grandfathers[0].Father1.Child1 = "The smart";
Grandfathers[0].Father1.Child2 = "the brave";
Grandfathers[0].Father1.Child3 = "the free";
Grandfathers[0].Father2.Child1 = "The good";
Grandfathers[0].Father2.Child2 = "the bad";
Grandfathers[0].Father2.Child3 = "the ugly";
Grandfathers[0].Father3.Child1 = "The boy";
Grandfathers[0].Father3.Child2 = "the girl";
Grandfathers[0].Father3.Child3 = "the other boy";
//To print the children
int CurrentArraySize = 1; //i can be changed with user input to accomodate more users for example, if there are 3 elements to the Grandfathers array i.e 3 different trees change CurrentArraySize to 3
for(int i = 0; i < CurrentArraySize; i++)
{
cout<<"Children under Father1 are : "<<Grandfathers[i].Father1.Child1<<", "<<Grandfathers[i].Father1.Child2<<" and "<<Grandfathers[i].Father1.Child3<<endl;
cout<<"Children under Father2 are : "<<Grandfathers[i].Father2.Child1<<", "<<Grandfathers[i].Father2.Child2<<" and "<<Grandfathers[i].Father2.Child3<<endl;
cout<<"Children under Father3 are : "<<Grandfathers[i].Father3.Child1<<", "<<Grandfathers[i].Father3.Child2<<" and "<<Grandfathers[i].Father3.Child3<<endl;
}
system("pause");
}