9618/22/M/J/23 question 4: worked solution (May/June 2023, paper 22)

Show as plain text

Cambridge AS Level Computer Science 9618 · 11.3 Structured Programming Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 3 of 3. Q79 9618/22/M/J/23 original Q4 Structured · 6 marks Worked solution Count one match at a time. Compare each character with the parameter, so the check is case sensitive. FUNCTION GetNum(ThisString : STRING, ThisChar : CHAR) RETURNS INTEGER DECLARE Index, Count : INTEGER Count ← 0 FOR Index ← 1 TO LENGTH(ThisString) IF MID(ThisString, Index, 1) = ThisChar THEN Count ← Count + 1 ENDIF NEXT Index RETURN Count ENDFUNCTION Function heading and ending including the parameters and return type [1] Local INTEGER variables declared for Index and Count [1] Loop for the length of ThisString [1] Character extracted and compared with the parameter inside the loop [1] Count incremented when they match [1] Count returned after the loop [1] KEY IDEA Loop over the string, compare each character with the target and add one to a counter for each match. TOTAL 6 marks Official mark scheme VIDEO ▶ Starts at Q4 (27:45) Starts at Q4 (27:45) · by Pseudocode Pro. Tap the picture to open it on YouTube.