🎯 Syllabus & Goals 3 min
Cambridge 8.2 · Arrays Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Declare a one-dimensional and a two-dimensional array in pseudocode, and create the equivalent list in Python.
- Use an index (or a row and column index) to read and write a single element.
- Use a loop — or nested loops for 2-D — to write data into an array and read it back out.
Textbook: Chapter 8, §8.2 (pp. 329–333)
Recap / Warm-Up 5 min
Last lesson: scope, library routines and maintainable code. Every variable so far has held one value. Today: one name for many values.
Quick starter
You need to store 30 students' marks, then output those above the class average. Why is 30 separate variables (Mark1, Mark2, …) a bad idea?
Reveal the answer
You would need 30 declarations and 30 input lines, and you could not use a loop to process them. An array stores all 30 under one name, and a loop can visit each one using its index.
🧠 Key Concept 14 min
1 · One-dimensional arrays (a list)
A 1-D array is like a single row of numbered lockers. The first index can be 0 or 1 — most languages, including Python, start at 0.
Temperatures[3] is the fourth element, because counting starts at 0.A declaration gives the name, the first index, the last index and the data type:
Cambridge pseudocode
DECLARE Temperatures : ARRAY[0:6] OF REAL Temperatures[3] ← 25 OUTPUT Temperatures[3] FOR Day ← 0 TO 6 OUTPUT "Enter temperature: " INPUT Temperatures[Day] NEXT Day
The same in Python
temperatures = [0.0] * 7 # 7 elements, all 0.0 temperatures[3] = 25 print(temperatures[3]) for day in range(0, 7): temperatures[day] = float(input("Enter temperature: ")) # or fill it when it is created: temperatures = [21, 23, 19, 25, 24, 18, 22]
2 · Two-dimensional arrays (a table)
A 2-D array has rows and columns. Each element needs two indexes: [row, column].
Cambridge pseudocode
DECLARE Scores : ARRAY[0:2, 0:3] OF INTEGER
FOR Student ← 0 TO 2
FOR Test ← 0 TO 3
OUTPUT "Enter score: "
INPUT Scores[Student, Test]
NEXT Test
NEXT Student
OUTPUT Scores[1, 2]The same in Python
# Python has no true arrays: a 2-D array is a list of lists scores = [[12, 15, 18, 11], # row 0 [20, 17, 19, 16], # row 1 [9, 14, 10, 13]] # row 2 print(scores[1][2])
19
Worked Example 12 min
Example 1 · 1-D array — which days were above average?
Input 7 daily temperatures, output the average to 1 decimal place, then list every day above the average. The data must be kept, because it is used twice — this is why an array is needed.
Cambridge pseudocode
CONSTANT Days ← 7
DECLARE Temperatures : ARRAY[0:6] OF REAL
DECLARE Day : INTEGER
DECLARE Total, Average : REAL
// write to the array
FOR Day ← 0 TO Days - 1
OUTPUT "Temperature for day ", Day, ": "
INPUT Temperatures[Day]
NEXT Day
// read from the array to total it
Total ← 0
FOR Day ← 0 TO Days - 1
Total ← Total + Temperatures[Day]
NEXT Day
Average ← Total / Days
OUTPUT "Average: ", ROUND(Average, 1)
// read it again to compare
FOR Day ← 0 TO Days - 1
IF Temperatures[Day] > Average
THEN
OUTPUT "Day ", Day, " was above average at ", Temperatures[Day]
ENDIF
NEXT DayThe same in Python
# Daily high temperatures for one week, stored in a list (array) DAYS = 7 temperatures = [0.0] * DAYS # 7 elements, indexes 0 to 6 for day in range(0, DAYS): # writing to the array temperatures[day] = float(input("Temperature for day " + str(day) + ": ")) total = 0 for day in range(0, DAYS): # reading from the array total = total + temperatures[day] average = total / DAYS print("Average:", round(average, 1)) for day in range(0, DAYS): # reading it again if temperatures[day] > average: print("Day", day, "was above average at", temperatures[day])
Temperature for day 0: 21 Temperature for day 1: 23 Temperature for day 2: 19 Temperature for day 3: 25 Temperature for day 4: 24 Temperature for day 5: 18 Temperature for day 6: 22 Average: 21.7 Day 1 was above average at 23.0 Day 3 was above average at 25.0 Day 4 was above average at 24.0 Day 6 was above average at 22.0
- First loop:
Daygoes 0 → 6, filling one element per pass.writing to an array with iteration. - Second loop totals the elements: 21 + 23 + 19 + 25 + 24 + 18 + 22 = 152; 152 ÷ 7 = 21.714… → 21.7.reading from an array with iteration.
- Third loop compares each element with 21.714…: days 1, 3, 4 and 6 are above it.without the array, the first six values would already be lost.
Example 2 · 2-D array — row totals with nested loops
The Scores array above already holds 3 students × 4 tests. Output each student's total and average.
Cambridge pseudocode
CONSTANT Students ← 3
CONSTANT Tests ← 4
DECLARE Student, Test, StudentTotal : INTEGER
FOR Student ← 0 TO Students - 1
StudentTotal ← 0
FOR Test ← 0 TO Tests - 1
StudentTotal ← StudentTotal + Scores[Student, Test]
NEXT Test
OUTPUT "Student ", Student, " total: ", StudentTotal, " average: ", StudentTotal / Tests
NEXT Student
OUTPUT "Student 1, test 2 scored ", Scores[1, 2]The same in Python
# Test scores: 3 students (rows) x 4 tests (columns) scores = [[12, 15, 18, 11], [20, 17, 19, 16], [9, 14, 10, 13]] STUDENTS = 3 TESTS = 4 for student in range(0, STUDENTS): student_total = 0 for test in range(0, TESTS): student_total = student_total + scores[student][test] print("Student", student, "total:", student_total, "average:", student_total / TESTS) print("Student 1, test 2 scored", scores[1][2])
Student 0 total: 56 average: 14.0 Student 1 total: 72 average: 18.0 Student 2 total: 46 average: 11.5 Student 1, test 2 scored 19
| Student | Test | Scores[Student, Test] | StudentTotal | OUTPUT |
|---|---|---|---|---|
| 0 | 0 | |||
| 0 | 12 | 12 | ||
| 1 | 15 | 27 | ||
| 2 | 18 | 45 | ||
| 3 | 11 | 56 | Student 0 total: 56 average: 14 | |
| 1 | 0 | |||
| 0 … 3 | 20, 17, 19, 16 | 72 | Student 1 total: 72 average: 18 |
The outer loop picks a row; the inner loop walks along that row's columns. To total a column (one test for all students), swap which loop is outside.
Try It Yourself 12 min
Goal: Declare MyList : ARRAY[0:9] OF INTEGER, fill it with a FOR loop from the keyboard, then output the element at index 4. Do the same in Python.
Goal: Using the Temperatures array, output the highest temperature and the day it happened. (Start with Highest ← Temperatures[0].)
Goal: A 2-D array Names : ARRAY[1:10, 1:2] OF STRING holds a first name (column 1) and a family name (column 2) for 10 people. Write pseudocode to output each full name, then sort the rows into family-name order before outputting them.
Hint
Outputting needs one loop over the rows. For sorting, adapt the Unit 7 bubble sort: compare Names[Index, 2] with Names[Index + 1, 2], and when they are out of order swap both columns using a temporary variable.
📝 Exam Practice 10 min
Define the terms array and index.
Mark scheme
- Array: a data structure holding several elements of the same data type under one identifier (1).
- Index: the number that identifies the position of an element in the array (1).
Write the pseudocode to declare a 2-D array Seats of BOOLEAN values with 12 rows and 20 columns, using 1 as the first index for both.
Mark scheme
DECLARE Seats : ARRAY[1:12, 1:20]— correct bounds, rows then columns (1)OF BOOLEAN(1)
The array Price : ARRAY[1:50] OF REAL holds the prices of 50 items. Write pseudocode to output how many prices are over $20 and the total of all 50 prices.
Mark scheme
- Count and total initialised to 0 (1).
FOR Index ← 1 TO 50…NEXT Index(1).Total ← Total + Price[Index](1).IF Price[Index] > 20 THEN Count ← Count + 1(1).- Both outputs after the loop, with messages (1).
Explain why nested loops are used to fill a two-dimensional array.
Mark scheme
- Each element needs two indexes — a row and a column (1)…
- …so one loop moves through the rows and the inner loop moves through every column of each row (1).
🗝️ Recap & Key Terms 3 min
An array stores many values of one type under one name. A 1-D array uses one index; a 2-D array uses [row, column]. A loop counter used as the index lets one statement process every element — nested loops for 2-D. Python uses lists (and lists of lists) and always starts at 0.
- Array
- A data structure containing several elements of the same data type, accessed using the same identifier name.
- Element
- One individual item stored in an array.
- Index
- Identifies the position of an element in an array.
- One-dimensional array
- An array that can be thought of as a list; each element has one index.
- Two-dimensional array
- An array that can be thought of as a table of rows and columns; each element has a row index and a column index.
Homework 1 min
Task (≤ 15 min): Write pseudocode to input 300 names into the array Name[1:300], then input a name to search for and output its position, or "Not found". [5]
Model answer
DECLARE Name : ARRAY[1:300] OF STRING
DECLARE Index, Position : INTEGER
DECLARE Target : STRING
FOR Index ← 1 TO 300
OUTPUT "Enter name ", Index, ": "
INPUT Name[Index]
NEXT Index
OUTPUT "Name to find: "
INPUT Target
Position ← 0
Index ← 1
WHILE Position = 0 AND Index <= 300 DO
IF Name[Index] = Target
THEN
Position ← Index
ENDIF
Index ← Index + 1
ENDWHILE
IF Position > 0
THEN
OUTPUT Target, " is at position ", Position
ELSE
OUTPUT "Not found"
ENDIFMarks: FOR loop inputs into the array (1); search target input (1); loop comparing each element (1); stops when found or at the end (1); correct outputs (1).