Arrays and Records
What Is an Array and Why Use One
An array is a data structure that stores multiple values of the same data type under a single identifier. Each value is accessed by its index — an integer indicating its position in the array.
Without an array, storing five test scores requires five separate variables:
score0 ← 72
score1 ← 85
score2 ← 63
score3 ← 91
score4 ← 78
Processing all five then requires five separate lines of code. With an array, a loop handles any number of elements:
scores ← [72, 85, 63, 91, 78]
FOR i ← 0 TO 4
OUTPUT scores[i]
NEXT i
| Property | Detail |
|---|---|
| Same data type | All elements must be the same type — an integer array cannot hold strings |
| Fixed size | The number of elements is determined when the array is declared |
| Zero-based indexing | The first element is at index 0; a 5-element array uses indices 0 to 4 |
| Random access | Any element can be read or written directly using its index |
In AQA pseudocode,
scores[0]accesses the first element,scores[4]the fifth. An array withnelements has valid indices 0 ton − 1. Accessing an index outside this range is an out-of-bounds error.
1D Arrays: Declaring and Accessing Elements
A one-dimensional (1D) array is a flat, ordered list of values.
Declaration and element access:
days ← ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
OUTPUT days[0] # Mon
OUTPUT days[3] # Thu
days[5] ← "Weekend" # overwrite "Sat"
OUTPUT days[5] # Weekend
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
print(days[0]) # Mon
days[5] = "Weekend"
print(days[5]) # Weekend
Worked example — Store five temperatures and find the one at a user-specified index:
temps ← [12.5, 15.0, 9.3, 18.7, 14.1]
OUTPUT "Enter an index (0–4): "
idx ← USERINPUT
OUTPUT "Temperature: " & STR(temps[idx])
Worked example — Store exam grades and count how many are 'A':
grades ← ["B", "A", "C", "A", "A", "B"]
count ← 0
FOR i ← 0 TO 5
IF grades[i] = "A" THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count # 3
Traversing a 1D Array
Traversal means visiting every element in an array in sequence, typically using a FOR loop where the loop variable serves as the index.
Worked example — Calculate the total and average of an array of scores:
scores ← [72, 85, 63, 91, 78]
total ← 0
FOR i ← 0 TO 4
total ← total + scores[i]
NEXT i
average ← total / 5
OUTPUT "Total: " & STR(total)
OUTPUT "Average: " & STR(average)
total is an accumulator — initialised to 0 before the loop, then incremented each iteration. This pattern appears in nearly every array-processing exam question.
Worked example — Find the maximum value in an array:
scores ← [72, 85, 63, 91, 78]
maximum ← scores[0] # start by assuming the first is largest
FOR i ← 1 TO 4 # compare the rest
IF scores[i] > maximum THEN
maximum ← scores[i]
ENDIF
NEXT i
OUTPUT maximum # 91
Start the maximum at scores[0] (not at 0 or a guessed value). Update whenever a larger value is found. The same pattern finds the minimum by reversing the comparison to <.
Always use
TO length − 1as the upper bound (or the last valid index explicitly). For a 5-element array this isTO 4. WritingTO 5reads index 5 — a position that does not exist.
2D Arrays
A two-dimensional (2D) array is a grid of values organised into rows and columns. Two indices are required: array[row][column].
grid ← [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
OUTPUT grid[0][0] # 1 (row 0, column 0)
OUTPUT grid[1][2] # 6 (row 1, column 2)
OUTPUT grid[2][0] # 7 (row 2, column 0)
Worked example — Store and display marks for 3 students across 2 papers:
marks ← [[72, 85],
[63, 91],
[78, 80]]
FOR row ← 0 TO 2
FOR col ← 0 TO 1
OUTPUT marks[row][col]
NEXT col
NEXT row
Output order: 72, 85, 63, 91, 78, 80. The inner loop completes one full pass for each iteration of the outer loop — a 3×2 grid produces 6 outputs.
Worked example — Calculate the total marks for each student (row totals):
FOR row ← 0 TO 2
rowTotal ← 0
FOR col ← 0 TO 1
rowTotal ← rowTotal + marks[row][col]
NEXT col
OUTPUT "Student " & STR(row + 1) & " total: " & STR(rowTotal)
NEXT row
Use a 2D array whenever data forms a natural grid: a seating plan, a game board, a timetable, or exam marks across multiple subjects.
Something not quite clicking?
Ask Aica to explain any part of this differently. Free, takes 30 seconds.
Records
A record is a data structure that groups related values which may have different data types under a single identifier. Each value is a field, accessed by name rather than index.
RECORD Student
name : STRING
age : INTEGER
grade : CHARACTER
score : REAL
ENDRECORD
pupil1.name ← "Alice"
pupil1.age ← 16
pupil1.grade ← 'A'
pupil1.score ← 87.5
OUTPUT pupil1.name & " achieved grade " & pupil1.grade
# Python represents records as dictionaries or objects
pupil1 = {"name": "Alice", "age": 16, "grade": "A", "score": 87.5}
print(pupil1["name"] + " achieved grade " + pupil1["grade"])
Array vs Record — key distinction:
| Feature | Array | Record |
|---|---|---|
| Element types | All the same type | Fields can be different types |
| Access method | By integer index: arr[i] | By field name: record.field |
| Represents | A collection of similar items | One entity with multiple attributes |
| Example | 30 student scores | One student's name, age, grade, score |
Arrays of records combine both: a 1D array where each element is a record. This is how a class register or product catalogue is naturally represented — a list of entities, each with multiple attributes of different types.
Swap Algorithm and Array Algorithms
Two algorithms appear repeatedly in array exam questions:
Swapping two elements — requires a temporary variable to avoid overwriting:
# Swap scores[0] and scores[1]
temp ← scores[0]
scores[0] ← scores[1]
scores[1] ← temp
Without temp, the first assignment scores[0] ← scores[1] overwrites the original scores[0] before it has been saved. The value is permanently lost.
Linear search — find the index of a target value:
target ← 91
found ← FALSE
FOR i ← 0 TO 4
IF scores[i] = target THEN
OUTPUT "Found at index " & STR(i)
found ← TRUE
ENDIF
NEXT i
IF NOT found THEN
OUTPUT "Value not in array"
ENDIF
The found flag is initialised to FALSE before the loop and set to TRUE when the target is located. Checking found after the loop handles the case where the value is absent. (For a full comparison of linear and binary search, see the Searching Algorithms lesson.)
The swap, find-maximum, accumulate-total, and linear-search patterns cover the vast majority of array manipulation marks in AQA exam questions. Practise writing each one from memory.
Common Exam Mistakes
1. Using 1-based instead of 0-based indexing
AQA pseudocode uses 0-based indexing. A 5-element array has valid indices 0–4. Writing FOR i ← 1 TO 5 misses index 0 and attempts to access index 5, which does not exist.
2. Forgetting the temporary variable in a swap
scores[0] ← scores[1] then scores[1] ← scores[0] copies the same value into both elements. scores[0]'s original value is gone. Always save to temp first.
3. Initialising the maximum to 0
maximum ← 0 works only when all array values are positive. If any value could be negative or zero, the maximum is initialised incorrectly. Always set maximum ← array[0] and begin comparisons from index 1.
4. Confusing arrays and records
An array holds many values of the same type; a record holds fields of different types for one entity. Storing a student's name (string), age (integer), and score (real) together requires a record, not an array.
5. Out-of-bounds access in traversal
For a 5-element array (indices 0–4), FOR i ← 0 TO 4 is correct. FOR i ← 0 TO 5 or FOR i ← 0 TO LEN(scores) without subtracting 1 reads one position past the end of the array.
Generate revision on any topic you study
Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set — personalised to your level.
Lessons on anything
Structured, level-matched lessons on any topic you study
Practice quizzes
Find out what you actually know before the exam does
Flashcard sets
Lock in key concepts with instant revision cards
Ask Aica
Stuck on something? Get a clear explanation, any time
Selection and Iteration
Subroutines: Procedures and Functions
Related lessons
7 Slides
7 Slides
7 Slides