In: Computer Science
Document the arrays, using a chart with columns titled: outer loop, inner loop, i, j, and x. Finally, draw a picture of each array (after the program portion has executed). (5 pts) int[][] arr1 = new int[5][5]; int[][] arr2 = new int[5][5]; x = 1; for(int i = 0; I < 5; i++) { for(int j = 1; j < 6; j++) { arr1[i][j-1] = x; x++; if(x == 6) x += 2; } } for(int i = 4; i >= 0; i--) for(int j = 4; j > -1; j--) arr2[j][i] = arr1[i][j];
The answer is given in a tabular form :
Please note that in the first loop when i=0 and j=5, X has the value 5. Next time when the loop is executed, arr1[0][4] will be 5.
X gets incremented to 6. Now the if condition X==6 will be true . Hence x will get the value 8 after doing X+=2. Thats how arr1[1][0] has 8 in it. Hope this helps.