Cambridge O Level Computer Science 2210 · 8.2 Arrays

Q56 · 2210/22/O/N/24 Q12

Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 2 of 3.

Q562210/22/O/N/24original Q12Structured · 15 marks
Question 12 from 2210/22/O/N/24

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 IDEAValidate first, store in the arrays inside one loop, total and compare in another, then output.
TOTAL 15 marks

Official mark scheme

Mark scheme for 2210/22/O/N/24 question 12
VIDEO
▶ Topic lesson

A lesson on this topic · by Craig'n'Dave. Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.