Given a matrix, print the sum of the row elements in the matrix.
For example, consider the matrix below.
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
The row sum of the matrix above is as follows.
6
15
24
6
) indicates the sum of the elements of the first row, i.e., 1
, 2
, and 3
.15
) indicates the sum of the elements of the second row, i.e., 4
, 5
, and 6
.24
) indicates the sum of the elements of the third row, i.e., 7
, 8
, and 9
.rowSum
.rowSum
array defined in Step 1 of the algorithm.rowSum
array.import java.util.Arrays;public class Main{private static int[] printRowSum(int[][] matrix){int numRows = matrix.length;int numCols = matrix[0].length;int[] rowSum = new int[numRows];for(int i = 0; i < numRows; i++){int sum = 0;for(int j = 0; j < numCols; j++) sum += matrix[i][j];rowSum[i] = sum;}return rowSum;}private static void printMatrix(int[][] matrix){for (int[] row : matrix)System.out.println(Arrays.toString(row));}public static void main(String[] args){int matrix[][] = {{1, 2, 3},{4, 5, 6},{7, 8 , 9}};printMatrix(matrix);System.out.println("The row sum of the above matrix is as follows:");int[] rowSum = printRowSum(matrix);for(int i: rowSum)System.out.println(i);}}