Paper 2 · Arrays
Cambridge arrays start at 1
· 5 min read · For students
In Cambridge pseudocode the first slot of an array is index 1, unless the question prints different bounds. Scores[0] is not the first score. It is outside the array, and the program stops.
DECLARE Scores : ARRAY[1:3] OF INTEGER
Scores[1] <- 4
Scores[2] <- 7
Scores[3] <- 9
OUTPUT Scores[0]DECLARE Scores : ARRAY[1:3] OF INTEGER
DECLARE i : INTEGER
Scores[1] <- 4
Scores[2] <- 7
Scores[3] <- 9
FOR i <- 1 TO 3
OUTPUT Scores[i]
NEXT i4 7 9
Say both bounds in the DECLARE
The declaration is ARRAY[lower:upper] OF type. For three scores that is ARRAY[1:3] OF INTEGER. The lower bound is usually 1 in IGCSE questions. If a question says the array runs from 0, then 0 is correct for that question and nowhere else.
A 2D array needs two pairs of bounds: ARRAY[1:4, 1:3] OF INTEGER is four rows and three columns. The first cell is Grid[1, 1], not Grid[0, 0]. See 1D arrays and 2D arrays.
The loop stops on the last index
If the upper bound is 3, the loop is FOR i <- 1 TO 3. TO 4 reads one past the end and the program fails on the last pass, often after it has already printed the real values. That is the other off-by-one: the start is fine, the finish is one too far.
- Lower bound in the DECLARE, lower bound in the FOR. They match.
- Upper bound in the DECLARE, upper bound in the FOR. They match.
- Use the index when you print:
OUTPUT Scores[i], notOUTPUT Scores.
The declare an array lesson starts from an empty declaration. The scan lesson is the loop that walks every element once.
Check your own code
Run it in the compiler. A mistake is explained in plain English, and many of them have a one-click fix.