9618/21/O/N/23 question 4: worked solution (October/November 2023, paper 21)
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. Q24 9618/21/O/N/23 original Q4 Structured · 9 marks Worked solution (a) Write a function with two parameters, count the matching strings in a loop, then compare the count with the maximum after the loop. FUNCTION TooMany(Search : STRING, Max : INTEGER) RETURNS BOOLEAN DECLARE Count, Index : INTEGER Count ← 0 FOR Index ← 1 TO 150 IF Data[Index] = Search THEN Count ← Count + 1 ENDIF NEXT Index IF Count > Max THEN RETURN TRUE ELSE RETURN FALSE ENDIF ENDFUNCTION Function heading, ending and RETURNS BOOLEAN [1] Count and Index declared as integers [1] Count set to 0 before the loop [1] Loop (any type) that runs 150 times [1] Each element of Data compared with the search string, and Count increased if equal, inside the loop [1] Count compared with Max after the loop, returning TRUE or FALSE in both cases [1] (b) Check the row is even with MOD, and check both columns of that row. Both conditions go in one IF with AND. IF Row MOD 2 = 0 AND (Data[Row, 1] = Search OR Data[Row, 2] = Search) THEN Test that the row is an even number, for example Row MOD 2 = 0 [1] Test whether either column equals the search string, using OR [1] One correctly formed selection: a single IF with AND, or two nested IFs, or a loop using STEP 2 from row 2 [1] KEY IDEA Count matches in a loop and make the final comparison and return only after the loop has finished. TOTAL 9 marks Official mark scheme VIDEO ▶ Starts at Q4 (19:01) Starts at Q4 (19:01) · by Pseudocode Pro. Tap the picture to open it on YouTube.