Programming Constructs and Operators: Question 1
Syllabus 8.1
A conference organiser uses this pseudocode to work out how many minibuses are needed to transport delegates to a venue. Each minibus seats exactly 9 people.
DECLARE Delegates : INTEGER
DECLARE FullBuses : INTEGER
DECLARE Remaining : INTEGER
Delegates ← 47
FullBuses ← Delegates DIV 9
Remaining ← Delegates MOD 9
OUTPUT FullBuses, " full minibuses and ", Remaining, " delegates left over"
What is output when this algorithm is run?
Show worked solution Hide worked solution
Worked solution
Step 1: Recall what DIV and MOD do
In Cambridge pseudocode, for two integers A and B:
A DIV Bgives the whole number of timesBdivides intoA, with any remainder discarded (integer division, always rounded down towards zero for positive integers).A MOD Bgives the remainder left over afterA DIV Bwhole groups ofBhave been removed fromA.
Both operators use the same two operands, Delegates (47) and 9, so they must be worked out
from the same pair of numbers.
Step 2: Work out FullBuses
FullBuses ← Delegates DIV 9
Find the largest whole number of 9s that fit into 47:
9 × 5 = 45(fits, since 45 is less than or equal to 47)9 × 6 = 54(too big, since 54 is greater than 47)
So 47 DIV 9 = 5. There are 5 complete minibuses.
Step 3: Work out Remaining
Remaining ← Delegates MOD 9
This is what is left over after removing those 5 complete groups of 9:
47 - (5 × 9) = 47 - 45 = 2
So 47 MOD 9 = 2. There are 2 delegates left over after filling the 5 full minibuses.
Step 4: Confirm the output
The OUTPUT statement prints FullBuses, the fixed text " full minibuses and ", Remaining,
then " delegates left over". Substituting the traced values gives:
5 full minibuses and 2 delegates left over
Step 5: Why the other options are wrong
- Option B (6 full minibuses and 2 delegates left over) comes from doing ordinary division,
47 ÷ 9 = 5.22, and rounding this up to 6, but
DIVtruncates rather than rounds, so it must give exactly 5, not 6. - Option C (2 full minibuses and 5 delegates left over) comes from swapping the two
operators: computing
FullBuseswithMOD(giving 2) andRemainingwithDIV(giving 5). The pseudocode clearly assignsDIVtoFullBusesandMODtoRemaining. - Option D (5 full minibuses and 47 delegates left over) comes from misunderstanding
MODas simply returning the original value ofDelegatesunchanged, instead of the true remainder of 2 after 5 whole groups of 9 have been removed.
Final answer
- The algorithm outputs “5 full minibuses and 2 delegates left over”, option A.