Totalling, counting, maximum, minimum and average Cambridge IGCSE Computer Science (9–1) revision
Not started
Learn it
In plain words
A handful of small patterns turn up in almost every program: adding things up, counting things, and finding the biggest or the smallest. Learn them once and you will recognise them everywhere.
6 things to know
- Totalling: keep a running total. Set Total ← 0 before the loop. Inside the loop, Total ← Total + Value.
- Counting: add one each time something happens. Set Count ← 0 before the loop. Inside the loop, Count ← Count + 1.
- Average: after the loop, Average ← Total / Count.
- Maximum: set Max to the first value. For each later value, IF Value > Max THEN Max ← Value.
- Minimum: set Min to the first value. For each later value, IF Value < Min THEN Min ← Value.
- If you cannot start with the first value, start Max at a value lower than any possible data, and Min at a value higher than any possible data.
Worked example
An algorithm finds the largest of the marks 12, 30 and 7. Trace what happens to Max.
- Max is set to the first mark: 12.
- The next mark is 30. Is 30 > 12? Yes, so Max becomes 30.
- The last mark is 7. Is 7 > 30? No, so Max stays at 30.
- The algorithm outputs 30.
Tips and tricks
- Set Total and Count to zero before the loop, not inside it. Inside, they would be reset on every pass.
- Starting Min at 0 is a classic mistake. If every value is positive, nothing is ever smaller than 0, and the answer is wrongly 0.
It lands in your notebook with its questions as flashcards.
Totalling, counting, maximum, minimum and average: 6 questions and answers
These are the quiz’s questions. Do the quiz first, then come back here for the ones that got you.
Which statement keeps a running total?
Each new number is added to what is already there.
Which statement counts?
One is added each time.
How is an average calculated from a total and a count?
Divide what was added up by how many there were.
What should a running total be set to before the loop begins?
Otherwise it starts from whatever happened to be in memory.
When finding a maximum, which test is used for each new value?
A bigger value replaces the maximum so far.
All the temperatures entered are between 5 and 40. An algorithm sets Min ← 0 and then tests IF Temp < Min. What will it output as the minimum?Stretch
No temperature is below 0, so Min never changes. It should start at the first value, or at a high value.
Quiz
6 questions
Tap an answer and you’ll see straight away whether it’s right, and why.
Worksheet
2 questions, 4 marks. Write your answers on paper, then check them.
Totalling, counting, maximum, minimum and average
Cambridge IGCSE Computer Science (9–1) 0984 · 4 marks · papermunch.org
Name ______________________________ Date ______________
Write pseudocode statements that add the value in Price to a running total and count how many prices have been entered.[2]
Show answerHide answer
Total ← Total + Price and Count ← Count + 1.
Explain why a variable used for a running total must be set to zero before the loop starts.[2]
Show answerHide answer
So that the total starts from a known value. If it were set inside the loop it would be reset to zero on every pass.
Answers: Totalling, counting, maximum, minimum and average
- 1. Total ← Total + Price and Count ← Count + 1.
- 2. So that the total starts from a known value. If it were set inside the loop it would be reset to zero on every pass.



