The shape of the lesson
By the end of the lesson, learners can
- Declaring a 2D array: The two bounds in the declaration give the number of rows and the number of columns.
- Nested loops: In the lab, choose 2D array with nested loops.
- Column totals and grand totals: Swap the loops to work down each column instead of across each row.
- Declare: ARRAY[1:rows, 1:columns] OF type.
- Access: Name[Row, Column]; elements = rows × columns.
- Nested loops: the inner loop completes for each pass of the outer loop.
- Totals: reset a row total inside the outer loop, before the inner loop.
One picture every idea returns to
A two-dimensional (2D) array is a table of elements of the same data type, with two indexes: one for the row and one for the column. Declare it with two sets of bounds: DECLARE Sales : ARRAY[1:3, 1:4] OF INTEGER (3 rows, 4 columns, 12 elements). Access an element with Sales[Row, Column]. To process every element, use nested loops: the inner loop runs completely for every pass of the outer loop.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open with the question in the title, Rows and columns.. Some data comes as a table: sales for each shop in each week, a noughts and crosses board, marks for each student in each test. Put the figure on the board: it is the one picture every idea in the lesson returns to. | Whole class, then pairs |
| 5 to 13 min | Teach and check: Declaring a 2D array | The two bounds in the declaration give the number of rows and the number of columns. Your turn: Using the table, what are Sales[3, 1] and Sales[1, 4], and how many elements does Sales have? | Whole class, then pairs |
| 13 to 21 min | Teach and check: Nested loops | In the lab, choose 2D array with nested loops. Project tab 6 of the lab (2D arrays with nested loops) and let a student drive it while the class predicts. Model the worked example Row totals on the board, one line at a time. | Pairs |
| 21 to 30 min | Teach and check: Column totals and grand totals | Swap the loops to work down each column instead of across each row. Model the worked example Column totals on the board, one line at a time. Your turn: A 2D array is declared ARRAY[1:5, 1:6]. How many times does the statement inside a pair of nested loops over every element run? | Mix-Pair-Share |
| 30 to 36 min | Check | The four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong. | Individual, then whole class |
| 36 to 43 min | Plenary | The exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording. | Individual, random call |
| 43 to 45 min | Exit and homework | Set the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave. | Individual |
Protect the parts that carry the learning
The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.
Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.
Cut first if the build overruns: the last check in Column totals and grand totals, never the check or the plenary.
In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.
Mix-Pair-Share
Students mix around the room until you call pair; they pair with the nearest classmate, discuss the prompt, then mix again. Three prompts of rising difficulty, three different partners.
The full structure, with its variations and the other twelve, is on the cooperative-learning page.
What to head off, and how
| Trap learners fall into | Teaching move that pre-empts it |
|---|---|
| Do not swap the indexes. | Sales[3, 2] is row 3, column 2 (the value 1), but Sales[2, 3] is row 2, column 3 (the value 4). |
| RowTotal ← 0 must be inside the outer loop but before the inner loop. | Outside both loops, each row's total would include the rows before it. |
Two-dimensional arrays and nested loops: worksheet
6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.
Describe a two-dimensional array.
Write a declaration for a 2D array Seats that stores a CHAR for each seat in a cinema with 12 rows of 20 seats.
State the values stored and the output.
FOR R ← 1 TO 2
FOR C ← 1 TO 3
Grid[R, C] ← R * C
NEXT C
NEXT R
OUTPUT Grid[2, 3], " ", Grid[1, 2]Marks[1:30, 1:4] stores 4 test marks for each of 30 students (one row per student). Write pseudocode that outputs each student's total mark.
A student puts RowTotal ← 0 before both loops in a row-total program. Explain the effect on the output.
Using the Sales table in this lesson (3 shops by 4 weeks), state (a) Sales[1, 2] (b) the total for week 4 (c) the total for shop 2.
Answer key and marking notes
- A table-like data structure with rows and columns, under one identifier, all elements the same data type.
- Each element is accessed with two indexes: row and column.
DECLARE Seats : ARRAY[1:12, 1:20]OF CHAR
- Row 1: 1, 2, 3.
- Row 2: 2, 4, 6.
- Output: 6 2
FOR Student ← 1 TO 30Total ← 0inside the outer loop, before the inner loopFOR Test ← 1 TO 4...NEXT TestTotal ← Total + Marks[Student, Test]inside the inner loopOUTPUT Student, Totalafter the inner loop, thenNEXT Student
- RowTotal is never reset between rows.
- So each row's output is a running total that includes all previous rows; only the first row's total is correct.
- (a) 8
- (b) 6 + 7 + 8 = 21
- (c) 3 + 9 + 4 + 7 = 23
6 multiple-choice questions on this lesson
From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.
How many elements are in the array declared by DECLARE Seats : ARRAY[1:4, 1:6] OF CHAR?
- A24
- B10
- C6
- D35
The 2D array Sales holds these values.
| Sales | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | 14 | 9 | 12 | 7 |
| Row 2 | 11 | 16 | 8 | 13 |
| Row 3 | 5 | 10 | 15 | 6 |
OUTPUT Sales[2, 3]?- A8
- B10
- C16
- D12
How many times is OUTPUT "*" run?
FOR Row ← 1 TO 3
FOR Column ← 1 TO 4
OUTPUT "*"
NEXT Column
NEXT Row- A12
- B7
- C4
- D3
Sales holds the values in the table.
| Sales | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | 14 | 9 | 12 | 7 |
| Row 2 | 11 | 16 | 8 | 13 |
| Row 3 | 5 | 10 | 15 | 6 |
FOR Row ← 1 TO 3
RowTotal ← 0
FOR Column ← 1 TO 4
RowTotal ← RowTotal + Sales[Row, Column]
NEXT Column
OUTPUT "Shop ", Row, " total: ", RowTotal
NEXT Row- AShop 2 total: 48
- BShop 2 total: 35
- CShop 2 total: 90
- DShop 2 total: 42
Sales holds the values in the table.
| Sales | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | 14 | 9 | 12 | 7 |
| Row 2 | 11 | 16 | 8 | 13 |
| Row 3 | 5 | 10 | 15 | 6 |
RowTotal ← 0 to before the outer loop.RowTotal ← 0
FOR Row ← 1 TO 3
FOR Column ← 1 TO 4
RowTotal ← RowTotal + Sales[Row, Column]
NEXT Column
OUTPUT "Shop ", Row, " total: ", RowTotal
NEXT RowWhat is the third line output?- AShop 3 total: 126
- BShop 3 total: 36
- CShop 3 total: 90
- DShop 3 total: 48
Grid holds the values in the table.
| Grid | Column 1 | Column 2 | Column 3 |
|---|---|---|---|
| Row 1 | 4 | 1 | 2 |
| Row 2 | 9 | 5 | 8 |
| Row 3 | 6 | 3 | 7 |
Total ← 0
FOR Index ← 1 TO 3
Total ← Total + Grid[Index, 4 - Index]
NEXT Index
OUTPUT Total- A13
- B16
- C19
- D7
Quiz key
There are 4 rows and 6 columns, so 4 × 6 = 24 elements. 10 adds the bounds instead of multiplying. 6 is only the number of columns. 35 treats the bounds as running from 0 (5 × 7).
Sales[2, 3] is row 2, column 3, which holds 8. 10 is Sales[3, 2], with the indexes swapped. 16 is row 2, column 2. 12 is row 1, column 3.
The outer loop makes 3 passes and the inner loop runs 4 times in each, so the OUTPUT runs 3 × 4 = 12 times. 7 adds the loop counts. 4 counts only one pass of the outer loop. 3 counts only the outer loop.
On the second pass Row = 2 and the inner loop adds 11 + 16 + 8 + 13 = 48. 35 is the total of column 2, with rows and columns confused. 90 would be the running total of rows 1 and 2, which happens only if RowTotal is not reset. 42 is the total of row 1.
Because RowTotal is only set to 0 once, each output includes all rows so far: 42, then 42 + 48 = 90, then 90 + 36 = 126. 36 is the correct row 3 total from the original program. 90 is the second line. 48 is the row 2 total.
The elements used are Grid[1, 3] = 2, Grid[2, 2] = 5 and Grid[3, 1] = 6: the diagonal from top right to bottom left, total 13. 16 is the other diagonal, Grid[Index, Index] (4 + 5 + 7). 19 is the total of column 1. 7 is the total of row 1.
What to project
The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.
Slides for this lesson
A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.