3D array help please
Mar 11, 2015 at 10:50am UTC
I have a 3D array X[2][4][5] but what i want to do is read from a file an integer n_el that must be between 0<n_el<=40 and then read n_el integers into the array X.
Finally cout the array X
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream IN("input.txt" );
ofstream OUT("output" );
if ( IN && OUT)
{
int n_el;
IN>>n_el;
cout<<n_el;
int X[2][4][5];
for ( int i=0; i<2; i++)
{
for (int j=0; j<4; j++)
{
for (int z=0; z<5; z++)
{IN>> X[i][j][z];
cout<<X[i][j][z]<<endl;
}
}
}
}
else
cout<<"error" ;
IN.close(); OUT.close();
}
Basically i don't know how to read n_el integers into the array.
How do i code it?
Thanks
Mar 11, 2015 at 11:06am UTC
Introduce another variable like int idx_el = 0;
Increase the index in the inner loop and break all loops if idx_el
reached n_el
Mar 11, 2015 at 12:01pm UTC
thanks!
Last edited on Mar 11, 2015 at 12:08pm UTC
Topic archived. No new replies allowed.