Topic 8 ยท Programming ยท 8.15

Arrays Explained Simply

An array stores many related values under one identifier. Each value is found using its position, called an index.

Invitation

How can one variable store a whole class list?

Using a separate variable for every student quickly becomes difficult to manage.

An array stores several values of the same type under one meaningful name.

One identifier. Many related values. Each value has an index.
Figure 1
Many names, one array
StudentName[1] = "Amina"
StudentName[2] = "Ben"
StudentName[3] = "Chen"
Big Idea

The index tells the program which element to use

Look at Figure 2. The array name stays the same, but the index changes. The index is the address of one element.

Index 172
Index 265
Index 381
Index 454
Index 590

Score[3] means: use the value at position 3. In this array, that value is 81.

The square brackets do not hold the value. They hold the position of the value.
FutureLogic Bridge

An array is like a row of labelled pigeonholes

Each pigeonhole belongs to the same cabinet, but every slot has its own number.

The cabinet name is the array identifier. The slot number is the index. The item inside is the stored value.

Array name = cabinet. Index = slot number. Element = value inside.
Figure 3
The memory-slots teaching chip
1Red
2Blue
3Green
4Gold
5White
Colour[4] stores โ€œGoldโ€.
Worked Example

Use a loop to output every array element

The loop changes the index from 1 to 5. Each pass accesses the next element.

PseudocodeIndexes 1โ€“5
FOR Index โ† 1 TO 5
  OUTPUT Score[Index]
NEXT Index
PythonIndexes 0โ€“4
for index in range(5):
    print(score[index])
Important: Cambridge pseudocode often uses indexes starting at 1. Python lists normally start at 0. Follow the language and array bounds given in the question.
1D and 2D Arrays

A 1D array is a line; a 2D array is a grid

1D array

Uses one index.

1Ada
2Ben
3Chen

StudentName[2]

2D array

Uses two indexes: row and column.

1,172
1,268
2,181
2,277

Score[2,1] means row 2, column 1.

1D = one index. 2D = two indexes.
Exam Tip

Use the exact array name and bounds from the question

Array questions often provide identifiers such as StudentName[], ScreenTime[] or TeamPoints[]. Use those exact names.

Full-mark habit: initialise totals and counters, use the correct number of indexes, and loop through every required element.

Typical exam pattern

PseudocodeTotal an array
Total โ† 0
FOR Index โ† 1 TO 50
  Total โ† Total + Number[Index]
NEXT Index
OUTPUT Total
Common Mistake

Do not forget the index

โœ— Incomplete

Total โ† Total + Number

This does not identify which element is being added.

โœ“ Correct

Total โ† Total + Number[Index]

The index selects one element during each loop pass.

Another common mistake: using one index for a 2D array. A 2D array needs both a row and a column, for example ScreenTime[Student, Day].
Summary

Arrays organise related values

1One identifier stores many values.
2An index selects one element.
3Loops process arrays efficiently.
Remember: 1D arrays use one index. 2D arrays use two.