0984

Arrays

Programming · 4 question types

A 1-dimensional array (1D array) is a single row of elements, like a list.

In CIE pseudocode an array is declared with DECLARE, an ARRAY[start:end] size, and a data type:

DECLARE Scores : ARRAY[1:5] OF INTEGER
DECLARE Names  : ARRAY[1:10] OF STRING
DECLARE Flags  : ARRAY[1:3] OF BOOLEAN
  • Scores : ARRAY[1:5] OF INTEGER creates an array called Scores with 5 (indices 1, 2, 3, 4 and 5), each holding an integer.
  • Names : ARRAY[1:10] OF STRING creates 10 string slots.

After declaration, the array exists but the elements have no useful values yet; they need to be assigned.

1-indexed vs 0-indexed

CIE pseudocode normally uses 1 as the lower bound: the first element is at position 1, the last element at position n (where n is the size). Both syllabuses allow the first index to be zero or one, so read the bounds in the declaration: an array declared with ARRAY[0:4] also has five elements, at positions 0 to 4.

Python is 0-indexed: the first element is at position 0, the last at position n − 1.

CIE pseudocodePython
First element of an array of size 5Arr[1]arr[0]
Last element of an array of size 5Arr[5]arr[4]

Start your arrays at 1 in CIE pseudocode answers unless the question sets one up from 0. A lower bound of 0 is also allowed, but every index and loop must then stay within the bounds you declared.

The diagram below shows the anatomy of a 0-indexed array of length 5: the first element sits at index 0 and the last at index 4, so an array of size n runs from index 0 to index n − 1.

A 1D array of length 5 holding the letters B, E, A, D, S in cells indexed 0 to 4 across the top; annotations point to the first index (0), the first element (B), the element at index 3 (the 4th element, D), and a double-headed arrow labelled array length is 5
Source: Arrays by Save My Exams
Exam tip

Writing an array declaration in full

Write each declaration in full, such as DECLARE Scores : ARRAY[1:30] OF INTEGER (a 2D array has two pairs of bounds, ARRAY[1:8, 1:12]). One error-correction question declared a 2D array of city and country names with no colon and as BOOLEAN; the corrected line restores the colon and makes the type STRING. Two printed algorithms use shortened forms such as DECLARE A[1:50] : INTEGER; neither is the syllabus form, so do not copy them.

Every element shares one type, so a scenario keeps its names in one array and the numbers it calculates with in another, linked by index.

Initialising an array with values

In CIE pseudocode, array elements are assigned one at a time, each by its index. There is no whole-array literal: you cannot write Scores ← [12, 10, 5, 2, 8] in CIE pseudocode.

DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 12
Scores[2] ← 10
Scores[3] ← 5
Scores[4] ← 2
Scores[5] ← 8

After this, Scores contains the five values 12, 10, 5, 2, 8. When the values follow a pattern, a FOR loop can assign the elements instead of writing each line out by hand.