2210/22/O/N/24 question 12: worked solution (October/November 2024, paper 22)
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. Q56 2210/22/O/N/24 original Q12 Structured · 15 marks Worked solution Whole program Plan: validate Number (3 to 20), then loop to input each room, store the area, total the areas, find the largest and smallest, and output everything. // Input and validate the number of rooms (3 to 20) REPEAT OUTPUT "How many rooms (3 to 20)? " INPUT Number IF Number < 3 OR Number > 20 THEN OUTPUT "Invalid. The number must be from 3 to 20." ENDIF UNTIL Number >= 3 AND Number <= 20 // Input each room's data FOR Index ← 1 TO Number OUTPUT "Enter the name of room ", Index, ": " INPUT Rooms[Index] OUTPUT "Enter the length in metres: " INPUT Dimensions[Index, 1] OUTPUT "Enter the width in metres: " INPUT Dimensions[Index, 2] // Area = length * width, rounded to 2 decimal places Dimensions[Index, 3] ← ROUND(Dimensions[Index, 1] * Dimensions[Index, 2], 2) NEXT Index // Total area and starting largest / smallest TotalArea ← 0 Largest ← 1 Smallest ← 1 FOR Index ← 1 TO Number TotalArea ← TotalArea + Dimensions[Index, 3] IF Dimensions[Index, 3] > Dimensions[Largest, 3] THEN Largest ← Index ENDIF IF Dimensions[Index, 3] < Dimensions[Smallest, 3] THEN Smallest ← Index ENDIF NEXT Index AverageArea ← ROUND(TotalArea / Number, 2) // Output all room details FOR Index ← 1 TO Number OUTPUT "Room: ", Rooms[Index] OUTPUT "Length: ", Dimensions[Index, 1], " m" OUTPUT "Width: ", Dimensions[Index, 2], " m" OUTPUT "Area: ", Dimensions[Index, 3], " square metres" NEXT Index OUTPUT "Largest room: ", Rooms[Largest] OUTPUT "Smallest room: ", Rooms[Smallest] OUTPUT "Total area of the house: ", TotalArea, " square metres" OUTPUT "Average room area: ", AverageArea, " square metres" Number of rooms input with a prompt and validated for 3 to 20 using a loop [2] Room name, length and width input in a loop and stored in Rooms[] and Dimensions[] [2] Area calculated as length * width, rounded to 2 decimal places and stored [2] Total area and average area calculated, average rounded to 2 decimal places [2] Largest and smallest room found by area using comparisons [2] All required outputs (every room's name, dimensions and area, largest, smallest, total, average) with suitable messages [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 meets all the requirements [1] Any correct pseudocode or program code that does the same job scores. Minor syntax errors are ignored. KEY IDEA Validate first, store in the arrays inside one loop, total and compare in another, then output. TOTAL 15 marks Official mark scheme VIDEO ▶ Topic lesson A lesson on this topic · by Craig'n'Dave. Tap the picture to open it on YouTube.