java - Sum of 3x3 in 2D-Array -
write program in java read 2 3 3 matrix , find out sum , display result?
i tried got runtime error
scanner r=new scanner(system.in); int [][]array = null; int[][]array2 = null; int total=0; system.out.println("enter matrix"); for(int row=0;row<array.length;row++){ for(int col=0;col<array[row].length;col++){ array[row][col]=r.nextint(); array[row][col]=r.nextint()
;
system.out.print(" "+total +" "); total=array[row][col]+array2[row][col]; system.out.println(" ");
the first for-loop demonstrates how input values in arrays. code require user inputs values of both arrays simultaneously.
the second for-loop demonstrates how sum values of each array. later, both arrays added together.
//since know the array 3x3, //declare it! int[][] array1 = new int[3][3]; int[][] array2 = new int[3][3]; int array1total = 0; int array2total = 0; int endresult; (int x = 0; x < array1.length; x++) { (int y = 0; y < array1[x].length; y++) { array1[x][y] = r.nextint(); array2[x][y] = r.nextint(); } } (int x = 0; x < array1.length; x++) { (int y = 0; y < array1[x].length; y++) { array1total += array1[x][y]; array2total += array2[x][y]; } } endresult = array1total + array2total;
Comments
Post a Comment