Calculating an average in pseudocode

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

Calculating an average in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly calculates an average in IGCSE pseudocode?
A Average ← Total / Count
B Average = Total / Count
2. Where should the average be calculated in a loop that adds up marks?
A Inside the loop on every repeat
B After the loop has finished
3. An average is worked out by...
A dividing the total by the count
B multiplying the total by the count
4. Which check stops the program dividing by zero?
A IF Total > 0 THEN
B IF Count > 0 THEN
5. Which data type should the Average variable have?
A DECLARE Average : REAL
B DECLARE Average : INTEGER

Match the Pairs

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

Left columnRight column
1Average ← Total / Count ADivides the total of all the values by how many values there were, and stores the result in Average
2IF Count > 0 THEN Average ← Total / Count ENDIF BThe average is calculated inside the loop, so it is worked out before all the values have been added. It should be calculated once after NEXT, when the loop has finished.
3The average is calculated after the loop has finished, once Total and Count are complete CWithout brackets, the division happens first, so only Count / Count is divided and the result is wrong. It must be Total / Count, or use brackets such as (Total + Extra) / Count if more is added.
4The result of a division can have decimal places, so the variable is declared as REAL DENDWHILE Average ← Total / Count
5Average ← Total + Count / Count EOnly works out the average when at least one value was entered, so it never divides by zero
6FOR Number ← 1 TO 10 Total ← Total + Number Average ← Total / Count NEXT Number FDECLARE Average : REAL

Definitions

Average ← Total / Count

Fill in the Blanks

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

1.
Average ← _____ / Count
2.
Average _ Total / Count
3.
IF Count __ 0 THEN Average ← Total / Count ENDIF
4.
DECLARE Average : ____
5.
Average ← _Total + Bonus) / Count

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Average : INTEGER
B Average ← Total / Count
C OUTPUT Average
Question 2
A Count ← 5
B Average ← Total * Count
C OUTPUT Average
Question 3
A INPUT Total
B INPUT Count
C IF Count = 0 THEN
D Average ← Total / Count
E ENDIF
Question 4
A INPUT Mark1
B INPUT Mark2
C Average ← Mark1 + Mark2 / 2
D OUTPUT Average
Question 5
A DECLARE Average : REAL
B Average = Total / Count
C OUTPUT "Average: ", Average

Answer Key

Calculating an average in pseudocode
Section A: Quick Questions
Q1
A — Average ← Total / Count
Q2
B — After the loop has finished
Q3
A — dividing the total by the count
Q4
B — IF Count > 0 THEN
Q5
A — DECLARE Average : REAL
Section B: Match the Pairs
Q1
Average ← Total / Count → Divides the total of all the values by how many values there were, and stores the result in Average
Q2
IF Count > 0 THEN Average ← Total / Count ENDIF → Only works out the average when at least one value was entered, so it never divides by zero
Q3
The average is calculated after the loop has finished, once Total and Count are complete → ENDWHILE Average ← Total / Count
Q4
The result of a division can have decimal places, so the variable is declared as REAL → DECLARE Average : REAL
Q5
Average ← Total + Count / Count → Without brackets, the division happens first, so only Count / Count is divided and the result is wrong. It must be Total / Count, or use brackets such as (Total + Extra) / Count if more is added.
Q6
FOR Number ← 1 TO 10 Total ← Total + Number Average ← Total / Count NEXT Number → The average is calculated inside the loop, so it is worked out before all the values have been added. It should be calculated once after NEXT, when the loop has finished.
Section C: Definitions
Q1
Average ← Total / Count — An average is the total divided by the count. Work it out after the loop ends, and only when Count is not 0. Store it in a REAL variable.
Section D: Fill in the Blanks
Q1
Average ← Total / Count
Q2
Average ← Total / Count
Q3
IF Count <> 0 THEN Average ← Total / Count ENDIF
Q4
DECLARE Average : REAL
Q5
Average ← (Total + Bonus) / Count
Section E: Spot the Error
Q1
A — An average is often not a whole number, so it must be stored in a REAL variable. It should be DECLARE Average : REAL.
Q2
B — An average is the total divided by the count, not multiplied. It should be Average ← Total / Count.
Q3
C — The division must only happen when Count is not 0, to avoid dividing by zero. The condition should be IF Count <> 0 THEN.
Q4
C — Without brackets, only Mark2 is divided by 2 because division is done before addition. It should be Average ← (Mark1 + Mark2) / 2.
Q5
B — Assignment uses the arrow ←, not =. It should be Average ← Total / Count.