Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.
Use a loop that runs 100 times, add a number to the total only when it is above zero, and output the total after the loop.
DECLARE Count, Total, NextNumber : INTEGER
Total ← 0
FOR Count ← 1 TO 100
OUTPUT "Input an integer value"
INPUT NextNumber
IF NextNumber > 0 THEN
Total ← Total + NextNumber
ENDIF
NEXT Count
OUTPUT TotalAll variables declared [1]
Loop that runs 100 times [1]
Prompt and input a value inside the loop [1]
Test for the value being greater than 0 inside the loop [1]
Total updated by adding the value inside the loop [1]
The output of Total after the loop also scores. Any five of the six points get the 5 marks.
Write:
Construct: iteration (repetition) [1]
Use: to loop through all 100 inputs, so the code runs 100 times [1]
Alternative: selection, used to test whether each value input is positive.
▶ Whole paper
Video solution of the whole paper · by Zafar Ali Khan (ZAK). 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.