9618/23/M/J/23 question 6: worked solution (May/June 2023, paper 23)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 11.2 Constructs Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3. Q46 9618/23/M/J/23 original Q6 Structured · 11 marks Worked solution (a) Loop from Start + 1 to End - 1. Turn each number into a string so the last two digits can be taken out and added. PROCEDURE Select(Start, End : INTEGER) DECLARE ThisNum, Total : INTEGER DECLARE ThisString : STRING DECLARE Char1, Char2 : CHAR FOR ThisNum ← Start + 1 TO End - 1 ThisString ← NUM_TO_STR(ThisNum) Char1 ← RIGHT(ThisString, 1) Char2 ← LEFT(RIGHT(ThisString, 2), 1) Total ← STR_TO_NUM(Char1) + STR_TO_NUM(Char2) IF Total = 6 THEN OUTPUT ThisString ENDIF NEXT ThisNum ENDPROCEDURE Procedure heading and ending [1] Count-controlled loop [1] Loop range correct: Start + 1 to End - 1 [1] Convert the loop counter to a string [1] Extract the last digit (inside the loop) [1] Extract the second-to-last digit (inside the loop) [1] Add the two digits to get the sum [1] If the sum is 6, output the number (as a string or an integer) inside the loop (also scores, but only seven marks are available). (b) Write: Interface - the function takes two integer parameters, the number and the required total [1] Write: ... and returns a Boolean value (for example CheckNum(Number, Total : INTEGER) RETURNS BOOLEAN) [1] Write any two advantages: CheckNum() can be called repeatedly wherever it is needed [1] It is designed and tested once and then used many times [1] Any later change to CheckNum() needs making once only, so it is easier to maintain (also scores, but only two advantage marks are available). KEY IDEA Put a repeated check in one function with parameters, so it is written, tested and changed in one place. TOTAL 11 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.