Totalling, counting, maximum, minimum and average Cambridge IGCSE Computer Science 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

  1. Totalling: keep a running total. Set Total ← 0 before the loop. Inside the loop, Total ← Total + Value.
  2. Counting: add one each time something happens. Set Count ← 0 before the loop. Inside the loop, Count ← Count + 1.
  3. Average: after the loop, Average ← Total / Count.
  4. Maximum: set Max to the first value. For each later value, IF Value > Max THEN Max ← Value.
  5. Minimum: set Min to the first value. For each later value, IF Value < Min THEN Min ← Value.
  6. 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.

  1. Max is set to the first mark: 12.
  2. The next mark is 30. Is 30 > 12? Yes, so Max becomes 30.
  3. The last mark is 7. Is 7 > 30? No, so Max stays at 30.
  4. 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.
6 questions, about 2 minutes.

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.

  1. Which statement keeps a running total?
    • Total ← Number
    • Total ← Total + Number (the answer)
    • Total ← Total + 1
    • Number ← Total

    Each new number is added to what is already there.

  2. Which statement counts?
    • Count ← Number
    • Count ← Count + Number
    • Count ← Count + 1 (the answer)
    • Count ← 1

    One is added each time.

  3. How is an average calculated from a total and a count?
    • Total × Count
    • Count / Total
    • Total / Count (the answer)
    • Total − Count

    Divide what was added up by how many there were.

  4. What should a running total be set to before the loop begins?
    • 0 (the answer)
    • 1
    • 100
    • the largest value

    Otherwise it starts from whatever happened to be in memory.

  5. When finding a maximum, which test is used for each new value?
    • IF Value < Max
    • IF Value > Max (the answer)
    • IF Value = Max
    • IF Value <> 0

    A bigger value replaces the maximum so far.

  6. 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
    • 0 (the answer)
    • 5
    • 40
    • the first temperature

    No temperature is below 0, so Min never changes. It should start at the first value, or at a high value.

Still stuck on this one?Ask in the Papermunch Discord, or help someone else who is. Discord is for ages 13 and up.Join the server

Things you can type

Or go straight to

Or browse a shelf