Cambridge AS Level Computer Science 9618 · 11.2 Constructs

Q57 · 9618/23/O/N/24 Q2

Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.

Q579618/23/O/N/24original Q2Structured · 7 marks
Question 2 from 9618/23/O/N/24

Worked solution

(a)

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 Total

All 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.

(b)

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.

KEY IDEAA count-controlled loop with a selection inside it, adding to a total that was set to zero first.
TOTAL 7 marks

Official mark scheme

Mark scheme for 9618/23/O/N/24 question 2
VIDEO
▶ 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.