Cambridge IGCSE · Topic 7 (3-4)
IGCSE Computer Science 0478 / Topic 7 / IGCSE

Keep a running total.

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.

The key idea

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.

Totalling: add each mark to a running total Mark Total 6438915047 64102193243290 Total 290, Average 290 / 5 = 58 Highest 91, Lowest 38, Count of marks ≥ 50: 3
Fig. 1: five marks added one at a time to a running total
Section 01

Totalling and counting.

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
Examiner trap

Totalling adds the value (Total ← Total + Mark); counting adds 1 (Count ← Count + 1). Swapping them is one of the most common errors examiners set.

Section 02

Maximum and minimum.

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.

CounterMarkTotalCountPassHighestLowest
000100
1646416464
23810238
391193291
4502433
547290
Your turn
A program finds the lowest of some temperatures that can be from -50 to 50. Why would Lowest ← 0 be a poor starting value?
Examiner trap

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.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which statement adds a value to a running total?

Quick check+10 XP

What should Count be set to before a counting loop starts?

Quick check+10 XP

Marks range from 0 to 100. Which initial value is safe for Highest?

Quick check+10 XP

In the lab's totalling program, when is the average calculated?

Section 03

Working out the average.

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.

Worked example Average with a count
Values are input until -1 is entered. The inputs are 12, 15, 20, 9, -1. Find the total, the count and the average.
  1. The rogue value -1 stops the loop; it is not added or counted.
  2. Total = 12 + 15 + 20 + 9 = 56.
  3. Count = 4 values.
  4. Average = 56 / 4 = 14.
Answer: Total 56, count 4, average 14
Your turn
Three test scores are 7, 8 and 10. What does ROUND(Total / 3, 1) output?
Examiner trap

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.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

The values 14, 6, 22, 18 are input. What is the average?

Exam style+20 XP

Which line is needed inside the loop to find the minimum value?

Exam style+20 XP

A program counts how many of 30 temperatures are above 25. Which pair of statements is correct?

Exam style+20 XP

Values 3, 9, 4 are input into the algorithm in this lesson but with Highest ← 10 at the start. What is output for Highest?

Skill unlocked

Totalling, counting and averages, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Totalling, counting, maximum, minimum and average: the standard patterns, their initial values, and tracing them.