Spec & Goals 3 min
AQA Spec 3.2.6 · Data structures (arrays)
By the end of this lesson you can:
- Describe an array as an indexed collection of items of the same data type.
- Access an element by its index, remembering the index starts at 0.
- Use a FOR loop to iterate over a 1-D array, and a nested loop over a 2-D array.
Warm-Up 5 min
Last lesson you used relational and Boolean operators to compare values inside conditions.
Quick starter
Aisyah needs to store the test scores of 30 pupils. Why is one variable per pupil a bad idea?
Reveal the answer
Thirty separate variables means thirty names to track and repeated code. One array holds all 30 scores under a single name, and a loop can process every score.
Key Concept — arrays and indexing 14 min
An array is an indexed collection of items that all share the same data type. Each item is an element.
The index
Each element has a position number called its index. In AQA pseudo-code the index starts at 0.
If scores holds five marks, the boxes are numbered like this:
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Element | 72 | 58 | 90 | 65 | 81 |
So scores[0] is 72 and scores[2] is 90.
Iterating with a FOR loop
To visit every element, count the index from 0 to the last position with a FOR loop.
Two-dimensional arrays
A 2-D array is a table of rows and columns. You give two indices: grid[row, col].
Both indices start at 0, so grid[0, 0] is the top-left element. You iterate a 2-D array with a nested FOR loop — one for the rows, one for the columns.
Worked Example — total and average of test scores 12 min
Problem: a 1-D array scores holds five marks. Output their total and average.
AQA pseudo-code
scores ← [72, 58, 90, 65, 81]
total ← 0
FOR i ← 0 TO 4
total ← total + scores[i]
ENDFOR
average ← total / 5
OUTPUT total
OUTPUT averageThe loop runs from index 0 to 4 — the five valid positions. Each pass adds one element to total.
The same algorithm in Python
scores = [72, 58, 90, 65, 81] total = 0 for i in range(5): total = total + scores[i] average = total / 5 print(total) print(average)
A 2-D access
Now suppose marks are stored as a table — one row per pupil, one column per subject:
marks ← [[72, 58], [90, 65], [81, 77]] OUTPUT marks[1, 0]
Row 1, column 0 is 90 — the second pupil's first subject. Remember both indices start at 0.
Try It Yourself 12 min
Goal: The array temps ← [31, 33, 30, 34] holds the daily high in Kuala Lumpur. State the value of temps[1] and temps[3].
Hint: count from 0, not 1.
Goal: Write AQA pseudo-code that uses a FOR loop to OUTPUT every element of a 6-item array called prices.
Hint: loop the index from 0 to 5 and output prices[i].
Goal: A 2-D array seats[row, col] stores a cinema booking grid. Write pseudo-code using a nested FOR loop that outputs every seat in a 3-row, 4-column grid.
📝 Exam Practice 10 min
Answer the way the examiner expects — the command word and the marks tell you how much to write.
An array is declared as scores ← [72, 58, 90, 65, 81]. State the value stored at scores[2].
Mark scheme
90(1) — index 2 is the third element, because indexing starts at 0.
Complete the loop so it totals every element of the 5-item array scores.
total ← 0
FOR i ← 0 TO ____
total ← ____
ENDFORMark scheme
- Upper bound is
4(1). - Body is
total ← total + scores[i](1).
A 2-D array grid[row, col] stores data in rows and columns. Give the index used to access row 1, column 2.
Mark scheme
grid[1, 2](1).
An array stores n items. State the index of the last element.
Mark scheme
n − 1(1) — because the first index is 0.
Recap & Key Terms 3 min
An array holds many elements of the same type under one name. You reach an element by its index, which starts at 0, and iterate with a FOR loop. A 2-D array is a table reached with two indices and iterated with a nested loop.
- Array
- An ordered collection of elements of the same data type, stored under one name.
- Index
- The position number of an element; the first index is 0.
- Element
- A single item stored in an array.
- One-dimensional
- An array of a single list of elements, reached with one index, e.g.
scores[2]. - Two-dimensional
- An array of rows and columns, reached with two indices, e.g.
grid[row, col].
Homework 1 min
Task (≤ 15 min): The array rainfall ← [12, 30, 8, 25] holds four readings. Write AQA pseudo-code that finds and outputs their total using a FOR loop.
Model answer
rainfall ← [12, 30, 8, 25]
total ← 0
FOR i ← 0 TO 3
total ← total + rainfall[i]
ENDFOR
OUTPUT totalAward marks for: initialise total to 0 (1), loop the range 0 to 3 (1), add rainfall[i] then output the total (1).