Basically, the program given to me is to add the sum of the array's rows and columns and give it back into the same array
if m = 3 , n = 4
Eg:
3 4 5 6 18
1 2 3 4 10
4 3 3 2 12
8 9 11 12
(The last 12 is the sum of 6 + 4 +2)
So, this is the code for it
void summatrix()
{
for(int i=0;i<m-1;i++) // m - 1 because total array size is m but values only placed till m-1
{
int sum = 0;
for(int j=0;j<n-1;j++)
{
sum += mat[i][j];
mat[i][n] = sum; // Pretty sure the problem is here
}
System.out.println("Sum of "+ i + " " + (n) + " is "+ sum);
}
for(int j=0;j<n-1;j++) // n-1 beacuse total array size is n, values only placed till n-1
{
int sum=0;
for(int i=0;i<m-1;i++)
{
sum+=mat[i][j];
mat[m][j] = sum; // Pretty sure the problem is here
}
System.out.println("Sum of "+ (m) + " " + j + " is "+ sum);
}
show_mat(); // prints the array
Input and output :
The matrix is :
1 2 3 0
1 2 3 0
1 2 3 0
0 0 0 0
Sum of 0 4 is 6
Sum of 1 4 is 6
Sum of 2 4 is 6
Sum of 4 0 is 3
Sum of 4 1 is 6
Sum of 4 2 is 9
The matrix is :
1 2 3 0
1 2 3 0
1 2 3 0
0 0 0 0
My printing code:
void show_mat()
{
System.out.println("The matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
I can post all of my code or anymore whatever is needed if wanted. Sorry if my coding looks bad.. still relatively new to this.