← IGCSE Computer Science Paper 2

Working out an average from a total and a count

After the loop ends, divide the total by the count to get the mean. Check that Count > 0 first, so the program never divides by zero.

Understanding Working out an average from a total and a count in IGCSE Computer Science Paper 2

What it does: Each pass of the loop adds the value to Total and adds 1 to Count, so both are ready for the average at the end. The division comes after ENDWHILE so it uses the final Total and Count, and it sits inside an IF so it never divides by 0. Dividing by zero is not allowed. When the user enters no values, Count stays at 0, so the program must not divide. 150 / 5 = 30. Multiplying would give 750, but an average is found by dividing.

Common mistakes: Count >= 0 is true when Count is 0, so the program would still divide by zero. The check must be IF Count > 0 THEN. The average is the total divided by the count, not the other way round. It should be Average <- Total / Count.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 379–388) and word search (Modules 379–398).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly works out the average once Total and Count are known?
Total <- Average / CountAverage <- Total / Count
Matches
WHILE Number <> -1 DO Total <- Total + Number Count <- Count + 1 Average <- Total / Count INPUT Number ENDWHILE
↓
The average is calculated inside the loop, so it is recalculated every time instead of once at the end. It should come after ENDWHILE.
Applying
Meanings
Term
Average <- Total / Count
Meaning
After the loop ends, divide the total by the count to get the mean. Check that Count > 0 first, so the program never divides by zero.
Practice
Total <- 0
Testing
Gaps
Average <- Total Count
Check Answer
Spots
1IF Count >= 0 THEN
2 Average <- Total / Count

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Wolverine' badge — Fearless Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly works out the average once Total and Count are known?
A. Total <- Average / Count
B. Average <- Total / Count ✓
The average (mean) is the total divided by the count, and the result is stored in Average.
2. Where should Average <- Total / Count go in a loop that reads 10 marks?
A. Inside the loop, before the last mark is read
B. After the loop has ended ✓
Calculate the average after the loop, when Total and Count hold their final values. Inside the loop they are still changing.
3. Which two statements belong inside the loop to build up the data for an average?
A. Total <- Total + Mark and Count <- Count + 1 ✓
B. Total <- Total / Count and Count <- Count + 1
The loop adds each value to Total and adds 1 to Count. The division is done only once, after the loop.
4. Why check IF Count > 0 before working out the average?
A. If Count is 0, dividing by it would cause an error ✓
B. If Count is 0, the average is always 100
Dividing by zero is not allowed. When the user enters no values, Count stays at 0, so the program must not divide.
5. Which is the safer way to calculate the average?
A. IF Count > 0 THEN Average <- Total / Count ENDIF ✓
B. Average <- Total / Count IF Count > 0 THEN ENDIF
The IF must come first so the division only runs when Count is greater than 0.

Match the Pairs

Average <- Total / Count → Divides the running total by the number of values to find the mean, after the loop has finished
IF Count > 0 THEN → Checks that at least one value was entered, so the program never divides by zero
Total and Count are both updated in the same loop, once for each value entered → Total <- Total + Number Count <- Count + 1
The average is calculated once, after the loop has ended, and only if Count is above 0 → ENDWHILE IF Count > 0 THEN Average <- Total / Count ENDIF
WHILE Number <> -1 DO Total <- Total + Number Count <- Count + 1 Average <- Total / Count INPUT Number ENDWHILE → The average is calculated inside the loop, so it is recalculated every time instead of once at the end. It should come after ENDWHILE.
Average <- Total / Count OUTPUT Average → There is no check that Count is above 0. If the user enters no values, the program divides by zero. It needs IF Count > 0 THEN around the calculation.

Key Term

Average <- Total / Count
After the loop ends, divide the total by the count to get the mean. Check that Count > 0 first, so the program never divides by zero.

Fill in the Blanks

1. Average <- Total / Count
Hint: Type the arithmetic operator that divides the total by the count.
2. IF Count > 0 THEN Average <- Total / Count ENDIF
Hint: Type the relational operator that checks the count is greater than zero, so the program never divides by zero.
3. Total <- Total + Mark Count <- Count + 1
Hint: Type the variable name that keeps track of how many values have been entered.
4. ENDWHILE IF Count > 0 THEN Average <- Total / Count
Hint: Type the keyword that follows the condition in a selection statement.
5. Average <- Total / Count OUTPUT "Average: ", Average
Hint: Type the variable that stores the result of the division, so it can be displayed.

Spot the Error

Question 1
A IF Count >= 0 THEN
B Average <- Total / Count
C OUTPUT Average
D ENDIF
Line A — Count >= 0 is true when Count is 0, so the program would still divide by zero. The check must be IF Count > 0 THEN.
Question 2
A IF Count > 0 THEN
B Average <- Count / Total
C OUTPUT Average
D ELSE
E OUTPUT "No marks entered"
F ENDIF
Line B — The average is the total divided by the count, not the other way round. It should be Average <- Total / Count.
Question 3
A REPEAT
B INPUT Mark
C Total <- Total + Mark
D Count <- Count + Mark
E UNTIL Count = 5
F Average <- Total / Count
Line D — Count must go up by 1 for each mark entered, not by the mark itself. It should be Count <- Count + 1.
Question 4
A Average <- 0
B IF Total > 0 THEN
C Average <- Total / Count
D ENDIF
E OUTPUT Average
Line B — The divisor is Count, so Count is the value that must be checked. Total > 0 does not stop a division by zero. It should be IF Count > 0 THEN.
Question 5
A FOR N <- 1 TO 4
B Total <- Total + Mark[N]
C Count <- 1
D NEXT N
E Average <- Total / Count
Line C — Count <- 1 resets Count on every pass, so it always ends as 1 and the average is wrong. It should be Count <- Count + 1.

Student with a class code? Join your class →