DEV Community

Neelakandan R
Neelakandan R

Posted on • Edited on

Multidimensional array--matrix

Gross matrix:X

package afterfeb13; public class multidimarray { public static void main(String[] args) { int[][] mark = { { 10, 20, 80 }, { 40, 50, 60 }, { 70, 80, 90 } }; int total = 0; int sum = 0; for (int row = 0; row < mark.length; row++) { for (int col = 0; col < mark[row].length; col++) { if (row + col == 2) { total = total + mark[row][col]; } if (col == row) { sum = sum + mark[row][col]; } } }System.out.println("row + col == 2 = " + total); System.out.println("col==row = " + sum); System.out.println("sum+total = "+ (sum+total)); } } 
Enter fullscreen mode Exit fullscreen mode

Output:

row + col == 2 = 200
col==row = 150
sum+total = 350

maxium 1 present in which index:

public class multidimentional { public static void main(String[] args) { int[][] arr = { { 1, 1, 1, 1, 0 }, { 1, 1, 1, 0, 0 }, { 0, 1, 0, 1, 0 } }; int max = 0; int row_MaxOne = 0; for (int row = 0; row < arr.length; row++) { int count = 0; for (int col = 0; col < arr[row].length; col++) { if (arr[row][col] == 1) { count++; } } // System.out.println(row + "index count = " + count + " "); if (count > max) { max = count; row_MaxOne = row;// sending row index[0][1][2] } } System.out.println("index has max ones = " + row_MaxOne + "th index"); System.out.println("max one present is = " + max); } } 
Enter fullscreen mode Exit fullscreen mode

Output:

index has max ones = 0th index
max one present is = 4

Top comments (0)