9618/22/M/J/21 question 6: worked solution (May/June 2021, paper 22)

Show as 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. Q11 9618/22/M/J/21 original Q6 Structured · 8 marks Worked solution Use a CharCount array of six elements: 1 to 5 for a, e, i, o, u and 6 for the other letters. Set all to 0, convert each character to lower case, use CASE to add 1 to the right count, then output all six. PROCEDURE CountVowels(ThisString : STRING) DECLARE Index : INTEGER DECLARE ThisChar : CHAR FOR Index ← 1 TO 6 CharCount[Index] ← 0 NEXT Index FOR Index ← 1 TO LENGTH(ThisString) ThisChar ← LCASE(MID(ThisString, Index, 1)) CASE OF ThisChar 'a' : CharCount[1] ← CharCount[1] + 1 'e' : CharCount[2] ← CharCount[2] + 1 'i' : CharCount[3] ← CharCount[3] + 1 'o' : CharCount[4] ← CharCount[4] + 1 'u' : CharCount[5] ← CharCount[5] + 1 'a' TO 'z' : CharCount[6] ← CharCount[6] + 1 ENDCASE NEXT Index FOR Index ← 1 TO 6 OUTPUT CharCount[Index] NEXT Index ENDPROCEDURE Procedure heading with the string parameter, and ENDPROCEDURE [1] Local variable declared for the loop counter (CharCount is not declared, as it is global) [1] All elements of CharCount set to 0 [1] Loop through every character of ThisString [1] MID() used to take out a single character [1] Each vowel tested (upper or lower case) and its count increased [1] Other letters tested and the consonant count increased [1] Results output after the loop [1] KEY IDEA Convert each character to one case, then use CASE to add to the matching element of a count array; put the catch-all range last. TOTAL 8 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.