Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
Use two loops one after the other. Each one stops when a zero is entered.
PROCEDURE TwoParts()
DECLARE NextNum, Average : REAL
TotalA ← 0.0
TotalB ← 0.0
REPEAT
INPUT NextNum
TotalA ← TotalA + NextNum
UNTIL NextNum = 0
REPEAT
INPUT NextNum
TotalB ← TotalB + NextNum
UNTIL NextNum = 0
Average ← (TotalA + TotalB) / 2
OUTPUT "The average is ", Average
ENDPROCEDUREProcedure heading and ending [1]
Local variables declared (NextNum and Average) [1]
TotalA and TotalB set to 0 at the start [1]
First conditional loop that adds to TotalA until zero is entered [1]
Second conditional loop that adds to TotalB until zero is entered [1]
Average of the two totals worked out and output with a message [1]
Write: a 1D array [1]
It holds 20 real values [1]
Write any three of these:
Many values are held under one identifier, so fewer variable names are needed [1]
The values are easier to process, search or access, for example with a loop and an index [1]
The program is easier to write, understand, maintain, test or debug [1]
▶ Starts at Q4 (27:54)
Starts at Q4 (27:54) · by Pseudocode Pro. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.