Advertisement
therrontelford

Summing column 2d matrix

Feb 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.48 KB | None | 0 0
  1.  
  2. public class SummingColumn8_1 {
  3.  
  4.     public static void main(String[] args) {
  5.         // create a matrix
  6.         int[][] matrix={
  7.                 {1,2,3},
  8.                 {4,5,6},
  9.         };
  10.         // outer loop controls the column number
  11.         // inner loop advances present column to the next row
  12.         for (int j=0; j<matrix[0].length; j++){
  13.             int columnTotal=0;
  14.             for (int i=0; i<matrix.length; i++){
  15.                 columnTotal+= matrix[i][j];
  16.             }
  17.             System.out.println("The total for column "+j+ " is "+ columnTotal);
  18.         }
  19.  
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement