-1

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.

sun
  • 305
  • 4
  • 13

3 Answers3

0

Put the mat[i][n-1] = sum; and mat[m-1][j] = sum; outside the inner for loop and inside the outer for loop.

nbro
  • 15,395
  • 32
  • 113
  • 196
Ripu Daman
  • 2,482
  • 2
  • 18
  • 22
0

In your inner loop you should set the value of your sum after the loop and only once. Something like this:

int sum = 0;
for(int j = 0; j < n-1; j++) {
    sum += mat[i][j];
}
mat[i][n-1] = sum;//n-1
nbro
  • 15,395
  • 32
  • 113
  • 196
0

Make sure that the cells to contain the row and column sum are zero before summing.

Try

package com.example.matrix;

public class MatrixOperation {

    public void summatrix(int[][] matrix) {
        int rows;
        int cols;

        rows = matrix.length;
        cols = matrix[0].length;
        // initialize sums
        for (int i=0; i < rows-1; i++) {
            matrix[i][cols-1] = 0;
        }
        for (int j=0; j < cols-1; j++) {
            matrix[rows-1][j] = 0;
        }

        for (int i=0; i < rows-1; i++) {
            for (int j=0; j < cols-1; j++) {
                matrix[rows-1][j] = matrix[rows-1][j]+matrix[i][j];
                matrix[i][cols-1] = matrix[i][cols-1] + matrix[i][j];
            }
        }


        for (int i=0; i < rows; i++) {
            for (int j=0; j < cols; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println("");
        }
    }
}
ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37