9618/22/O/N/23 question 4: worked solution (October/November 2023, paper 22)

Show as plain text

Cambridge AS Level Computer Science 9618 · 12.3 Program Testing and Maintenance Worked solution from the Software Development Paper 2 Problem-solving and Programming booklet, Part 3 of 3. Q47 9618/22/O/N/23 original Q4 Structured · 9 marks Worked solution (a) Count odd and even numbers in a loop until 99 is read. Read the first value before the loop, so 99 is never counted. PROCEDURE Count() DECLARE COdd, CEven, ThisNum : INTEGER COdd ← 0 CEven ← 0 INPUT ThisNum WHILE ThisNum <> 99 IF ThisNum MOD 2 = 1 THEN COdd ← COdd + 1 ELSE CEven ← CEven + 1 ENDIF INPUT ThisNum ENDWHILE OUTPUT "Count of odd and even numbers: ", COdd, CEven ENDPROCEDURE Procedure heading and ending [1] Conditional loop that stops when ThisNum is 99 [1] Input of ThisNum inside the loop [1] Check inside the loop whether ThisNum is odd or even [1] Correct count incremented, with both counts set to 0 before the loop [1] After the loop, output both counts with a suitable message [1] Declaring COdd, CEven and ThisNum as integers also earns a mark, but the maximum is 6. A REPEAT loop that does not count 99 is also accepted. (b) Choose three sequences that test different things. Each must end with 99. Any three of these: 3, 7, 11, 99 - only odd values, to check the even count stays 0 [1] 2, 8, 100, 99 - only even values, to check the odd count stays 0 [1] 99 - no values before the end marker, to check both counts are 0 [1] Other accepted tests: the same number of odd and even values, just one odd or even value among the others, or a sequence with a missing or misplaced 99. KEY IDEA Read before the loop and again at the end of it, so the end value is tested but never counted. TOTAL 9 marks Official mark scheme VIDEO ▶ Starts at Q4 (20:57) Starts at Q4 (20:57) · by Pseudocode Pro. Tap the picture to open it on YouTube.