The shape of the lesson
By the end of the lesson, learners can
- One dimension: Declare the array with its bounds and data type, then use a loop variable as the index to visit every element in turn.
- Two dimensions: In the lab, run the 2D array program: it fills a 2 by 3 table with nested loops and totals each row.
- Choosing a structure: Examiners ask you to select a 1D or 2D array and justify it.
- Elements = upper bound − lower bound + 1; a 2D array has rows × columns.
- Declare:
DECLARE X : ARRAY[1:n] OF typeorARRAY[1:r, 1:c] OF type. - Process: a FOR loop for 1D; nested FOR loops for 2D, with totals reset in the right loop.
- Choose: 2D when each value has a row and a column; records when the data types differ.
One picture every idea returns to
An array is a fixed-length data structure of elements that all have the same data type, held under one identifier. Each element is reached by its index (subscript). The lower bound is the index of the first element and the upper bound the index of the last, so ARRAY[1:30] has 30 elements. A 1D array is a list with one index; a 2D array is a table reached by [row, column]. Arrays are processed one element at a time with a FOR loop; a 2D array needs nested loops.
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, Many values, one name.. An array stores a fixed number of values of the same data type under one identifier, each reached by its index. 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: One dimension | Declare the array with its bounds and data type, then use a loop variable as the index to visit every element in turn. Model the worked example Trace table on the board, one line at a time. Your turn: Write a FOR loop that sets every element of Names: ARRAY[1:30] OF STRING to the empty string. | Whole class, then pairs |
| 13 to 21 min | Teach and check: Two dimensions | In the lab, run the 2D array program: it fills a 2 by 3 table with nested loops and totals each row. 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. Your turn: Swap the loops so that the outer loop is FOR Column ← 1 TO 4. What total is output for column 3? | Pairs |
| 21 to 30 min | Teach and check: Choosing a structure | Examiners ask you to select a 1D or 2D array and justify it. Model the worked example Select and declare on the board, one line at a time. | Corners |
| 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, 23 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 Choosing a structure, 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.
Corners
Put one answer or viewpoint in each corner of the room. Students move to the corner they agree with, justify the choice to a partner there, then hear one voice from each corner before the reveal.
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 |
|---|---|
| Start the maximum at the first element, Max ← Scores[1], not at 0: if every value were negative, a start of 0 would give the wrong answer. | Return to One dimension: ask a pair to state the correct version and show it on the lab before moving on. |
| Keep the index order the same as the declaration. | If the array is [1:3, 1:4], then Marks[4, 2] is out of range: there is no row 4. |
| Do not mix data types in one array. "A 2D array with a column of names and a column of marks" would need every element to be STRING, so the marks could not be used in arithmetic without conversion. | Return to Choosing a structure: ask a pair to state the correct version and show it on the lab before moving on. |
1D and 2D arrays: worksheet
6 questions, 23 marks. Written in the style of the papers; no past paper question is reproduced.
An array is declared as DECLARE Temps : ARRAY[0:23] OF REAL. State (a) the lower bound, (b) the upper bound, (c) the number of elements.
Write pseudocode to declare a 1D array Names to hold 50 names, and to set every element to the empty string.
Nums holds 3, 8, 2, 8, 5, 1 in elements 1 to 6. Complete a trace table for this pseudocode, state the output and describe what it does.
Count ← 0
Last ← Nums[1]
FOR i ← 2 TO 6
IF Nums[i] > Last THEN
Count ← Count + 1
ENDIF
Last ← Nums[i]
NEXT i
OUTPUT CountSales : ARRAY[1:4, 1:12] OF REAL holds the sales of 4 shops (rows) for 12 months (columns). Write pseudocode to output the total sales for each month, and then the number of the month with the highest total.
A theatre has 12 rows of 25 seats. The program records whether each seat is booked. (a) Choose a suitable data structure, justify your choice and write its declaration. (b) Write a statement to record that seat 17 in row 5 is booked.
This pseudocode should output the total of all 48 values in Sales, but the output is wrong. Identify the error, explain its effect and write the correction.
FOR Shop ← 1 TO 4
Total ← 0
FOR Month ← 1 TO 12
Total ← Total + Sales[Shop, Month]
NEXT Month
NEXT Shop
OUTPUT TotalAnswer key and marking notes
- (a) 0.
- (b) 23.
- (c) 23 − 0 + 1 = 24.
DECLARE Names : ARRAY[1:50] OF STRING.FOR Index ← 1 TO 50...NEXT Index.Names[Index] ← ""inside the loop.
- i = 2: 8 > 3, Count = 1, Last = 8; i = 3: 2 > 8 FALSE, Last = 2.
- i = 4: 8 > 2, Count = 2, Last = 8; i = 5: 5 > 8 FALSE, Last = 5; i = 6: 1 > 5 FALSE, Last = 1.
- Output: 2.
- It counts how many values are greater than the value immediately before them.
- Initialise
BestTotal ← -1(or the total of month 1) andBestMonth ← 1. - Outer loop
FOR Month ← 1 TO 12; setMonthTotal ← 0inside it. - Inner loop
FOR Shop ← 1 TO 4withMonthTotal ← MonthTotal + Sales[Shop, Month]. - After the inner loop:
OUTPUT Month, MonthTotal. IF MonthTotal > BestTotal THEN BestTotal ← MonthTotal, BestMonth ← Month.- After the outer loop:
OUTPUT BestMonth.
- (a) A 2D array, because each seat is identified by a row and a seat number.
- Of BOOLEAN, because each seat is either booked or not.
DECLARE Seats : ARRAY[1:12, 1:25] OF BOOLEAN.- (b)
Seats[5, 17] ← TRUE.
Total ← 0is inside the outer loop.- Total is reset for every shop, so only the total of shop 4 is output.
- Move
Total ← 0to beforeFOR Shop ← 1 TO 4.
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 does DECLARE Seats : ARRAY[1:6, 0:9] OF BOOLEAN create?
- A60
- B54
- C50
- D45
Which statement about an array in 9618 pseudocode is correct?
- AEvery element has the same data type, and the number of elements is fixed when it is declared
- BEach element can have a different data type, chosen when a value is stored
- CThe upper bound is the largest value currently stored in the array
- DThe array grows by one element each time a new value is assigned
Grid : ARRAY[1:3, 1:4] OF INTEGER holds the values below, indexed [row, column]. What is the value of Grid[2, 3]?
| Column 1 | Column 2 | Column 3 | Column 4 | |
|---|---|---|---|---|
| Row 1 | 6 | 2 | 9 | 4 |
| Row 2 | 3 | 8 | 5 | 7 |
| Row 3 | 1 | 4 | 6 | 2 |
- A5
- B4
- C8
- D6
Nums holds 4, 7, 1, 9, 3, 6 in elements 1 to 6. What is output?
Total ← 0
FOR i ← 1 TO 6
IF Nums[i] MOD 2 = 1 THEN
Total ← Total + Nums[i]
ENDIF
NEXT i
OUTPUT Total- A20
- B10
- C30
- D4
Grid holds the values below. What is output by the pseudocode?
| Column 1 | Column 2 | Column 3 | Column 4 | |
|---|---|---|---|---|
| Row 1 | 6 | 2 | 9 | 4 |
| Row 2 | 3 | 8 | 5 | 7 |
| Row 3 | 1 | 4 | 6 | 2 |
Best ← -1
BestCol ← 0
FOR Col ← 1 TO 4
ColTotal ← 0
FOR Row ← 1 TO 3
ColTotal ← ColTotal + Grid[Row, Col]
NEXT Row
IF ColTotal > Best THEN
Best ← ColTotal
BestCol ← Col
ENDIF
NEXT Col
OUTPUT BestCol, " ", Best- A
3 20 - B
2 23 - C
3 9 - D
4 13
Data is declared as ARRAY[1:10] OF INTEGER. This code is meant to shift every value one place towards the start. What goes wrong?
FOR i ← 1 TO 10
Data[i] ← Data[i + 1]
NEXT i- AOn the last pass it refers to Data[11], which is outside the bounds of the array
- BOn the first pass it refers to Data[0], which is outside the bounds of the array
- CEvery element ends up holding the value that was in Data[1]
- DThe values are shifted one place towards the end instead of the start
Quiz key
The first dimension has 6 − 1 + 1 = 6 indices and the second 9 − 0 + 1 = 10, so 6 × 10 = 60 elements. 54 uses 9 for the second dimension (upper minus lower without the + 1), 50 uses 5 for the first, and 45 makes the same slip in both.
An array is a fixed-length structure of elements that all share the declared data type. Mixed data types need a record. The bounds are indices (the first and last positions), not stored values, and an array does not grow: its size is set by its declaration.
Row 2 is 3, 8, 5, 7, and its third column holds 5. 4 is Grid[3, 2] (row and column swapped), 8 is Grid[2, 2], and 6 is Grid[3, 3].
The odd values are 7, 1, 9 and 3, and 7 + 1 + 9 + 3 = 20. 10 adds the even values (4 + 6), 30 adds every element, and 4 is how many odd values there are rather than their total.
The column totals are 10, 14, 20 and 13, so the largest is column 3 with 20. 2 23 is the largest row total (3 + 8 + 5 + 7), which comes from mixing up rows and columns. 3 9 picks the right column but reports its largest single element, and 4 13 is simply the last column processed.
When i = 10 the statement reads Data[11], but the upper bound is 10, so this is an out-of-range error; the loop should run to 9. Data[0] is never used, since i starts at 1. Each element correctly receives the next value along (no value is copied into every element), and copying from i + 1 to i moves values towards the start.
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.