File info:
6 10
0 1 0 1 0 1 1 1 1 1
0 1 1 1 1 1 1 1 1 0
0 0 1 1 1 1 1 1 1 0
0 1 1 1 1 0 1 0 1 1
0 1 1 1 1 1 0 1 1 1
0 1 1 1 0 1 1 0 1 1
The script:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int x; int y;
int masyvas[x][y]; // masyvas is 2D array
int sum = 0;
ifstream D ("Duomenys.txt"); // the files name
D >> x >> y; // x and y is 6 and 10
// The scanning/reading
for (int i = 1; i < x + 1; i++)
{
for (int j = 1; j < y + 1; j++)
{
D >> masyvas[i][j];
}
}
// the printing
for (int i = 1; i < x + 1; i++){
for (int j = 1 ; j < y + 1; j++){
cout << masyvas[x][j] << " ";
sum = sum + masyvas[i][j];
}
cout << sum << endl;
sum = 0;
}
D.close();
return 0;
}
Now when I test this file ( compile it ) getting no errors but I get this.
Result:
0 1 0 1 0 1 1 1 1 1 7
0 1 0 1 0 1 1 1 1 1 7
0 1 0 1 0 1 1 1 1 1 7
0 1 0 1 0 1 1 1 1 1 7
0 1 0 1 0 1 1 1 1 1 7
0 1 0 1 0 1 1 1 1 1 7
Now, why does it repeat? Where am I doing it wrong? And did I do right with the rows? ( trying to sum them up)
The wanted result:
0 1 0 1 0 1 1 1 1 1 7
0 1 1 1 1 1 1 1 1 0 8
0 0 1 1 1 1 1 1 1 0 7
0 1 1 1 1 0 1 0 1 1 7
0 1 1 1 1 1 0 1 1 1 8
0 1 1 1 0 1 1 0 1 1 7
How can I achieve that?