Arrays: 1D and 2D
An array is a data structure that stores multiple values of the same data type under a single variable name. Each value is accessed using an index (position number). OCR J277 Paper 2 tests array creation, access, iteration and 2D arrays — often alongside FOR loops.
1D arrays (one-dimensional)
A 1D array is like a list: a single row of elements.
Declaration and initialisation (OCR pseudocode)
// Declare an array of 5 integers
scores = [10, 25, 8, 42, 17]
Indexing
OCR arrays are zero-indexed: the first element is at index 0.
scores[0] // 10 (first element)
scores[1] // 25 (second element)
scores[4] // 17 (last element — index = length - 1)
Reading and writing
// Read a value
OUTPUT scores[2] // outputs 8
// Write a value (update element at index 3)
scores[3] = 99
// scores is now [10, 25, 8, 99, 17]
Iterating through an array with a FOR loop
total = 0
FOR i = 0 TO 4
total = total + scores[i]
END FOR
OUTPUT total // outputs 159
Or using LEN() to get the length automatically:
FOR i = 0 TO LEN(scores) - 1
OUTPUT scores[i]
END FOR
Finding the largest value in an array
largest = scores[0]
FOR i = 1 TO LEN(scores) - 1
IF scores[i] > largest THEN
largest = scores[i]
END IF
END FOR
OUTPUT largest
2D arrays (two-dimensional)
A 2D array is a grid (table) of values — rows and columns. Useful for grids, matrices, game boards, spreadsheets.
Declaration (OCR pseudocode)
// 3 rows, 4 columns
grid = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]]
Indexing: grid[row][column]
grid[0][0] // 1 (row 0, column 0)
grid[1][2] // 7 (row 1, column 2)
grid[2][3] // 12 (row 2, column 3)
Iterating through a 2D array (nested FOR loops)
FOR row = 0 TO 2
FOR col = 0 TO 3
OUTPUT grid[row][col]
END FOR
END FOR
Real-world use: seating plan
// seats[row][column] stores student name or "" if empty
seats = [["Alice", "Bob", ""],
["", "Charlie", "Daisy"],
["Eve", "", "Frank"]]
OUTPUT seats[1][1] // Charlie
Why use arrays?
- Store many values of the same type without needing separate variable names (no need for score1, score2, score3...).
- Easy to iterate over using loops.
- Random access by index — O(1) — very fast.
Common OCR exam mistakes
- Using index 1 for the first element — OCR arrays are zero-indexed (first = index 0).
- Forgetting
LEN(array) - 1as the last index — iterating toLEN(array)goes one step too far. - Using the wrong order for 2D arrays — always
grid[row][column], notgrid[column][row]. - Trying to store different data types in one array — arrays hold one data type only.
AI-generated · claude-opus-4-7 · v3-ocr-computer-science