Filling a 2D array with nested FOR loops

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

Filling a 2D array with nested FOR loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which structure visits every element of a 3 x 3 2D array?
A A FOR Column loop inside a FOR Row loop
B A single FOR Row ← 1 TO 3 loop
2. Which shows the NEXT statements in the correct order for nested loops?
A NEXT Column, then NEXT Row
B NEXT Row, then NEXT Column
3. FOR Row ← 1 TO 3 contains FOR Column ← 1 TO 3. How many times does the inner loop body run in total?
A 9 times (3 for each row)
B 3 times
4. Which line, inside the inner loop, sets the current element of Board to a space?
A Board[Row] ← ' '
B Board[Row, Column] ← ' '
5. Row is 2. What does the inner loop do?
A Column becomes 2, then Row moves on to 3
B Column runs 1 to 3 completely, then Row becomes 3

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 Column ← 1 TO 3 AFOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Column NEXT Row
2Board[Row, Column] ← ' ' BNEXT Column NEXT Row
3The inner loop must be closed first, so its NEXT comes before the NEXT of the outer loop CSets the current element, chosen by the row and column loop variables, to a space
4A complete nested loop that sets every element of a 3 by 3 array to a space DThe Row variable is used for both indexes. The second index should be Column, so only the diagonal elements are set.
5FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Row NEXT Column EOuter loop steps through the rows; the inner loop steps through the columns for each row
6FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Row] ← ' ' NEXT Column NEXT Row FThe NEXT statements are in the wrong order. The inner loop (Column) must be closed first, then the outer loop (Row).

Definitions

nested FOR loop

Fill in the Blanks

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

1.
FOR Row ← 1 TO 3 ___ Column ← 1 TO 3 Board[Row, Column] ← ' '
2.
FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT ______
3.
FOR Row ← 1 TO _ FOR Column ← 1 TO 3 Board[Row, Column] ← ' '
4.
FOR Column ← 1 TO 3 Board[Row, Column] _ ' ' NEXT Column
5.
Board[Row, Column] ← ' ' NEXT Column ____ Row

Spot the Error

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

Question 1
A FOR Row = 1 TO 3
B FOR Column ← 1 TO 3
C Board[Row, Column] ← ' '
D NEXT Column
E NEXT Row
Question 2
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 3
C Grid[Row] ← ' '
D NEXT Column
E NEXT Row
Question 3
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 4
C Board[Row, Column] ← ' '
D NEXT Column
E NEXT Row
Question 4
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 3
C Table[Row, Column] ← ' '
D NEXT Row
E NEXT Row
Question 5
A FOR Row ← 1 UNTIL 3
B FOR Column ← 1 TO 3
C Map[Row, Column] ← ' '
D NEXT Column
E NEXT Row

Answer Key

Filling a 2D array with nested FOR loops
Section A: Quick Questions
Q1
A — A FOR Column loop inside a FOR Row loop
Q2
A — NEXT Column, then NEXT Row
Q3
A — 9 times (3 for each row)
Q4
B — Board[Row, Column] ← ' '
Q5
B — Column runs 1 to 3 completely, then Row becomes 3
Section B: Match the Pairs
Q1
FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 → Outer loop steps through the rows; the inner loop steps through the columns for each row
Q2
Board[Row, Column] ← ' ' → Sets the current element, chosen by the row and column loop variables, to a space
Q3
The inner loop must be closed first, so its NEXT comes before the NEXT of the outer loop → NEXT Column NEXT Row
Q4
A complete nested loop that sets every element of a 3 by 3 array to a space → FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Column NEXT Row
Q5
FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Row NEXT Column → The NEXT statements are in the wrong order. The inner loop (Column) must be closed first, then the outer loop (Row).
Q6
FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Row] ← ' ' NEXT Column NEXT Row → The Row variable is used for both indexes. The second index should be Column, so only the diagonal elements are set.
Section C: Definitions
Q1
nested FOR loop — A FOR loop for the columns placed inside a FOR loop for the rows. The inner loop runs fully for each row, so every element of a 2D array is visited and given a value.
Section D: Fill in the Blanks
Q1
FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' '
Q2
FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Column
Q3
FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' '
Q4
FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Column
Q5
Board[Row, Column] ← ' ' NEXT Column NEXT Row
Section E: Spot the Error
Q1
A — A FOR loop sets its counter with the assignment arrow ←, not =. The line should be FOR Row ← 1 TO 3.
Q2
C — A 2D array element needs both indexes. It should be Grid[Row, Column] ← ' ' so the inner loop sets each column in the row.
Q3
B — The board only has 3 columns, so the inner loop must stop at 3. Going to 4 causes an index out of range error. It should be FOR Column ← 1 TO 3.
Q4
D — The inner loop must be closed first, so line 4 should be NEXT Column. NEXT Row comes last, after the inner loop has finished.
Q5
A — A count-controlled loop uses the keyword TO, not UNTIL. It should be FOR Row ← 1 TO 3.