CIE IGCSE | 8.2.1.1 Declare and Use One-Dimensional Arrays

Lesson objective

Declare one-dimensional arrays with explicit bounds and types, read and update individual elements, and distinguish indices from stored values.

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

8

INDEX 2

5

INDEX 3

12

INDEX 4

7

INDEX 5

9

Scores[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
One declaration describes the collection and its elements
PartMeaning
ScoresThe array identifier
ARRAYDeclares an array structure
[1:5]Permitted indices from 1 to 5 inclusive
OF INTEGEREach 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.

Length = upper bound - lower bound + 1
BoundsValid indicesNumber of elements
[1:5]1, 2, 3, 4, 55
[0:4]0, 1, 2, 3, 45
[3:7]3, 4, 5, 6, 75

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]
Assignments replace the value at the selected index
MomentScores[2]Other elements
Before update58, 12, 7, 9 unchanged
After replacement10Unchanged
After adding 313Unchanged

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"
ENDIF

11 | 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.

MATCH THE ARRAY NOTATION TO ITS ROLE

Terminology

Terminology

Array

A fixed-length collection of elements with one declared data type and consecutive indices.

One-dimensional array

An array using one index to select an element.

Element

One storage position in an array.

Index

The number selecting an element’s position.

Element value

The data currently stored in an array element.

Lower bound

The first permitted index.

Upper bound

The last permitted index.

Array length

The number of elements: upper bound minus lower bound plus one.

Element type

The data type shared by the array’s elements.

Declaration

Names an array and specifies its bounds and element type.

Initialisation

Assigning starting values before elements are read.

Square brackets

The notation surrounding array bounds or indices in Cambridge pseudocode.

Variable index

An index supplied by the current value of an INTEGER variable.

Index expression

An expression evaluated to an INTEGER selecting an element.

Out-of-bounds access

Reading or writing with an index outside the declared limits.

Element assignment

Storing a value in a selected array element.

Value copy

Assigning one element’s current value to another without linking later changes.

Traversal

Visiting array elements in a planned order, often using a loop.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which describe a Cambridge 1D array?
2. Scores[3] holds 12. Which statements are correct?
3. DECLARE Scores : ARRAY[1:5] OF INTEGER establishes which?
4. How many elements does ARRAY[3:7] contain?
5. Which accesses are valid for ARRAY[1:5]?
6. PlayerNumber is four and Scores[4] is seven. What happens with SelectedScore ← Scores[PlayerNumber]?
7. Scores[2] starts at five. Scores[2] ← 10 then Scores[2] ← Scores[2] + 3. Which apply?
8. Which are necessary before reading an element?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Define a one-dimensional array and an element.

2. Explain the difference between index and value using Scores[3] = 12.

3. Write a declaration for ten INTEGER scores indexed one through ten.

4. Calculate the element counts for [0:4] and [3:7].

5. Trace Scores[2] ← 5 followed by Scores[3] ← Scores[2], then Scores[2] ← 9.

6. Explain what Scores[Position + 1] selects when Position is two, and why Position five is a problem for [1:5].

7. Explain why a bounds check alone does not guarantee a meaningful read.

8. Suggest one reason to use an array rather than separately named score variables.

BUILD IT | FOUR DAILY TEMPERATURES

Declare a REAL array Temperatures indexed one to four. Store 12.5, 14.0, 11.5 and 13.0. Use INTEGER Day equal to three to output the selected temperature. Then increase that element by 1.0 and output it again. Predict the two outputs and explain which other elements change.

Reveal one possible solution
DECLARE Temperatures : ARRAY[1:4] OF REAL
DECLARE Day : INTEGER
Temperatures[1] ← 12.5
Temperatures[2] ← 14.0
Temperatures[3] ← 11.5
Temperatures[4] ← 13.0
Day ← 3
OUTPUT Temperatures[Day]
Temperatures[Day] ← Temperatures[Day] + 1.0
OUTPUT Temperatures[Day]

The outputs are 11.5 and 12.5. Only index three changes; Day remains three and the array still contains four elements. Day must be within one to four. Extend the trace by copying Temperatures[Day] into another REAL variable, then changing the element again: the copied value does not automatically follow the update.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 8.2.1.1 Declare and Use One-Dimensional Arrays is coming soon. Complete the matching task and written questions in your exercise book.