Most exam algorithms process a list of values: add them up, count the ones that meet a condition, find the biggest and smallest, work out the mean. Each job is a short standard pattern inside a loop, and each one starts with the right initial value.
Totalling: set Total ← 0 before the loop, then Total ← Total + Value inside it. Counting: set Count ← 0, then Count ← Count + 1 each time the condition is met. Maximum and minimum: start from a value that will certainly be replaced (or from the first item), then replace it when a bigger (or smaller) value is found. Average: total ÷ number of values, calculated after the loop.
In the lab, choose the totalling and counting programs and run them. Watch the trace table: Total grows by each value, while Count grows by exactly 1 when the condition is true.
DECLARE Mark, Total, CountPass, Highest, Lowest, Counter : INTEGER
DECLARE Average : REAL
Total ← 0
CountPass ← 0
Highest ← 0
Lowest ← 100
FOR Counter ← 1 TO 5
OUTPUT "Enter a mark from 0 to 100"
INPUT Mark
Total ← Total + Mark
IF Mark >= 50
THEN
CountPass ← CountPass + 1
ENDIF
IF Mark > Highest
THEN
Highest ← Mark
ENDIF
IF Mark < Lowest
THEN
Lowest ← Mark
ENDIF
NEXT Counter
Average ← Total / 5
OUTPUT "Average ", Average, " Passes ", CountPass
OUTPUT "Highest ", Highest, " Lowest ", Lowest
Totalling adds the value (Total ← Total + Mark); counting adds 1 (Count ← Count + 1). Swapping them is one of the most common errors examiners set.
The trace below follows the algorithm with the marks 64, 38, 91, 50, 47. Highest starts at 0, the lowest possible mark, so the first real mark replaces it; Lowest starts at 100, the highest possible mark.
| Counter | Mark | Total | CountPass | Highest | Lowest |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 100 | ||
| 1 | 64 | 64 | 1 | 64 | 64 |
| 2 | 38 | 102 | 38 | ||
| 3 | 91 | 193 | 2 | 91 | |
| 4 | 50 | 243 | 3 | ||
| 5 | 47 | 290 |
Highest ← Values[1] and Lowest ← Values[1], then compare the rest.Lowest ← 0 be a poor starting value?Starting Lowest at 0 is wrong whenever all the values could be above 0: the minimum would never change. Start the minimum high and the maximum low, or start both at the first value.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which statement adds a value to a running total?
What should Count be set to before a counting loop starts?
Marks range from 0 to 100. Which initial value is safe for Highest?
In the lab's totalling program, when is the average calculated?
The average (mean) is the total divided by how many values there are. It is calculated once, after the loop has finished, and is often a real number.
ROUND(Total / 3, 1) output?Do not calculate the average inside the loop, and do not divide by a loop counter whose value after the loop you are unsure of. Divide by a known number, or by a Count variable you have kept yourself.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
The values 14, 6, 22, 18 are input. What is the average?
Which line is needed inside the loop to find the minimum value?
A program counts how many of 30 temperatures are above 25. Which pair of statements is correct?
Values 3, 9, 4 are input into the algorithm in this lesson but with Highest ← 10 at the start. What is output for Highest?
This skill is now recorded on your profile. Keep the chain going.