I have a code that is supposed to take a file with n lines, each with p doubles, and add each double to a 2d array. For some reason when I try to print the array it shows every other row being zeroed out. What am I doing wrong?
The text file would look like this
3.0 5.0 9.0 1.0
7.0 10.0 2.0 6.0
4.0 8.0 11.0 12.0
20.0 19.0 15.0 13.0
29.0 24.0 17.0 21.0
Here is my code
fileInStream.open(input);
cellArr = new double[rows * cols];
int i, j = 0;
while (getline(fileInStream, line)){
stringstream ss(line);
while (ss >> cell){
*(cellArr + i * cols + j) = stod(cell);
j++;
}
i++;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << *(cellArr + i * cols + j) << " ";
}
cout << endl;
}
My output looks like this
3 5 9 1
0 0 0 0
7 10 2 6
0 0 0 0
4 8 11 12