← IGCSE Computer Science Paper 2

Calculating an average in Cambridge pseudocode

Divides the finished Total by Count after the loop ends, giving the mean. Declare Average as REAL. Check Count > 0 first to avoid dividing by zero.

Understanding Calculating an average in Cambridge pseudocode in IGCSE Computer Science Paper 2

What it does: The division comes after NEXT, when the loop has added up all the values. It is not inside the loop. The IF stops the division from running when Count is 0, because dividing by zero causes an error. An average often has decimal places, for example 74.5, so it should be declared as REAL. The variable being assigned goes on the left of the arrow, and the calculation goes on the right. Divide by the number of items, 5. You cannot divide by the array name itself. 240 divided by 4 is 60. Adding the numbers instead of dividing is a common mistake.

Common mistakes: A FOR loop must use ← to set the start value. It should be FOR Index ← 1 TO 5. An average is often a decimal such as 15.5, so Average must be declared as REAL, not INTEGER.

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 286–295) and word search (Modules 275–295).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Where should Average ← Total / Count go in a loop that totals marks?
After the loop has finished (after NEXT)Inside the loop, before NEXT
Matches
FOR Index ← 1 TO 5 Total ← Total + Marks[Index] Average ← Total / Count NEXT Index
↓
The average is calculated inside the loop, so it is worked out with an unfinished total. The division should come after NEXT Index.
Applying
Meanings
Term
Average ← Total / Count
Meaning
Divides the finished Total by Count after the loop ends, giving the mean. Declare Average as REAL. Check Count > 0 first to avoid dividing by zero.
Practice
DECLARE Total : INTEGER
Testing
Gaps
DECLARE Average :
Check Answer
Spots
2Total ← 0
3FOR Index = 1 TO 5

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

The 'Blobfish' badge — Pressure Handler, 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. Where should Average ← Total / Count go in a loop that totals marks?
A. After the loop has finished (after NEXT) ✓
B. Inside the loop, before NEXT
The total is only complete once every item has been added, so the division goes after the loop ends.
2. Which declaration is best for a variable that stores an average?
A. DECLARE Average : INTEGER
B. DECLARE Average : REAL ✓
An average often has decimal places, for example 74.5, so it should be declared as REAL.
3. Which line correctly calculates the average in Cambridge pseudocode?
A. Average ← Total / Count ✓
B. Total / Count ← Average
The variable being assigned goes on the left of the arrow, and the calculation goes on the right.
4. Which check stops the program dividing by zero?
A. IF Count > 0 THEN ✓
B. IF Count = 0 THEN Average ← Total / Count
Only divide when Count is greater than 0. If Count is 0, the division would be by zero, which causes an error.
5. An array Marks has 5 items and the loop adds each one to Total. Which fixed count gives the correct average?
A. Average ← Total / Marks
B. Average ← Total / 5 ✓
Divide by the number of items, 5. You cannot divide by the array name itself.

Match the Pairs

Average ← Total / Count → Divides the finished total by the number of items to give the mean
DECLARE Average : REAL → Sets up a variable that can store an average with decimal places
Divide after the loop has finished, so Total holds every value → NEXT Index Average ← Total / Count
Check that Count is above zero before dividing, to avoid dividing by zero → IF Count > 0 THEN Average ← Total / Count ENDIF
FOR Index ← 1 TO 5 Total ← Total + Marks[Index] Average ← Total / Count NEXT Index → The average is calculated inside the loop, so it is worked out with an unfinished total. The division should come after NEXT Index.
DECLARE Average : INTEGER Average ← Total / Count → Average is declared as INTEGER, so it cannot store decimal values such as 72.5. It should be declared as REAL.

Key Term

Average ← Total / Count
Divides the finished Total by Count after the loop ends, giving the mean. Declare Average as REAL. Check Count > 0 first to avoid dividing by zero.

Fill in the Blanks

1. DECLARE Average : REAL
Hint: Type the data type that can store a decimal result.
2. Average ← Total / Count
Hint: Type the arithmetic operator that divides the total by the count.
3. Total ← Total + Scores[Index] NEXT Index Average ← Total / Count
Hint: Type the keyword that ends the loop, so the division happens after it.
4. IF Count > 0 THEN Average ← Total / Count ENDIF
Hint: Type the comparison operator that checks Count is greater than zero.
5. DECLARE Marks : ARRAY[1:3] OF INTEGER Total ← Marks[1] + Marks[2] + Marks[3] Average ← Total / 3
Hint: Type the fixed number of elements in the array to divide by.

Spot the Error

Question 1
A DECLARE Average : REAL
B Total ← 0
C FOR Index = 1 TO 5
D Total ← Total + Marks[Index]
E NEXT Index
F Average ← Total / 5
Line C — A FOR loop must use ← to set the start value. It should be FOR Index ← 1 TO 5.
Question 2
A DECLARE Average : INTEGER
B Total ← 120
C Count ← 8
D Average ← Total / Count
E OUTPUT Average
Line A — An average is often a decimal such as 15.5, so Average must be declared as REAL, not INTEGER.
Question 3
A Count ← 4
B Total ← 1
C FOR Index ← 1 TO Count
D Total ← Total + Marks[Index]
E NEXT Index
F Average ← Total / Count
Line B — Total must start at 0. Starting at 1 adds an extra 1 to the sum, so the average will be wrong. It should be Total ← 0.
Question 4
A INPUT Count
B IF Count > 0 THEN
C Average ← Total DIV Count
D ENDIF
Line C — DIV throws away the decimal part, so the mean would be wrong. Use / to get the exact average: Average ← Total / Count.
Question 5
A DECLARE Average : REAL
B Total ← 75
C Count ← 6
D Total / Count ← Average
E OUTPUT Average
Line D — The variable being assigned to must be on the left of ←. It should be Average ← Total / Count.

Student with a class code? Join your class →