Displaying a 2D array with nested loops

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

Displaying a 2D array with nested loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which nested loop layout displays a 2D array in row order?
A Outer loop for columns, inner loop for rows
B Outer loop for rows, inner loop for columns
2. Where should OUTPUT Grid[Row, Column] go to display every element once?
A Inside the inner loop
B After the outer loop has finished
3. A 2D array Grid[1:3, 1:4] is displayed with nested loops. How many times does OUTPUT Grid[Row, Column] run?
A 12 times
B 7 times
4. Which FOR line is the inner loop for Seats[1:5, 1:8] when the outer loop uses Row?
A FOR Column ← 1 TO 8
B FOR Column ← 1 TO 5
5. Which code outputs ONLY row 2 of Grid[1:3, 1:4]?
A FOR Row ← 1 TO 3 OUTPUT Grid[Row, 2] NEXT Row
B FOR Column ← 1 TO 4 OUTPUT Grid[2, Column] NEXT Column

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 AThe OUTPUT line gives only one index. A 2D array needs both the row and the column, so it should be Grid[Row, Column].
2OUTPUT Grid[2, Column] BInside a loop over Column, this displays every element of row 2 only.
3The inner loop moves across the columns, and OUTPUT inside it displays one element each time round. CThe outer loop: it moves through each row of the 2D array, one row at a time.
4To display only column 3 of a 2D array, loop through the rows and keep the column index fixed. DFOR Column ← 1 TO 4 OUTPUT Grid[Row, Column] NEXT Column
5FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row] NEXT Column NEXT Row EThe outer loop has no NEXT Row, so it is never closed. NEXT Row must be added after NEXT Column.
6FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column] NEXT Column FFOR Row ← 1 TO 3 OUTPUT Grid[Row, 3] NEXT Row

Definitions

OUTPUT Name[Row, Column]

Fill in the Blanks

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

1.
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 ______ Grid[Row, Column]
2.
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row, ______]
3.
FOR Row ← 1 TO 3 OUTPUT Grid[___, 2] NEXT Row
4.
OUTPUT Grid[Row, Column] NEXT ______ NEXT Row
5.
FOR Row ← 1 TO 3 FOR Column ← 1 __ 4 OUTPUT Seats[Row, Column]

Spot the Error

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

Question 1
A FOR Row 1 TO 3
B FOR Col ← 1 TO 4
C OUTPUT Grid[Row, Col]
D NEXT Col
E NEXT Row
Question 2
A FOR Row ← 1 TO 3
B FOR Col ← 1 TO 4
C OUTPUT Grid[Row, Col]
D NEXT Row
E NEXT Row
Question 3
A FOR Student ← 1 TO 4
B FOR Test ← 1 TO 3
C OUTPUT Scores[Test, Student]
D NEXT Test
E NEXT Student
Question 4
A FOR Row ← 1 TO 5
B FOR Row ← 1 TO 8
C OUTPUT Seats[Row, Seat]
D NEXT Seat
E NEXT Row
Question 5
A FOR Day ← 1 TO 7
B FOR Hour ← 1 TO 3
C OUTPUT Temp[Day]
D NEXT Hour
E NEXT Day

Answer Key

Displaying a 2D array with nested loops
Section A: Quick Questions
Q1
B — Outer loop for rows, inner loop for columns
Q2
A — Inside the inner loop
Q3
A — 12 times
Q4
A — FOR Column ← 1 TO 8
Q5
B — FOR Column ← 1 TO 4 OUTPUT Grid[2, Column] NEXT Column
Section B: Match the Pairs
Q1
FOR Row ← 1 TO 3 → The outer loop: it moves through each row of the 2D array, one row at a time.
Q2
OUTPUT Grid[2, Column] → Inside a loop over Column, this displays every element of row 2 only.
Q3
The inner loop moves across the columns, and OUTPUT inside it displays one element each time round. → FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column] NEXT Column
Q4
To display only column 3 of a 2D array, loop through the rows and keep the column index fixed. → FOR Row ← 1 TO 3 OUTPUT Grid[Row, 3] NEXT Row
Q5
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row] NEXT Column NEXT Row → The OUTPUT line gives only one index. A 2D array needs both the row and the column, so it should be Grid[Row, Column].
Q6
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column] NEXT Column → The outer loop has no NEXT Row, so it is never closed. NEXT Row must be added after NEXT Column.
Section C: Definitions
Q1
OUTPUT Name[Row, Column] — Placed inside nested FOR loops (outer loop for rows, inner loop for columns), this line displays each element of a 2D array once, in row order.
Section D: Fill in the Blanks
Q1
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column]
Q2
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column]
Q3
FOR Row ← 1 TO 3 OUTPUT Grid[Row, 2] NEXT Row
Q4
OUTPUT Grid[Row, Column] NEXT Column NEXT Row
Q5
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Seats[Row, Column]
Section E: Spot the Error
Q1
A — The outer loop for the rows is missing the assignment arrow. It should be FOR Row ← 1 TO 3.
Q2
D — The inner loop is FOR Col, so it must end with NEXT Col. NEXT Row belongs on the last line, closing the outer loop.
Q3
C — The row index must come first and the column index second. Scores[Test, Student] swaps them, so the elements are not shown in row order. It should be Scores[Student, Test].
Q4
B — The inner loop reuses the variable Row, which clashes with the outer loop. The inner loop for the columns needs its own variable: FOR Seat ← 1 TO 8.
Q5
C — A 2D array needs two indexes, a row and a column. Temp[Day] gives only one. It should be OUTPUT Temp[Day, Hour].