How to save file data into 2-d array
Nov 17, 2012 at 10:43pm UTC
I can save my file data into array now, but don't know how to put into 2-d array.
This is my program:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <sstream>
#include <cmath>
using namespace std;
int main(){
const int DATA_SIZE = 15;
const int NUMROWS = 5;
const int NUMCOLS = 3;
int i, j;
double data[DATA_SIZE];
ifstream input_stream(data.txt");
if ( !input_stream.fail())
{
for (int n = 0; n < DATA_SIZE; n++)
{
input_stream >> data[n];
cout << data[n] << endl;
}
double val[NUMROWS][NUMCOLS] = data[DATA_SIZE];
for (i = 0; i < NUMROWS; i++)
{ cout << endl;
for ( j = 0; j < NUMCOLS; j++)
{
cout << setprecision(4) << val [i][j] << " " ;
}
}
cout << endl;
input_stream.close();
}
else
cout << " Unable to open file" << endl;
system(" pause");
return 0;
}
Can anyone help me with it?
Last edited on Nov 17, 2012 at 11:55pm UTC
Nov 17, 2012 at 11:29pm UTC
When you specify a position in an array you are essentially accessing a pointer to a memory location. In order to write to each member you would need to iterate through each location ofthe array and assign a value.
Topic archived. No new replies allowed.