This is a homework question, but the answer I seek is not for the entire homework; this is only the first part of it. The rest is simple calculations that I need to do, alone with switch/case scenarios. I am only asking this part of the question as I am admittedly bad at arrays.
For my class assignment, we are being requested to take data from a .txt file and produce some data into 2D arrays with it. The only question (issue) I have is that I am trying to read this file into a 2D array in the first place.
The table is supposed to be 34 rows long, 14 columns wide; the first column is dedicated to years. I'll feature an example of such, but I wish to try to get this myself, so I will refrain from adding the whole thing:
1982 1192004.204 146797.49 305259.749 0 282773.248 0 312374.013 195.94
124.979 4842.865 0 0 2244372.487
1983 1259424.279 144498.593 274098.458 0 293677.119 0 335290.855 215.867
162.745 6075.101 0 2.668 2313445.686
1984 1341680.752 119807.913 297393.596 0 327633.549 0 324311.365 461.411
424.54 7740.504 5.248 6.49 2419465.367
This is the code I am putting up as I think it should be.
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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
ifstream infile;
int main()
{
//open data files
infile.open("Energy.dat");
double energy[34][14];
//Assigning data to arrays row by row
for(int i = 0; i < 34; i++)
{
for(int j = 1; j <= 14; j++)
{
infile >> energy[i][j];
cout << energy[i][j] << "\t";
}
cout << "\n";
}
return 0;
}
|
My result comes out with mainly arbitrary numbers, even the first one. On top of that, the code output doesn't come back as a table of 34 x 14. Any help would be greatly appreciated!