🎯 Syllabus & Goals 3 min
Cambridge 7.4 · Standard methods of solution Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Write the standard methods for totalling and counting, including counting down.
- Find the maximum and minimum of a list using both initialisation methods.
- Calculate an average (mean) from a total, and trace all of these in a trace table.
Textbook: Chapter 7, §7.4.1–7.4.3 (pp. 272–274)
Recap / Warm-Up 5 min
You can now write assignment, selection and all three loops. Standard methods are small, reusable patterns built from exactly those parts. Real programs repeat them thousands of times.
Quick starter
Which loop would you use to process every mark in a class of 30 — and why?
Reveal the answer
FOR Counter ← 1 TO 30, because the number of repeats (30) is known in advance.
🧠 Key Concept 14 min
1 · Totalling
Totalling keeps a running total that each value is added to. The total must start at zero before the loop.
Total ← 0 FOR Counter ← 1 TO ClassSize Total ← Total + StudentMark[Counter] NEXT Counter
Total holds 330.2 · Counting
Counting keeps a count of how many times something happens. It adds 1, not the value. It is often inside an IF, so only some items are counted.
Count up — how many passed?
PassCount ← 0
FOR Counter ← 1 TO ClassSize
IF StudentMark[Counter] >= 50
THEN
PassCount ← PassCount + 1
ENDIF
NEXT CounterCount down — stock left
NumberInStock ← NumberInStock - 1
IF NumberInStock < 20
THEN
CALL Reorder()
ENDIF3 · Maximum, minimum and average
To find the largest value, keep a "best so far" and replace it whenever a larger value appears. The smallest works the same way the other way round. There are two ways to start:
Method A — extreme starting values
If the possible range is known (marks 0–100): start MaximumMark ← 0 and MinimumMark ← 100, then loop from item 1.
Method B — the first item
If the range is not known: start both at StudentMark[1], then loop from item 2. Works for any numbers.
The average (mean) extends totalling: total everything inside the loop, then divide by the number of values after the loop: Average ← Total / ClassSize.
Worked Example 12 min
Worked example 1 · Total, count of passes and average
Six students scored 45, 62, 38, 71, 55, 59 (stored in Score[1] to Score[6]). Output the total, the number of scores of 50 or more, and the average.
DECLARE Score : ARRAY[1:6] OF INTEGER
Total ← 0
PassCount ← 0
FOR Counter ← 1 TO 6
Total ← Total + Score[Counter]
IF Score[Counter] >= 50
THEN
PassCount ← PassCount + 1
ENDIF
NEXT Counter
Average ← Total / 6
OUTPUT Total, " ", PassCount, " ", Averagescore = [45, 62, 38, 71, 55, 59] total = 0 pass_count = 0 for counter in range(6): total = total + score[counter] if score[counter] >= 50: pass_count = pass_count + 1 average = total / 6 print(total, pass_count, average)
330 4 55.0
| Counter | Score[Counter] | Total | PassCount | Average | OUTPUT |
|---|---|---|---|---|---|
| 0 | 0 | ||||
| 1 | 45 | 45 | |||
| 2 | 62 | 107 | 1 | ||
| 3 | 38 | 145 | |||
| 4 | 71 | 216 | 2 | ||
| 5 | 55 | 271 | 3 | ||
| 6 | 59 | 330 | 4 | ||
| 55 | 330 4 55 |
- Initialise both to 0 before the loop.otherwise the first addition uses an unknown value.
- Every pass adds to Total; only passes where the score is ≥ 50 add 1 to PassCount (passes 2, 4, 5, 6).
- After the loop, Average ← 330 / 6 = 55.dividing inside the loop would give a wrong "average so far" every pass.
- Python note: lists start at index 0, so
range(6)gives 0–5. Pseudocode's array here runs 1–6. Same six items, different numbering.
Worked example 2 · Highest and lowest score (Method B)
MaximumScore ← Score[1]
MinimumScore ← Score[1]
FOR Counter ← 2 TO 6
IF Score[Counter] > MaximumScore
THEN
MaximumScore ← Score[Counter]
ENDIF
IF Score[Counter] < MinimumScore
THEN
MinimumScore ← Score[Counter]
ENDIF
NEXT Counter
OUTPUT MaximumScore, " ", MinimumScorescore = [45, 62, 38, 71, 55, 59] maximum_score = score[0] minimum_score = score[0] for counter in range(1, 6): if score[counter] > maximum_score: maximum_score = score[counter] if score[counter] < minimum_score: minimum_score = score[counter] print(maximum_score, minimum_score)
71 38
| Counter | Score[Counter] | MaximumScore | MinimumScore | OUTPUT |
|---|---|---|---|---|
| 45 | 45 | |||
| 2 | 62 | 62 | ||
| 3 | 38 | 38 | ||
| 4 | 71 | 71 | ||
| 5 | 55 | |||
| 6 | 59 | 71 38 |
- Both start at the first score, 45.a value from the list can never be "beaten" by an impossible starting value.
- The loop starts at 2, because item 1 is already the best so far.
- Two separate IFs, not IF…ELSE. Each score is checked against both the maximum and the minimum.with ELSE, a score that set a new maximum would skip the minimum test — harmless here, but a bad habit.
- Result: maximum 71, minimum 38.
Try It Yourself 12 min
Goal: Dry run worked example 1 with the scores 50, 20, 80, 49, 51, 90. State the three outputs.
Goal: Write pseudocode that inputs 12 monthly rainfall figures (in mm) and outputs the total, the average and the number of months with more than 100 mm.
Goal: Write pseudocode that inputs 7 daily temperatures from a polar research station (all below 0 °C) and outputs the highest and lowest. Explain why Method A with Max ← 0 would fail.
Hint
If every temperature is below 0, can any of them ever be greater than a starting maximum of 0? Use Method B instead: set both to the first temperature input.
📝 Exam Practice 10 min
Write pseudocode to input 20 temperatures, then output their total and their average.
Mark scheme
Total ← 0before the loop (1)- A loop that runs 20 times, e.g.
FOR Count ← 1 TO 20…NEXT Count(1) INPUT Temperatureinside the loop (1)Total ← Total + Temperatureinside the loop (1)Average ← Total / 20after the loop, and both output (1)
In an algorithm to find the lowest percentage mark, MinimumMark is set to 100 before the loop. Explain why it is set to 100 and not to 0.
Mark scheme
- 100 is the highest possible mark, so any real mark is less than or equal to it and will replace it (1).
- If it started at 0, no mark could be lower, so the minimum would wrongly stay 0 (1).
Complete a trace table for this algorithm using the inputs 7, 12, 3, 12.
Count ← 0
Big ← 0
FOR N ← 1 TO 4
INPUT X
IF X > 10
THEN
Count ← Count + 1
ENDIF
IF X > Big
THEN
Big ← X
ENDIF
NEXT N
OUTPUT Count, BigMark scheme
| Count | Big | N | X | OUTPUT |
|---|---|---|---|---|
| 0 | 0 | |||
| 7 | 1 | 7 | ||
| 1 | 12 | 2 | 12 | |
| 3 | 3 | |||
| 2 | 4 | 12 | 2 12 |
- Initial values 0, 0 (1)
- N and X columns correct (1)
- Count correct: 1 then 2 (1)
- Big correct: 7 then 12, and output 2 12 (1)
🗝️ Recap & Key Terms 3 min
Totalling adds each value to a total that starts at 0. Counting adds 1 each time something happens. Maximum and minimum keep a "best so far", starting either at extreme values or at the first item. The average is the total divided by the number of values, after the loop.
- Totalling
- Keeping a running total that values are added to, e.g.
Total ← Total + Mark. - Counting
- Keeping a count of the number of times an action is performed, e.g.
Count ← Count + 1. - Initialisation
- Giving a variable its starting value before it is used, e.g.
Total ← 0. - Maximum / minimum
- The largest / smallest value in a list, found by comparing each item with the best so far.
- Average (mean)
- The total of the values divided by how many values there are.
Homework 1 min
Task (≤ 15 min): A shop records the number of items sold each day for 30 days in Sales[1:30]. Write pseudocode to output the highest sales, the lowest sales, the average, and how many days sold more than the average. [6]
Model answer
Total ← 0
Highest ← Sales[1]
Lowest ← Sales[1]
FOR Day ← 1 TO 30
Total ← Total + Sales[Day]
IF Sales[Day] > Highest
THEN
Highest ← Sales[Day]
ENDIF
IF Sales[Day] < Lowest
THEN
Lowest ← Sales[Day]
ENDIF
NEXT Day
Average ← Total / 30
AboveCount ← 0
FOR Day ← 1 TO 30
IF Sales[Day] > Average
THEN
AboveCount ← AboveCount + 1
ENDIF
NEXT Day
OUTPUT Highest, " ", Lowest, " ", Average, " ", AboveCountMarks: initialisation (1); totalling (1); max (1); min (1); average after loop (1); a second loop to count days above the average — it cannot be known until the first loop ends (1).