Cambridge IGCSE · Topic 8.2
IGCSE Computer Science 0478 / Topic 8 / IGCSE

One name, many values.

Storing 30 test marks in 30 separate variables would be painful. An array keeps them all under one identifier, numbered by an index, so a single loop can input, total or search the lot.

The key idea

An array is a data structure that holds a fixed number of elements of the same data type under one identifier. Each element is reached by its index, written in square brackets: Scores[3]. Declare it with its lower and upper bounds: DECLARE Scores : ARRAY[1:5] OF INTEGER. The first index can be 0 or 1 (exam pseudocode generally uses 1). Because the index can be a variable, a FOR loop can visit every element in turn.

DECLARE Scores : ARRAY[1:5] OF INTEGER Scores 12345 12715911 index Scores[3] = 15 one identifier, five elements of the same data type, each reached by its index
Fig. 1: a one-dimensional array of five integers
Section 01

Declaring an array.

The declaration gives the identifier, the range of indexes and the data type of every element.

DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 12
Scores[2] ← 7
Scores[3] ← 15
Scores[4] ← 9
Scores[5] ← 11
OUTPUT Scores[3]

This outputs 15, the value in element 3.

The same idea in Python (for comparison only: the exam uses pseudocode)

scores = [12, 7, 15, 9, 11]    # a Python list; the first index is 0
print(scores[2])               # 15, the third element
Examiner trap

Using an index outside the bounds, such as Scores[6] or Scores[0] for ARRAY[1:5], is an error: that element does not exist.

Section 02

Arrays and loops.

In the lab, choose Maximum and minimum. Six scores are input into an array by one loop, and a second loop compares each element with the largest and smallest so far.

DECLARE Temps : ARRAY[1:7] OF REAL
DECLARE Day : INTEGER
DECLARE Total : REAL
FOR Day ← 1 TO 7
    INPUT Temps[Day]
NEXT Day
Total ← 0
FOR Day ← 1 TO 7
    Total ← Total + Temps[Day]
NEXT Day
OUTPUT "Average: ", Total / 7

The same idea in Python (for comparison only: the exam uses pseudocode)

temps = []
for day in range(7):
    temps.append(float(input()))
print("Average:", sum(temps) / 7)
Worked example Finding the largest
Scores holds 12, 7, 15, 9, 11. Trace this code and give the output.
Highest ← Scores[1], then FOR Index ← 2 TO 5: IF Scores[Index] > Highest THEN Highest ← Scores[Index]; after the loop, OUTPUT Highest.
  1. Highest starts as Scores[1] = 12.
  2. Index 2: 7 > 12? No. Index 3: 15 > 12? Yes, Highest ← 15.
  3. Index 4: 9 > 15? No. Index 5: 11 > 15? No.
Answer: 15
Examiner trap

Start Highest at the first element, not at 0: if every value were negative, a starting value of 0 would be wrongly output as the largest.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which statement about an array is correct?

Quick check+10 XP

How many elements are in DECLARE Marks : ARRAY[1:30] OF INTEGER?

Quick check+10 XP

In the lab's Maximum and minimum program with inputs 34, 71, 12, 58, 90, 45, what is output?

Quick check+10 XP

In a Python list, what is the index of the first element?

Section 03

Using the index.

The index can be any expression that gives an integer, so a program can search an array, or move values between neighbouring elements, such as Names[Count + 1].

DECLARE Found : BOOLEAN
DECLARE Index : INTEGER
DECLARE Target : INTEGER
INPUT Target
Found ← FALSE
Index ← 1
WHILE Found = FALSE AND Index <= 5 DO
    IF Scores[Index] = Target
      THEN
        Found ← TRUE
      ELSE
        Index ← Index + 1
    ENDIF
ENDWHILE
IF Found = TRUE
  THEN
    OUTPUT "Found at position ", Index
  ELSE
    OUTPUT "Not found"
ENDIF
Worked example Linear search
Scores holds 12, 7, 15, 9, 11 and the Target is 9. What is output?
  1. Index 1: 12 is not 9, Index ← 2.
  2. Index 2: 7 is not 9, Index ← 3. Index 3: 15 is not 9, Index ← 4.
  3. Index 4: 9 = 9, Found ← TRUE, so the loop stops with Index still 4.
Answer: Found at position 4
Your turn
Count is 2. Which element does Names[Count + 1] refer to?
Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which is a correct declaration of an array to hold 10 names?

Exam style+20 XP

Data holds 4, 8, 2, 6 at indexes 1 to 4. What is Data[2] + Data[4]?

Exam style+20 XP

Why is a FOR loop often used with an array?

Exam style+20 XP

FOR i ← 1 TO 4, Nums[i] ← i * 3, NEXT i. What is stored in Nums[3]?

Skill unlocked

One-dimensional arrays, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Declaring 1D arrays, inputting and processing values with loops, and finding totals, maximums and items.