Write your question here.
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.
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.
Well, not really, as your example would print data1's address, not contents, but close. You should rather call it this way:
1 2 3 4 5 6
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
1 2
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:
1 2 3 4 5
// 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:
1 2 3 4 5
// 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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;
}
}
}
}
}
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.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
usingnamespace 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
}
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
1 2
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.