Java : Program to find the sum of each row and each column of n x m of 2D Matrix


package jun2;

/*
* Find the sum of each row and each column of n x m of 2D Matrix
*
*/

public class SumOf2DMatrixRowsAndColumn {


public static void main(String[] args) {

int [][] twoDMatrix = {{ 20, 18, 23, 20, 16 },
{ 30, 20, 18, 21, 20 },
{ 16, 19, 16, 53, 24 },
{ 25, 24, 22, 24, 25 }
};
outputArray(twoDMatrix);
}

public static void outputArray(int[][] array) {
int sum= 0;
int rowSize = array.length;

int[] colSum =new int[array[0].length];
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
sum += array[i][j];
colSum[j] += array[i][j];
}
System.out.println("sum of rows "+ i +" = " + sum);
}
System.out.println(" ");
for(int k=0;k<colSum.length;k++){
System.out.println("sum of columns "+ k +" = " + colSum[k]);
}


}

}

/*output:

sum of rows 0 = 97
sum of rows 1 = 206
sum of rows 2 = 334
sum of rows 3 = 454

sum of columns 0 = 91
sum of columns 1 = 81
sum of columns 2 = 79
sum of columns 3 = 118
sum of columns 4 = 85

*/

No comments:

Post a Comment

Android : How to connect your Android device over Wifi using ADB command for App debugging

How to connect your Android device over Wi-Fi using ADB command Sometimes it requires to connect your Android dev...