Tracing nested loops over a 2D array

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Tracing nested loops over a 2D array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In a trace table for nested loops over a 2D array, which variable keeps the same value while the inner loop runs?
A The outer loop variable (Row)
B The inner loop variable (Column)
2. A 2D array has 3 rows and 4 columns. A nested loop visits every element once. How many passes does the inner loop body make in total?
A 7 (3 + 4)
B 12 (3 x 4)
3. FOR Row <- 1 TO 2 with FOR Column <- 1 TO 3 inside it. Which Row and Column pair is the SECOND one written in the trace table?
A Row 2, Column 1
B Row 1, Column 2
4. Which shows the correct order of the first three Row, Column pairs for FOR Row <- 1 TO 2 and FOR Column <- 1 TO 3?
A 1,1 then 1,2 then 1,3
B 1,1 then 2,1 then 1,2
5. When does the Row value change in the trace table of a nested loop?
A Only after Column has finished its last value
B After every single pass of the inner loop

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1FOR Row ← 1 TO 3 FOR Col ← 1 TO 4 AGrid[Row, Col] ← Row * Col
2Row = 2 | Col = 1, 2, 3 BThe loops make 3 x 4 = 12 passes. Row stays fixed while Col runs 1 to 4, so the trace table needs 12 rows.
3A trace table has a column for the element changed, so the student records which array cell is written on each pass CThe total passes were added instead of multiplied. It should be 2 x 3 = 6 passes, so the trace table needs 6 lines.
4The order elements are visited: the outer loop moves down the rows, and the inner loop moves across the columns of each row DFOR Row ← 1 TO 2 FOR Col ← 1 TO 3
5Row 1, Col 1 then Row 2, Col 1 then Row 3, Col 1 EA trace table shows the outer variable fixed at 2 while the inner variable runs through all of its values.
62 rows x 3 columns = 5 passes FThe trace table changes Row on every pass. The outer variable should stay fixed while Col runs through all its values before Row changes.

Definitions

trace table

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
FOR Row ← 1 TO 2 FOR ______ ← 1 TO 3 Grid[Row, Column] ← Row + Column
2.
Rows ← 2 Columns ← 3 Passes ← Rows _ Columns
3.
FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 Grid[___, Column] ← 0
4.
FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 // Last element visited: Grid[2, _]
5.
Grid[Row, Column] ← 0 NEXT ______ NEXT Row

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A FOR Row ← 1 TO 2
B FOR Col ← 1 TO 3
C G[Col, Row] ← Row * Col
D NEXT Col
E NEXT Row
Question 2
A // 3 rows, 4 columns: 12 passes
B FOR Row ← 1 TO 4
C FOR Col ← 1 TO 4
D Grid[Row, Col] ← 0
E NEXT Col
F NEXT Row
Question 3
A FOR Row ← 1 TO 2
B FOR Col ← 1 TO 2
C T[Row, Col] ← Row + Col
D NEXT Row
E NEXT Row
Question 4
A FOR Row ← 1 TO 3
B FOR Row ← 1 TO 2
C OUTPUT Row, Col
D NEXT Col
E NEXT Row
Question 5
A S ← 1
B FOR Row ← 1 TO 2
C FOR Col ← 1 TO 3
D S ← S + A[Row, Col]
E NEXT Col
F NEXT Row

Answer Key

Tracing nested loops over a 2D array
Section A: Quick Questions
Q1
A — The outer loop variable (Row)
Q2
B — 12 (3 x 4)
Q3
B — Row 1, Column 2
Q4
A — 1,1 then 1,2 then 1,3
Q5
A — Only after Column has finished its last value
Section B: Match the Pairs
Q1
FOR Row ← 1 TO 3 FOR Col ← 1 TO 4 → The loops make 3 x 4 = 12 passes. Row stays fixed while Col runs 1 to 4, so the trace table needs 12 rows.
Q2
Row = 2 | Col = 1, 2, 3 → A trace table shows the outer variable fixed at 2 while the inner variable runs through all of its values.
Q3
A trace table has a column for the element changed, so the student records which array cell is written on each pass → Grid[Row, Col] ← Row * Col
Q4
The order elements are visited: the outer loop moves down the rows, and the inner loop moves across the columns of each row → FOR Row ← 1 TO 2 FOR Col ← 1 TO 3
Q5
Row 1, Col 1 then Row 2, Col 1 then Row 3, Col 1 → The trace table changes Row on every pass. The outer variable should stay fixed while Col runs through all its values before Row changes.
Q6
2 rows x 3 columns = 5 passes → The total passes were added instead of multiplied. It should be 2 x 3 = 6 passes, so the trace table needs 6 lines.
Section C: Definitions
Q1
trace table — Tracks Row, Column and the element changed. The outer loop variable stays fixed while the inner one runs through all its values. Total passes = rows x columns.
Section D: Fill in the Blanks
Q1
FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 Grid[Row, Column] ← Row + Column
Q2
Rows ← 2 Columns ← 3 Passes ← Rows * Columns
Q3
FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 Grid[Row, Column] ← 0
Q4
FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 // Last element visited: Grid[2, 3]
Q5
Grid[Row, Column] ← 0 NEXT Column NEXT Row
Section E: Spot the Error
Q1
C — The first index is the row and the second is the column, so it must be G[Row, Col]. With G[Col, Row], Col reaches 3 and goes past the 2 rows, and the trace table would record the wrong element.
Q2
B — The array has only 3 rows, so the outer loop must stop at 3. Going to 4 gives 4 x 4 = 16 passes instead of 3 x 4 = 12, and Row = 4 is out of range.
Q3
D — Loops must be closed in the reverse order they were opened. The inner loop must end with NEXT Col before the outer loop ends with NEXT Row.
Q4
B — The inner loop reuses the variable Row, so the outer variable would not stay fixed. The inner loop must use its own variable: FOR Col ← 1 TO 2.
Q5
A — The total must start at 0. Starting at 1 makes every value in the S column of the trace table, and the final total, one too big.