2210/21/M/J/24 question 10: worked solution (May/June 2024, paper 21)
Show as plain textHide plain text
Cambridge O Level Computer Science 2210 · 8.2 Arrays Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 2 of 3. Q54 2210/21/M/J/24 original Q10 Structured · 15 marks Worked solution Whole program Plan: input the number of matches and check it, then for each club input the name and results (re-asking until they add up to Matches), work out points, find the highest points, then output every club that has them. // Input the number of matches and check it is 1 to 22 REPEAT OUTPUT "Enter the number of matches played (maximum 22): " INPUT Matches IF Matches < 1 OR Matches > 22 THEN OUTPUT "Invalid. Enter a number from 1 to 22." ENDIF UNTIL Matches >= 1 AND Matches <= 22 // Loop through the 12 clubs FOR Counter ← 1 TO 12 OUTPUT "Enter the name of club ", Counter, ": " INPUT Clubs[Counter] // Repeat until won + drawn + lost equals the matches played REPEAT OUTPUT "Enter matches won, drawn and lost for ", Clubs[Counter] INPUT Won, Drawn, Lost IF Won + Drawn + Lost <> Matches THEN OUTPUT "The total must be ", Matches, ". Please try again." ENDIF UNTIL Won + Drawn + Lost = Matches // Store the results in the 2D array Statistics[Counter, 1] ← Won Statistics[Counter, 2] ← Drawn Statistics[Counter, 3] ← Lost // 12 points for a win, 5 for a draw, 0 for a loss Points[Counter] ← Won * 12 + Drawn * 5 NEXT Counter // Find the highest number of points Max ← Points[1] FOR Maximum ← 2 TO 12 IF Points[Maximum] > Max THEN Max ← Points[Maximum] ENDIF NEXT Maximum // Output every club with the highest points (this handles ties) OUTPUT "Winning club(s):" FOR OutLoop ← 1 TO 12 IF Points[OutLoop] = Max THEN OUTPUT Clubs[OutLoop], " Wins: ", Statistics[OutLoop, 1], " Points: ", Points[OutLoop] ENDIF NEXT OutLoop Number of matches input, stored and validated (maximum 22) with a prompt [1] Names of the 12 clubs input and stored in Clubs[] [1] Won, drawn and lost input and stored in the 2D array Statistics[] [1] Total of won, drawn and lost checked against Matches [1] User asked to re-enter until the total matches (loop) [1] Points calculated using 12 for a win and 5 for a draw [1] Points stored in Points[] for the correct club [1] Highest points found using a loop and comparison [1] All clubs with the highest points identified, so a tie is handled [1] Output of club name(s), number of wins and total points with suitable messages [1] Inputs and outputs given suitable prompts and messages throughout [1] Comments explain how the code works (several useful comments) [2] Meaningful identifier names used, matching the ones in the question [1] Solution is in a logical order and works for all the requirements [1] Any correct pseudocode or program code that does the same job scores. Minor syntax errors are ignored. KEY IDEA Use one loop per job: validate input, store in the arrays, total up, find the maximum, then output every match for the maximum. TOTAL 15 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by ZAINEMATICS. Tap the picture to open it on YouTube.