I am trying to make a program where the sum of the rows and columns are counted individual, the rows turn out fine, but the columns is giving weird results. The correct result is suppose to look like this (example Matrix)
9 1 2 4
2 2 8 0
3 3 3 3
Sum of row 0 is 16
Sum of row 1 is 12
Sum of row 2 is 12
Sum of column 0 is 14
Sum of column 1 is 6
Sum of column 2 is 13
Sum of column 3 is 7
But mine comes out as
Sum of row 0 is 16
Sum of row 1 is 12
Sum of row 2 is 12
Sum of column 0 is 565359518
Sum of column 1 is 32772
Sum of column 2 is 13
Sum of column 3 is 7
#include <iostream>
using namespace std;
int main()
{
int value[3][4];
int sum = 0;
int row, col;
int i, j;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << "Enter a value : " << endl;
cin >> value[i][j];
}
}
cout << "Value of the array: " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << value[i][j] << " ";
}
cout << endl;
}
void row_sum(int value[i][j]);
{
int i, j, sum = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
sum = sum + value[i][j];
}
cout
<< "Sum of the row "
<< i << " is " << sum
<< endl;
sum = 0;
}
void col_sum(int value[i][j]);
{
int i, j, sum = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
sum = sum + value[j][i];
}
cout
<< "Sum of the column "
<< i << " is " << sum
<< endl;
sum = 0;
}
}
return 0;
}
}
(Originally it was i = 3, but i changed it to 4 because I thought that was what was wrong with the code, but the column still gives weird results.)