Arrays · Cambridge syllabus 7.4, 8.2 · Lesson 1 of 3
One-Dimensional Arrays
A one-dimensional array stores several values under one name, accessed by an index. Know the declared lower and upper bounds; index conventions differ between languages. Use the index to read or replace an individual element.
By the end, you can
- Declare and use one-dimensional arrays.
- Store multiple values in an array.
- Access array elements using indexes.
- Use variables as indexes.
1D arrays
An array is a set of elements of the same type stored under one identifier, each reached by its index. Thirty separate variables for thirty marks cannot be processed by a loop; one array of thirty can.
It is declared with its size and type: DECLARE Marks : ARRAY[1:30] OF INTEGER reserves thirty integer elements. Every element has the same type, and the size is usually fixed when the array is declared.
An element is used exactly like a variable, with its index in square brackets. Marks[1] ← 75 stores a value; Total ← Total + Marks[i] reads one. The index may be a literal, a variable or an expression, and using a variable is the whole point, because a loop counter can then walk through the array.
Where the indexes start matters and differs between languages. Pseudocode arrays usually run from 1 to n, matching everyday counting; Python and many others run from 0 to n − 1. An array of ten elements indexed from zero has no element ten, and reaching for one is an out-of-bounds error — the commonest array bug there is. Decide which convention applies and be consistent.
Arrays are used wherever many values of the same kind are handled together: marks, names, temperatures, prices, a list of items in a game. The gain is that one loop processes all of them, whatever the length.
Worth remembering
- One identifier, many elements of the same type, each reached by an index.
- The index may be a variable, which is what lets a loop walk the array.
- Know whether indexing starts at 0 or 1, and never read outside the bounds.
Worked example
For ARRAY[1:3], valid indexes are 1, 2 and 3. Scores[2] refers to the second element.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
6 − 2 + 1 = 5 elements.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.