0984

Arrays

Programming · 4 question types

Exam Frequency Analysis

Past paper frequency (2018 to 2024)

This topic accounts for approximately 5% of your exam marks.

stable
Rare
Stable5%

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.

Declaring a 2D array

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.

Reading and writing 2D elements

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.

Exam tip

Accessing a 2D array: getting the index order right

What comes up: questions ask you to correct or extend code that reads from or writes to a 2D array, or award a mark for using the correct element to access a named column of data.

Write (two marks): (1) use two indices separated by a comma: Array[Row, Column] — the first index selects the row, the second selects the column; (2) for nested traversal, the outer FOR loop steps through rows and the inner FOR loop steps through columns.

Watch out: a recurring error in past papers is using the wrong column index — for example, inputting data into column 2 when the mark scheme credits column 1 for that piece of data. Always check what each column stores, as defined in the question, and match your index to the right column. If the question uses a coordinate-style or Python-style notation, follow the convention shown in the question itself rather than assuming [Row, Column] order.

A 2D array as a worked table

Visualise the array above as a 3 × 3 grid:

Col 1Col 2Col 3
Row 1123
Row 2456
Row 3789

Grid[2, 3] is the element at row 2, column 3 (the value 6).

Convention: [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.

Whatever the indexing base, the same principle holds: the first index selects the row and the second index selects the column, as shown below.

A 2D array shown as a grid with three rows indexed 0 to 2 down the left side and five columns indexed 0 to 4 across the top; an annotation shows that the left index determines the row and the right index determines the column
Source: Arrays by Save My Exams