Calculating an average in Cambridge pseudocode

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Calculating an average in Cambridge pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which declaration is best for a variable that stores an average?
A DECLARE Average : INTEGER
B DECLARE Average : REAL
3. Which line correctly calculates the average in Cambridge pseudocode?
A Average ← Total / Count
B Total / Count ← Average
4. Which check stops the program dividing by zero?
A IF Count > 0 THEN
B IF Count = 0 THEN Average ← Total / Count
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

Average ← Total / Count

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
DECLARE Average : ____
2.
Average ← Total _ Count
3.
Total ← Total + Scores[Index] ____ Index Average ← Total / Count
4.
IF Count _ 0 THEN Average ← Total / Count ENDIF
5.
DECLARE Marks : ARRAY[1:3] OF INTEGER Total ← Marks[1] + Marks[2] + Marks[3] Average ← Total / _

Spot the Error

Shade in the letter of the line that contains an 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
Question 2
A DECLARE Average : INTEGER
B Total ← 120
C Count ← 8
D Average ← Total / Count
E OUTPUT Average
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
Question 4
A INPUT Count
B IF Count > 0 THEN
C Average ← Total DIV Count
D ENDIF
Question 5
A DECLARE Average : REAL
B Total ← 75
C Count ← 6
D Total / Count ← Average
E OUTPUT Average

Answer Key

Calculating an average in Cambridge pseudocode
Section A: Quick Questions
Q1
A — After the loop has finished (after NEXT)
Q2
B — DECLARE Average : REAL
Q3
A — Average ← Total / Count
Q4
A — IF Count > 0 THEN
Q5
B — Average ← Total / 5
Section B: Match the Pairs
Q1
Average ← Total / Count → Divides the finished total by the number of items to give the mean
Q2
DECLARE Average : REAL → Sets up a variable that can store an average with decimal places
Q3
Divide after the loop has finished, so Total holds every value → NEXT Index Average ← Total / Count
Q4
Check that Count is above zero before dividing, to avoid dividing by zero → IF Count > 0 THEN Average ← Total / Count ENDIF
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
DECLARE Average : REAL
Q2
Average ← Total / Count
Q3
Total ← Total + Scores[Index] NEXT Index Average ← Total / Count
Q4
IF Count > 0 THEN Average ← Total / Count ENDIF
Q5
DECLARE Marks : ARRAY[1:3] OF INTEGER Total ← Marks[1] + Marks[2] + Marks[3] Average ← Total / 3
Section E: Spot the Error
Q1
C — A FOR loop must use ← to set the start value. It should be FOR Index ← 1 TO 5.
Q2
A — An average is often a decimal such as 15.5, so Average must be declared as REAL, not INTEGER.
Q3
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.
Q4
C — DIV throws away the decimal part, so the mean would be wrong. Use / to get the exact average: Average ← Total / Count.
Q5
D — The variable being assigned to must be on the left of ←. It should be Average ← Total / Count.