123k views
4 votes
What is the printout (display) of the following program? public class Test { public static void main(String[] args) { 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); } }

User Jmehrens
by
7.6k points

2 Answers

2 votes

Answer:

3

Step-by-step explanation:

I just took it and got 100%

User Viktoriya
by
7.1k points
6 votes

Answer:

The printout of the program is 33

Step-by-step explanation:

Given the code as follows:

  1. int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};
  2. int v = values[0][0];
  3. for (int row = 0; row < values.length; row++)
  4. for (int column = 0; column < values[row].length; column++)
  5. if (v < values[row][column])
  6. v = values[row][column];
  7. 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).
User Taranttini
by
6.8k points