Worked solution from the Software Development Paper 2 Problem-solving and Programming booklet, Part 3 of 3.
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
ENDFUNCTIONFunction 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.
Choose four strings that each break a different rule:
| Input | Reason 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.
▶ 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.