Learn
8.2.1.1 | DECLARE AND USE ONE-DIMENSIONAL ARRAYS
01 | FIVE SCORES NEED FIVE PLACES
A quiz records five scores. You could create ScoreOne, ScoreTwo and three more separate variables, but every instruction would have to name a particular score. How would the program choose the score belonging to a player number?
An array stores a fixed number of related values under one identifier. Each storage position is an element. In Cambridge pseudocode, the elements of an array all have the same declared data type.
A one-dimensional array, or 1D array, uses one index to select an element. Think of a numbered row of storage places: Scores is the whole array’s name, and Scores[3] selects one place within it.
02 | INDEX AND VALUE ARE DIFFERENT
An index identifies an element’s position. The value is the data stored at that position. In the example below, index three holds value twelve.
INDEX 1
8INDEX 2
5INDEX 3
12INDEX 4
7INDEX 5
9Scores[3] means “the value at index three”, which is twelve here. It does not mean “find the value three”, and it does not mean the third score must equal three.
Different elements may hold the same value. Index one and index four could both contain eight without becoming the same element. The index identifies the place, not a unique score value.
03 | DECLARE THE ARRAY WITH BOUNDS AND A TYPE
A declaration names the array, states its permitted indices and gives its elements’ data type. The lower bound is the first permitted index; the upper bound is the last.
The declaration below creates five INTEGER elements, indexed one through five inclusive. Square brackets surround the bounds, a colon separates the lower and upper bounds, and OF introduces the element type.
The declaration alone does not assign the five quiz scores or guarantee that every element contains zero. Provide values before reading an element.
DECLARE Scores : ARRAY[1:5] OF INTEGER
| Part | Meaning |
|---|---|
| Scores | The array identifier |
| ARRAY | Declares an array structure |
| [1:5] | Permitted indices from 1 to 5 inclusive |
| OF INTEGER | Each element holds an INTEGER |
04 | COUNT ELEMENTS FROM BOTH BOUNDS
The number of elements is upper bound minus lower bound plus one. Both endpoints count.
ARRAY[1:5] has five elements. ARRAY[0:4] also has five elements, but different indices. ARRAY[3:7] has five elements too; the upper bound seven is not its length.
Cambridge generally uses a lower bound of one, but the syllabus also allows a first index of zero. Always read the explicit declaration. Do not assume the first index must match a familiar programming language.
| Bounds | Valid indices | Number of elements |
|---|---|---|
| [1:5] | 1, 2, 3, 4, 5 | 5 |
| [0:4] | 0, 1, 2, 3, 4 | 5 |
| [3:7] | 3, 4, 5, 6, 7 | 5 |
05 | ASSIGN A VALUE TO ONE ELEMENT
Use the array name followed by one index in square brackets on the left of the assignment arrow. Scores[3] ← 12 stores twelve at index three.
These assignments initialise all five elements individually. After them, the array matches the earlier visual example. Changing one element does not automatically change the others.
The colon belongs to the declaration’s bounds. An individual access uses Scores[3], not Scores[1:5].
DECLARE Scores : ARRAY[1:5] OF INTEGER Scores[1] ← 8 Scores[2] ← 5 Scores[3] ← 12 Scores[4] ← 7 Scores[5] ← 9
06 | READ AN ELEMENT INTO AN EXPRESSION
An array element can be read wherever its data type suits the expression. OUTPUT Scores[3] displays twelve. ChosenScore ← Scores[3] copies twelve into a separate INTEGER variable.
Reading an element does not remove its value. Scores[3] still holds twelve after either instruction. The whole array identifier and one indexed element are different things: use the index when you mean a particular score.
Once values have been assigned, two elements can participate in one calculation. Scores[1] + Scores[2] gives eight plus five, which is thirteen.
DECLARE ChosenScore : INTEGER DECLARE PairTotal : INTEGER ChosenScore ← Scores[3] PairTotal ← Scores[1] + Scores[2] OUTPUT ChosenScore OUTPUT PairTotal
07 | REPLACE OR UPDATE A STORED VALUE
Scores[2] ← 10 replaces the previous five at index two. It does not append a sixth element or move any other score.
Scores[2] ← Scores[2] + 3 then reads the current ten, adds three and stores thirteen back in that same element. Read the right-hand side first, then apply the assignment.
An array declared with five elements remains a five-element structure in this Cambridge model. Storing a new value into an existing element is not extending the array.
Scores[2] ← 10 Scores[2] ← Scores[2] + 3 OUTPUT Scores[2]
| Moment | Scores[2] | Other elements |
|---|---|---|
| Before update | 5 | 8, 12, 7, 9 unchanged |
| After replacement | 10 | Unchanged |
| After adding 3 | 13 | Unchanged |
08 | A VARIABLE CAN CHOOSE THE INDEX
The index does not have to be a literal written in the code. A variable holding a valid INTEGER can choose an element at runtime.
PlayerNumber equal to four makes Scores[PlayerNumber] refer to Scores[4]. In the original example its value is seven. PlayerNumber holds the index four; it does not receive seven unless you explicitly assign an element value to it.
This is one reason an array is more flexible than five separately named variables. One instruction can select a score based on the supplied number.
DECLARE PlayerNumber : INTEGER DECLARE SelectedScore : INTEGER PlayerNumber ← 4 SelectedScore ← Scores[PlayerNumber] OUTPUT SelectedScore
09 | AN EXPRESSION CAN ALSO SELECT AN INDEX
An index expression must evaluate to an INTEGER within the declared bounds. If Position is two, Scores[Position + 1] selects index three.
The assignment below copies the value at index two into index three. With the original array, index three changes from twelve to five; index two stays five. This is a value copy, not a permanent link between the elements.
Position equal to five would make Position + 1 equal six, which is outside the bounds. Check the evaluated index, not just the variable appearing in the expression.
DECLARE Position : INTEGER Position ← 2 Scores[Position + 1] ← Scores[Position]
10 | STAY WITHIN THE DECLARED BOUNDS
For Scores : ARRAY[1:5], zero and six are invalid indices. They do not select a hidden score and do not create a new element. Out-of-bounds access is an error; the exact runtime response depends on the programming environment.
A bounds check should happen before reading or writing the selected element. Assume SelectedIndex has already received an INTEGER, then test both limits.
This guard checks the index. It does not ensure the selected element has already been initialised; valid position and assigned value are separate requirements.
IF (SelectedIndex >= 1) AND (SelectedIndex <= 5)
THEN
OUTPUT Scores[SelectedIndex]
ELSE
OUTPUT "Index must be from 1 to 5"
ENDIF11 | EXPLORE ELEMENT READS AND WRITES
The demo starts with the five assigned scores shown earlier. Choose an index and a new INTEGER value. Read shows the current value; write replaces only the selected element. Reset restores the original five scores.
Negative values are allowed in this INTEGER demonstration. A particular quiz might impose an additional score range; the element type alone does not enforce every application rule.
12 | USE THE ELEMENT TYPE CONSISTENTLY
All Scores elements have type INTEGER. StudentNames could be declared as an array of STRING, and ActivePlayers as an array of BOOLEAN. One array declaration supplies one element type, not a different type for each position.
A STRING element can contain a whole name, not just one character. StudentNames[1] below holds “Maya”. The index still selects the array element; it is not a character position inside that name.
Keep the collection name meaningful so a reader can understand what its elements represent.
DECLARE StudentNames : ARRAY[1:3] OF STRING DECLARE ActivePlayers : ARRAY[1:3] OF BOOLEAN StudentNames[1] ← "Maya" ActivePlayers[1] ← TRUE OUTPUT StudentNames[1]
13 | INPUT CAN WRITE DIRECTLY INTO AN ELEMENT
An array element can be an INPUT destination. The instruction below receives a value and stores it at index two, replacing any previous value there.
The same idea works with a valid variable index: INPUT Scores[PlayerNumber]. The input supplies a score value; PlayerNumber decides where it goes.
As with ordinary variables, INPUT does not automatically prove the supplied data is suitable. This example assumes a valid INTEGER score and an in-bounds INTEGER player number.
OUTPUT "Enter the score for player two" INPUT Scores[2] OUTPUT "Player two score: ", Scores[2]
14 | A LOOP CAN VISIT THE ARRAY IN ORDER
An array’s consecutive indices fit naturally with a FOR loop. Position takes one through five, so the same OUTPUT instruction reads each element in turn.
This example assumes all five scores have been initialised. The loop does not assign them merely by reading them. Its bounds match the declaration, so every accessed position is valid.
Later lessons develop reading and writing arrays using iteration. For now, notice that the changing counter selects an element rather than changing the array’s name.
DECLARE Position : INTEGER FOR Position ← 1 TO 5 OUTPUT "Index ", Position, " holds ", Scores[Position] NEXT Position
15 | CHECK THE PLACE, TYPE AND VALUE
Before using an array element, identify the array’s bounds and element type. Evaluate the index, check it is permitted, and make sure a value has been assigned before reading.
A useful explanation is: “PlayerNumber is four, therefore Scores[PlayerNumber] selects the element at index four. That element contains seven, so the output is seven.” This distinguishes position from content.
Common errors include treating the upper bound as the length, assuming declarations fill every element with zero, using parentheses instead of square brackets, or expecting one assignment to append another element.
The next lesson extends the idea to two-dimensional arrays, where two indices select one element.