Use arrays to store multiple values of the same data type

Arrays

Learning Objective

Use arrays to store multiple values of the same data type, initialise them, and manipulate the data with count‑controlled FOR … NEXT loops.

What Is an Array?

  • A single named collection of variables that all hold the same data type.
  • Each element is identified by an index (or a pair of indices for 2‑D arrays).
  • All elements must be of the same data type (INTEGER, REAL, STRING, etc.).

Why Use Arrays?

  • Compact storage of many related values – you do not need a separate variable for each value.
  • Easy processing with count‑controlled loops (e.g. FOR … NEXT).
  • Reduces the chance of transcription errors that occur when many individual variables are used.

Identifier Rules (Cambridge 0478)

  • Identifiers must start with a capital letter.
  • Only letters and digits are allowed (no underscores or other symbols).
  • Identifiers are case‑insensitive, but the convention is to use capitalised names such as Scores, Temp, Grid.

Declaring Arrays (Cambridge 0478 syntax)

Dimension Formal declaration Example
1‑D DECLARE ArrayName : ARRAY[lower:upper] OF DataType DECLARE Scores : ARRAY[1:10] OF INTEGER
2‑D DECLARE ArrayName : ARRAY[l1:u1, l2:u2] OF DataType DECLARE Grid : ARRAY[0:4, 0:4] OF INTEGER

Lower‑bound and Upper‑bound

You may choose a lower bound of 0 or 1. Whichever bound you use must be stated explicitly in the declaration and must be respected when accessing elements. In exam answers you should always write the lower bound you have chosen – e.g. “using a lower bound of 1”.

Calculating the Size of an Array

The number of elements (the size) is given by:

Size ← Upper – Lower + 1

Example:

DECLARE Marks : ARRAY[1:5] OF INTEGER
DECLARE N : INTEGER
N ← 5 - 1 + 1      // N = 5 elements

Accessing Elements

Single‑dimensional: ArrayName[i]

Two‑dimensional: ArrayName[i, j]

thirdScore ← Scores[3]          // lower bound = 1
centreValue ← Grid[2, 2]        // middle of a 5×5 grid (0‑based)

Initialising an Array

You can give the values when the array is declared, or assign them later.

DECLARE Marks : ARRAY[1:5] OF INTEGER ← {78, 85, 92, 67, 80}

or

DECLARE Marks : ARRAY[1:5] OF INTEGER
Marks[1] ← 78
Marks[2] ← 85
…

Iterating Through an Array

Count‑controlled loops use FOR … NEXT. The loop variable runs from the declared lower bound to the upper bound.

DECLARE i : INTEGER
FOR i ← 1 TO 5          // use the same bounds as the declaration
    // process Marks[i]
NEXT i

If the lower bound is 0, adjust the limits accordingly (e.g. FOR i ← 0 TO 4).

Nested Loops for 2‑D Arrays

Two loop variables are required – one for each dimension.

DECLARE row, col : INTEGER
FOR row ← 0 TO 2
    FOR col ← 0 TO 2
        // process Grid[row, col]
    NEXT col
NEXT row

Common Operations (Checklist)

  1. Declare the array with the correct bounds and data type.
  2. Initialise the array (either at declaration or with assignments/loops).
  3. Read a value: value ← Array[i].
  4. Write a value: Array[i] ← value.
  5. Iterate with a FOR … NEXT loop that matches the declared bounds.
  6. Calculate the size using Upper – Lower + 1 when required.

Worked Example – Size Calculation

DECLARE Temp : ARRAY[0:7] OF INTEGER   // 8 temperatures, lower bound = 0
DECLARE N : INTEGER
N ← 7 - 0 + 1          // N = 8
OUTPUT "Number of elements = ", N

Example 1 – Average of a Set of Marks (1‑D)

DECLARE Marks : ARRAY[1:5] OF INTEGER ← {78, 85, 92, 67, 80}
DECLARE Total, i : INTEGER
DECLARE Average : REAL

Total ← 0
FOR i ← 1 TO 5
    Total ← Total + Marks[i]
NEXT i

Average ← Total / 5.0
OUTPUT "Average = ", Average

Example 2 – Sum of a 3×3 Matrix (2‑D)

DECLARE Grid : ARRAY[0:2, 0:2] OF INTEGER
DECLARE row, col, Sum : INTEGER
Sum ← 0

FOR row ← 0 TO 2
    FOR col ← 0 TO 2
        INPUT Grid[row, col]
        Sum ← Sum + Grid[row, col]
    NEXT col
NEXT row

OUTPUT "Total of matrix = ", Sum

Array Bounds Checking and Common Errors

  • Out‑of‑bounds access: Using an index lower than the lower bound or greater than the upper bound (e.g. Scores[11] when the upper bound is 10) causes a run‑time error and loses marks.
  • Wrong loop limits: Forgetting to match the loop limits with the declared bounds, especially when the lower bound is 0.
  • Confusing the array name with an element: Writing Scores instead of Scores[i] inside a loop.
  • Data‑type mismatch: Assigning a REAL value to an INTEGER array element, or vice‑versa.
  • Not stating the lower bound: In exam answers you must explicitly state whether you are using 0‑based or 1‑based indexing.

Memory Note (brief)

In the Cambridge syllabus, an array is stored in contiguous memory locations. This is why the index must be an integer and why the size of the array is fixed at declaration.

Exam Tip – “Store the values in an array”

  1. Write the DECLARE statement using the exact Cambridge syntax, stating the lower and upper bounds you have chosen.
  2. Show any required initialisation – either a literal list or a short loop that reads the values.
  3. Include at least one line that accesses an element (e.g. Array[i]) and, if the question asks for processing, a concise FOR … NEXT loop.
  4. When you use a lower bound of 0, remember to mention it explicitly in your answer.

Suggested Diagram

Visual representation of a 1‑D array (indices 0‑4) and a 2‑D array (indices 0‑2,0‑2) with example values.

Practice Question

Task: Write pseudo‑code to read 8 integer temperatures into an array called Temp, then output the highest temperature.

DECLARE Temp : ARRAY[1:8] OF INTEGER
DECLARE i, MaxTemp : INTEGER

MaxTemp ← -9999               // initialise with a value lower than any possible temperature

FOR i ← 1 TO 8
    INPUT Temp[i]
    IF Temp[i] > MaxTemp THEN
        MaxTemp ← Temp[i]
    END IF
NEXT i

OUTPUT "Highest temperature = ", MaxTemp

Create an account or Login to take a Quiz

34 views
0 improvement suggestions

Log in to suggest improvements to this note.