Programming · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 5% of your exam marks.
1D array manipulation with FOR loops appears regularly in Paper 2. 4 to 6 marks.
A 2-dimensional array (2D array) is a table of values with rows and columns. Each element is accessed using two indices: one for the row, one for the column.
A 2D array is the natural way to store anything that fits a grid: a noughts-and-crosses board, a school timetable, a spreadsheet, the pixels of a small image.
DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER
This creates a 3 × 3 grid: 3 rows (indices 1 to 3) and 3 columns (indices 1 to 3), 9 elements in total, each an integer.
Access an element with two indices separated by a comma:
Grid[1, 1] ← 1 // top-left
Grid[1, 2] ← 2
Grid[1, 3] ← 3
Grid[2, 1] ← 4
Grid[2, 2] ← 5
Grid[2, 3] ← 6
Grid[3, 1] ← 7
Grid[3, 2] ← 8
Grid[3, 3] ← 9 // bottom-right
OUTPUT Grid[1, 1] // outputs 1
OUTPUT Grid[2, 3] // outputs 6
The first index is the row; the second is the column. CIE conventions are 1-indexed for both.
Visualise the array above as a 3 × 3 grid:
| Col 1 | Col 2 | Col 3 | |
|---|---|---|---|
| Row 1 | 1 | 2 | 3 |
| Row 2 | 4 | 5 | 6 |
| Row 3 | 7 | 8 | 9 |
Grid[2, 3] is the element at row 2, column 3 (the value 6).
[Row, Column] or [X, Y]?The CIE convention is Array[Row, Column]. Some questions write Array[Row][Column] (Python style) or use coordinate-style Array[X, Y] where X is column and Y is row. Always check the example in the question to see which order is being used; do not assume.
![A 2D array drawn as a 3 by 3 grid of labelled cells: each cell shows its row-column index and value, with the declaration DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER shown beneath](/revision-notes/2d-array-grid.webp)