9618/21/O/N/21 question 2: worked solution (October/November 2021, paper 21)

Show as plain text

Cambridge AS Level Computer Science 9618 · 9.2 Algorithms Worked solution from the Algorithm Design and Problem-solving Paper 2 Problem-solving and Programming booklet, Part 2 of 2. Q16 9618/21/O/N/21 original Q2 Structured · 12 marks Worked solution (a) Refine the two main steps into smaller steps, using structured English only (no pseudocode statements): Use a variable as an index (position) to point at the elements of the array [1] Repeat the whole pass over the array until it is sorted, with an outer loop that goes round once for each element [1] Inside it, use an inner loop that goes through the array from the first element, and stops one place earlier each time because the largest values are already at the end [1] Compare the element at the current position with the next element [1] If the current element is greater than the next one, swap them [1] Describe the swap: copy one value to a temporary place, put the other value in its position, then put the temporary value in the other position [1] A more efficient algorithm that sorts the array correctly is also credited, but only 6 marks can be given. (b) Follow the flowchart box by box. The loop has two conditions, so use a WHILE loop: Count ← 1 Flag ← FALSE WHILE Flag = FALSE AND Count <= 5 CALL ReBoot() Count ← Count + 1 Flag ← Check() ENDWHILE IF Flag = FALSE THEN CALL Alert(27) ENDIF Count and Flag both set before the loop [1] A WHILE ... ENDWHILE loop (or REPEAT ... UNTIL) [1] The loop condition includes both Flag = FALSE and Count <= 5 [1] ReBoot() is called and Count is increased inside the loop [1] The value returned by Check() is assigned to Flag inside the loop [1] After the loop, Flag is tested and Alert(27) is called, outside the loop [1] KEY IDEA Turn each flowchart box into one statement; a loop with two conditions is a WHILE with AND. TOTAL 12 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Mr Saem. Tap the picture to open it on YouTube.