Programming · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 5% of your exam marks.
1D array manipulation with FOR loops appears regularly in Paper 2. 4 to 6 marks.
A FOR loop is the natural way to visit every element of an array in turn. The counter variable becomes the index.
DECLARE Index : INTEGER
FOR Index ← 1 TO 5
OUTPUT Scores[Index]
NEXT Index
This prints every element of Scores in turn:
12
100
5
2
8
Sum every element of a numeric array:
DECLARE Total : INTEGER
Total ← 0
FOR Index ← 1 TO 5
Total ← Total + Scores[Index]
NEXT Index
OUTPUT "Total: ", Total
Find the largest element (assumes at least one element exists):
DECLARE Highest : INTEGER
Highest ← Scores[1]
FOR Index ← 2 TO 5
IF Scores[Index] > Highest THEN
Highest ← Scores[Index]
ENDIF
NEXT Index
OUTPUT "Highest: ", Highest
Count how many elements satisfy a condition (e.g. count pass marks ≥ 50):
DECLARE Passes : INTEGER
Passes ← 0
FOR Index ← 1 TO 30
IF Marks[Index] >= 50 THEN
Passes ← Passes + 1
ENDIF
NEXT Index
OUTPUT "Number of passes: ", Passes
Search for a value (linear search, topic 20):
DECLARE Found : BOOLEAN
Found ← FALSE
FOR Index ← 1 TO 30
IF Marks[Index] = Target THEN
Found ← TRUE
OUTPUT "Found at position ", Index
ENDIF
NEXT Index
IF Found = FALSE THEN
OUTPUT "Not found"
ENDIF
Real algorithms usually need to know the array's size at run time, not as a hard-coded number. A common idiom:
DECLARE Size : INTEGER
Size ← 30
DECLARE Marks : ARRAY[1:Size] OF INTEGER
FOR Index ← 1 TO Size
OUTPUT Marks[Index]
NEXT Index
Now if the class size changes, only the line Size ← 30 needs to be edited.