9618/22/M/J/21 question 5: worked solution (May/June 2021, paper 22)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q10 9618/22/M/J/21 original Q5 Structured · 15 marks 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: 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] (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 IDEA Use 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 VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.