Cambridge AS Level Computer Science 9618 · 12.3 Program Testing and Maintenance

Q41 · 9618/22/O/N/22 Q6

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

Q419618/22/O/N/22original Q6Structured · 11 marks
Question 6 from 9618/22/O/N/22

Worked solution

(a)

Multiply 1 × 2 × 3 ... step by step. After each multiplication, compare the running product with the parameter. If they match, the current number is the BaseNumber.

FUNCTION FindBaseNumber(ThisValue : INTEGER) RETURNS INTEGER
    DECLARE Num, Try : INTEGER
    Try ← 1
    FOR Num ← 1 TO ThisValue
        Try ← Try * Num
        IF Try = ThisValue THEN
            RETURN Num
        ENDIF
    NEXT Num
    RETURN -1
ENDFUNCTION

Function heading and ending, with the parameter and return type [1]

Local integer variables declared for Num and Try [1]

A count-controlled loop from 1 to ThisValue [1]

Try multiplied by Num inside the loop [1]

Try compared with ThisValue inside the loop [1]

Num returned straight away when they are equal [1]

-1 returned after the loop if no match was found [1]

A WHILE loop with a Found flag, stopping when Try is greater than ThisValue, also scores.

(b)

Choose four strings that each break a different rule:

InputReason for choice
"Aardvark"Not a number, and not "End"
"27.3"A number but not an integer
"-3" (or "0")An integer, but not positive and non-zero
""An empty string

One distinct invalid string with a matching reason in row 1 [1]

Row 2 [1]

Row 3 [1]

Row 4 [1]

Another valid choice is "end" in the wrong case.

KEY IDEABuild up the product one number at a time and stop when it equals or passes the target.
TOTAL 11 marks

Official mark scheme

Mark scheme for 9618/22/O/N/22 question 6
VIDEO
▶ Whole paper

Video solution of the whole paper · by Audere Prep. 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.