Lesson 5.13 · Trace it
Trace: column totals (Java)
Java code19 rows to fill3 marks
Complete the trace table for this Java code. grid has 3 rows of 2 values, and grid[r][c] is the value in row r, column c. The outer loop goes along the columns and the inner loop goes down the rows. Then answer the question below the table. [3]
Line 1: int[][] grid = {{4, 7}, {9, 3}, {6, 8}};Line 2: for (int c = 0; c < grid[0].length; c++) {Line 3, indented 1 level: int total = 0;Line 4, indented 1 level: for (int r = 0; r < grid.length; r++) {Line 5, indented 2 levels: total = total + grid[r][c];Line 6, indented 1 level: }Line 7: }
Fill in one row each time a line changes c, r or total. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Java shows them: 3 and 3.0 are different, and text goes in quotes.
| Row | c | r | total |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 |