Arrays · Cambridge syllabus 7.4, 8.2 · Lesson 2 of 3
Array Processing
Iterate across the valid indexes to total, count, search or update an array. Initialise accumulators before the loop. For averages, divide by the actual count and handle an empty set when the problem allows one.
By the end, you can
- Read values from an array.
- Write values to an array.
- Use iteration to process array elements.
- Search an array.
- Calculate values from array contents, such as the total, the average, the largest and the smallest.
Iterating through arrays
Nearly everything done with an array is a loop over its indexes, and a handful of patterns cover almost every question.
To fill it: FOR i ← 1 TO 30, prompt, INPUT Marks[i], NEXT i. To print it: the same loop with an OUTPUT. To total it: set Total ← 0 before the loop, then Total ← Total + Marks[i] inside, and the average is the total divided by the number of elements — with the initialisation before the loop, not inside it.
To find the largest, set Highest ← Marks[1] before the loop and compare from the second element: IF Marks[i] > Highest, THEN Highest ← Marks[i]. Starting from the first element rather than from zero is what makes it work for a list of negative numbers, and keeping the index as well as the value is what lets the program say which element was largest.
To find the smallest, reverse the comparison: set Lowest ← Marks[1] before the loop, then IF Marks[i] < Lowest, THEN Lowest ← Marks[i]. The syllabus asks for the maximum, the minimum and the average, and one loop can find all three: compare with Highest, compare with Lowest and add to Total on every pass, then divide the total by the number of elements after the loop.
To search: walk the array comparing each element with the target, set a flag and record the position when it matches, and stop — a linear search, exactly as described in algorithm design.
To count how many satisfy a condition: set a counter to zero before the loop and increase it inside an IF. This is how a program counts the marks above a pass threshold.
Two arrays are often used in step, one holding names and another marks, with the same index referring to the same person in both. That works, and it requires care: anything that reorders one must reorder the other identically.
Worth remembering
- Initialise the total, the counter or the highest value before the loop, not inside it.
- Start the largest or the smallest at the first element, not at zero.
- Parallel arrays share an index, so reordering one means reordering both.
Worked example
A total over [4, 7, 9] progresses 0 → 4 → 11 → 20; the mean is 20/3.
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
The qualifying values are 8 and 6: total 14, count 2.
Check your method, units and explanation against it.
Marked practice
Set 1 of 5 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.