Use arrays to store multiple values of the same data type, initialise them, and manipulate the data with count‑controlled FOR … NEXT loops.
FOR … NEXT).Scores, Temp, Grid.| 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 |
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”.
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
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)
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 …
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).
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
value ← Array[i].Array[i] ← value.FOR … NEXT loop that matches the declared bounds.Upper – Lower + 1 when required.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
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
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
Scores[11] when the upper bound is 10) causes a run‑time error and loses marks.Scores instead of Scores[i] inside a loop.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.
Array[i]) and, if the question asks for processing, a concise FOR … NEXT loop.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
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.