Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q10 · 9618/22/M/J/21 Q5

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.

Q109618/22/M/J/21original Q5Structured · 15 marks
Question 5 from 9618/22/M/J/21

Worked solution

(a)(i)

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 Count

Array 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]

(a)(ii)

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.

(b)(i)

Fill in the table:

QuestionAnswer
Line number containing an initialisation statement07
Line number containing the start of a repeating block of code09
Line number containing a logic operation12
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]

(b)(ii)

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]

KEY IDEAUse a count-controlled loop for arrays, and when an element must be unique, check it against the earlier ones before keeping it.
TOTAL 15 marks

Official mark scheme

Mark scheme for 9618/22/M/J/21 question 5
VIDEO
▶ 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.