Answer:
The printout of the program is 33
Step-by-step explanation:
Given the code as follows:
- int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};
- int v = values[0][0];
- for (int row = 0; row < values.length; row++)
- for (int column = 0; column < values[row].length; column++)
- if (v < values[row][column])
- v = values[row][column];
-
- System.out.print(v);
The code above will find the largest value from the two-dimensional array and print it out.
The logic of finding the largest value is as follows:
- Create a variable, v, to hold the largest number (Line 2). At the first beginning, we simply set the value from the first row and first column (values[0][0]) as our current largest value.
- Next, we need to compare our current largest value against the rest of the elements in the two dimensional array.
- To make the comparison, we need a two-layers for loops that will traverse through every row and column of the array and compare each of the element with the current largest value (Line 3-6).
- If the current largest value, v, is smaller than any element of the array, update the v to the latest found largest value (Line 5-6).
- After completion of the for-loop, the v will hold the largest number and the program will print it out (Line 8).