### Array of arrays, tree of data

I have two arrays, data1 and data2. Each has 2x256 data values (basically they are tables of amplitude vs frequency)
I would like to create an array called Dipole, with two elements, which the entries for that array would be data1 and data2, like Dipole[2] = {data1, data2}.
My goal is to construct a sort of "tree" of data. I want to have a big array ( say System) of 10 entries, each entries containing of Dipole array, each Dipole array has 2 data arrays and the data arrays have 2x256 numbers. So I will call System[10] - Dipole[2] - data1 to analyze the table of data1 from the second element of the 10th dipole of the system.
Is there a way to do this? I can't find a way to make arrays of arrays. Not sure if it's even possible.

Why not use some kind of struct:
 ``123456`` ``````struct Dipole{ int amplitude; int frequency; }; Dipole myArray[256];``````

EDIT:
Well, not really thought it through
 ``123456`` ``````struct Dipole{ int amplitude [256]; int frequency [256]; }; Dipole system[10];``````

might be better.
Last edited on
Wow that helps a lot. Thanks
But it seems to me that in should be like this (I am not sure):

 ``123456`` `````` struct System{ double data1 [2][256]; double data2 [2][256]; }; System Dipole[10]; ``````

and then I could say for example:
cout << Dipole[0].data1
to print first set of data of the first dipole of the system. Is this right?

Also, I thought that I could use the answer and apply it to the overall problem, but I am not sure how to do it since I am not familiar with structures (I have used classes though)

The REAL problem is : I have 100 systems, each system has 10 dipoles, each dipole has 2 tables of amplitude vs. frequency.
How can I make something like
cout << System[0].Dipole[0].data1
to print the first dataset of the first dipole of the first system?
Thanks a lot.
OP wrote:
cout << Dipole[0].data1
to print first set of data of the first dipole of the system. Is this right?

Since it's an array(data1) in fact multi-dimensional array, you need to access the elements, which is easier using a loop.

eg:
 ``123`` ``````for(int i=0;i<2;i++l for(int j=0;j<256;j++) cout<

Aceix.
Last edited on
Well, not really, as your example would print data1's address, not contents, but close. You should rather call it this way:
 ``123456`` ``````for (int y = 0; y < 2; y ++){ for (int x = 0; x < 256; x++){ cout << System[s].Dipole[d].data1[y][x] << ", "; // Only this would print entire data inside data1 } }``````

And I would stick no my way of naming :D
 ``12`` ``````Dipole system[10]; // declare an array of ten Dipoles and call it system System Dipole[10]; // declare an array of ten Systems and call it a Dipole ``````

EDIT:
Just realized my loop not do not quite cover your implementation.
So - we need a type called system, that can hold ten dipoles? Easy:
 ``12345`` ``````// System is a datatype that always hold ten Dipoles, so its just // a wrapped one dimensional array struct System { Dipole dipole[10]; };``````

But what is Dipole? Put this lines before definition of `System` in the file:
 ``12345`` ``````// Dipole is a datatype, that always hold two 2D arrays of fixed sizes: struct Dipole { double data1 [2][256]; double data2 [2][256]; };``````

Then in main you create an array of 100 systems:
 ``12345678910111213141516`` ``````int main() { System systems[100]; // Create 100 of systems // And access the data inside: for( int s = 0; s < 100; s++) { // For each of 100 Systems for(int d = 0; d < 10; d++) { // For each of 10 Dipoles inside a system s for(int ampl = 0; ampl < 2; ampl++) { // For each row in amplitude vs frequency table for(int freq = 0; freq < 100; freq++) { // For each column in that row of the table // Let's say you want enter each value by hand to data1 and data2: double d1, d2; systems[s].dipole[d].data1[ampl][freq] = cin >> d1; systems[s].dipole[d].data2[ampl][freg] = cin >> d2; } } } } }``````
Last edited on
Amazing! This is gold. Thanks a lot!
Now I am having another problem. The command prompt is crashing. This is my code
Is this because it is too much for the computer to handle? It is a new computer.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include #include #include #include using namespace std; struct Dipole { double data1 [2][256]; double data2 [2][256]; }; struct System { Dipole dipole[10]; // type called system, that can hold ten dipoles }; int main() { double data0[256][2]= {}; int i =0; ifstream infile("ProposalPowers.dat"); // open file if (!infile) { cerr << "File could not be opened!" << endl; } // display error if file not found while (!infile.eof()) { infile >> data0[i][0] >> data0[i][1]; i++; } // to read a data file for (int l=0;l<256;l++) { data0[l][1] = 10*log(data0[l][1]); //convert to decibels } System systems[100]; // Create 100 of systems // access the data inside: for( int s = 0; s < 100; s++) { // For each of 100 Systems for(int d = 0; d < 10; d++) { // For each of 10 Dipoles inside a system s for(int z = 0; z < 256; z++) { /// This is easier for me to see... please correct me if Im wrong systems[s].dipole[d].data1[z][0] = data0[z][0]; systems[s].dipole[d].data1[z][1] = data0[z][1]; // filling out all entries systems[s].dipole[d].data2[z][0] = data0[z][0]; // using the same file systems[s].dipole[d].data2[z][1] = data0[z][1]; // just to check } } } cout << fixed << setprecision(20); // display data cout << systems[0].dipole[0].data2[0][0]; // just to check if it worked }``````
You are trying to fit
` double data0[256][2]= {};`
inside
` double data1 [2][256];`
 ``12`` ``````double data1 [256][2]; double data2 [256][2];``````

but the DOS windows is still crashing :/

--EDIT

OK I got it. I think after all it was too much for the computer to handle, I guess a lot of memory. I divided the 100 systems into ten groups of 10 systems each, like
 ``12`` ``````System group1[10]; System group2[10];``````

etc. I guess I can make a function to fill out the entries so I dont have to repeat the code.
Thanks a lot.
Last edited on
Topic archived. No new replies allowed.