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.

Read an element using its index

To read the value at a given position, use the array name followed by the index in square brackets:

OUTPUT Scores[3]    // outputs 5 (the third element)

The expression Scores[3] is just a single value (an integer in this case). It can be used anywhere a normal integer can:

TotalSoFar ← Scores[1] + Scores[2]
IF Scores[4] > 0
  THEN
     OUTPUT "Has a positive score"
ENDIF

Write a new value into an element

To change the value at a given position, assign with :

Scores[2] ← 100    // changes the second element from 10 to 100
// Scores is now: [12, 100, 5, 2, 8]

The other elements are untouched.

What happens if the index is out of range?

If the index is 0 or 6 (in our 5-element example), the access is invalid and the program will report an error or behave unpredictably. Always make sure indices stay within 1 ... LENGTH(Array).