Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
Declare the array, loop 100 times, store a random number from 1 to 200, count the ones from 66 to 173, then output the count after the loop. RAND(200) gives a real number from 0 up to (not including) 200, so INT(RAND(200)) + 1 gives 1 to 200.
DECLARE RNum : ARRAY[1:100] OF INTEGER
DECLARE Index, Count : INTEGER
Count ← 0
FOR Index ← 1 TO 100
RNum[Index] ← INT(RAND(200)) + 1
IF RNum[Index] >= 66 AND RNum[Index] <= 173 THEN
Count ← Count + 1
ENDIF
NEXT Index
OUTPUT CountArray declared correctly [1]
Loop that runs 100 times [1]
Array element accessed with the loop index on the left of the assignment, inside the loop [1]
RAND() used to make a value in the range 1 to 200 and assigned to the element [1]
Checks whether the number is from 66 to 173 and adds 1 to Count if so, inside the loop [1]
Count output after the loop [1]
Describe these changes (any three of these points score):
Set every element to a rogue value first (such as 0) to show it is unused [1]
Use a conditional loop for each element [1]
Check the stored number against the values already in the array [1]
Other points that also score: generate a random number in the range 1 to 200 and store it; if it is already there, generate another random value; otherwise keep it in the array and leave the loop.
Fill in the table:
| Question | Answer |
|---|---|
| Line number containing an initialisation statement | 07 |
| Line number containing the start of a repeating block of code | 09 |
| Line number containing a logic operation | 12 |
| Number of parameters to MID() | 3 |
Initialisation statement: line 07 [1]
Start of the repeating block: line 09 (line 10 is also accepted) [1]
Logic operation: line 12 [1]
MID() has 3 parameters [1]
NOT((a < 'a') OR (a > 'z')) means the character is not below a and not above z. Rewrite it as a character that is at least a AND at most z:
IF (NextChar >= 'a') AND (NextChar <= 'z') THEN
Uses IF ... AND ... [1]
Both conditions correct: NextChar >= 'a' and NextChar <= 'z' [1]
▶ Whole paper
Video solution of the whole paper · by Papersdock. 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.