9618/21/M/J/24 question 6: worked solution (May/June 2024, paper 21)
Show as plain textHide 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. Q85 9618/21/M/J/24 original Q6 Structured · 8 marks Worked solution (a) Work out the square of each side length with the formula, then test whether one side squared equals the sum of the other two. Any of the three sides could be the longest, so test all three: FUNCTION IsRA(x1, y1, x2, y2, x3, y3 : INTEGER) RETURNS BOOLEAN DECLARE Len1, Len2, Len3 : INTEGER Len1 ← (x1 - x2) ^ 2 + (y1 - y2) ^ 2 Len2 ← (x1 - x3) ^ 2 + (y1 - y3) ^ 2 Len3 ← (x2 - x3) ^ 2 + (y2 - y3) ^ 2 IF (Len1 = Len2 + Len3) OR (Len2 = Len1 + Len3) OR (Len3 = Len1 + Len2) THEN RETURN TRUE ELSE RETURN FALSE ENDIF ENDFUNCTION Square of the length of at least one side calculated [1] All three lengths squared calculated correctly [1] One correct comparison of the squares [1] All three comparisons made [1] Comparisons combined with OR (or nested IF) correctly [1] TRUE returned in one case and FALSE in the other [1] (b) Problem: SQRT() is not exact, so the two sides of the test may not be equal and the function returns FALSE for a right-angled triangle (an incorrect value). [1] Solution: round the calculated values to a set number of decimal places, or accept the answer when the difference is smaller than a small threshold . [1] KEY IDEA Square the side lengths and compare all three combinations; inexact real numbers should never be compared with =. TOTAL 8 marks Official mark scheme VIDEO ▶ Starts at Q6 (50:02) Starts at Q6 (50:02) · by Pseudocode Pro. Tap the picture to open it on YouTube.