Java examples for Data Structure:Matrix
Write code to do Matrix Diagonals Sum
You can use the following code structure and fill in your logics.
public class Main { public static void main(String args[]) throws Exception { int sumD1 = 0; int mRow = 5; int mCol = 5; int[][] Array = new int[mRow][mCol]; for (int r = 0; r < Array.length; r++) { for (int c = 0; c < Array[r].length; c++) { Array[r][c] = r * c; } } // Matrix printing block for (int r = 0; r < Array.length; r++) { for (int c = 0; c < Array[r].length; c++) { System.out.print(Array[r][c] + " "); } System.out.println(); } // End of Printing //your code here } }
public class Main { public static void main(String args[]) throws Exception { int sumD1 = 0; int mRow = 5; int mCol = 5; int[][] Array = new int[mRow][mCol]; for (int r = 0; r < Array.length; r++) { for (int c = 0; c < Array[r].length; c++) { Array[r][c] = r * c;/*from w w w . j a v a 2 s . c o m*/ } } // Matrix printing block for (int r = 0; r < Array.length; r++) { for (int c = 0; c < Array[r].length; c++) { System.out.print(Array[r][c] + " "); } System.out.println(); } // End of Printing // diagonal sum block for (int r = 0; r < Array.length; r++) { for (int c = 0; c < Array[r].length; c++) { if (r == c) { sumD1 += Array[r][c]; } } } System.out.println("Sum Diagonal 1: " + sumD1); // end of d1 vlock // diagonal 2 block int count = 0; int sumD2 = 0; int temp; int r, c; for (r = 0, c = mCol - 1; r < Array.length && c >= 0; r++, c--) { // r=0 c=3-1=2 v=0,2 // r=1 c=2-1=1 v=1,1 // r=2 c=2-1=1 v=2,1 sumD2 = sumD2 + Array[r][c]; } System.out.println("Sum Diagonal 2: " + sumD2); if (sumD1 == sumD2) { System.out.print("Equal"); } else if (sumD1 > sumD2) { System.out.print("diagonal 1: " + sumD1); } else System.out.print("Diagonal 2: " + sumD2); } }