Cambridge O Level Computer Science 2210 · 8.2 Arrays

Q53 · 2210/23/O/N/23 Q10

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

Q532210/23/O/N/23original Q10Structured · 15 marks
Question 10 from 2210/23/O/N/23

Worked solution

Loop through each day (column) and, inside it, each hour (row). Comment each block. This is one correct answer:

// Temperatures[Hour, Day]: 24 hours (rows) by 7 days (columns, Monday to Sunday)
CONSTANT Hours ← 24
CONSTANT Days ← 7
// start the week values so any real reading replaces them
MaxWeek ← -1000
MinWeek ← 1000
WeekTotal ← 0
FOR Day ← 1 TO Days
    // reset the values for each new day
    MaxDay ← -1000
    MinDay ← 1000
    DayTotal ← 0
    FOR Hour ← 1 TO Hours
        // add to the day total
        DayTotal ← DayTotal + Temperatures[Hour, Day]
        // check for a new maximum or minimum for the day
        IF Temperatures[Hour, Day] > MaxDay
            THEN
                MaxDay ← Temperatures[Hour, Day]
        ENDIF
        IF Temperatures[Hour, Day] < MinDay
            THEN
                MinDay ← Temperatures[Hour, Day]
        ENDIF
    NEXT Hour
    AvDay ← DayTotal / Hours
    // choose the name of the day
    CASE OF Day
        1 : DayName ← "Monday"
        2 : DayName ← "Tuesday"
        3 : DayName ← "Wednesday"
        4 : DayName ← "Thursday"
        5 : DayName ← "Friday"
        6 : DayName ← "Saturday"
        7 : DayName ← "Sunday"
    ENDCASE
    // output the results for the day, rounded to 2 decimal places
    OUTPUT DayName
    OUTPUT "Maximum temperature: ", ROUND(MaxDay, 2)
    OUTPUT "Minimum temperature: ", ROUND(MinDay, 2)
    OUTPUT "Average temperature: ", ROUND(AvDay, 2)
    // update the week maximum, minimum and total
    IF MaxDay > MaxWeek
        THEN
            MaxWeek ← MaxDay
    ENDIF
    IF MinDay < MinWeek
        THEN
            MinWeek ← MinDay
    ENDIF
    WeekTotal ← WeekTotal + DayTotal
NEXT Day
AvWeek ← WeekTotal / (Hours * Days)
// output the results for the week
OUTPUT "Maximum temperature for the week: ", ROUND(MaxWeek, 2)
OUTPUT "Minimum temperature for the week: ", ROUND(MinWeek, 2)
OUTPUT "Average temperature for the week: ", ROUND(AvWeek, 2)

Nested loops that read every reading for all 7 days and 24 hours [2]

Day maximum and minimum found with IF, and reset for each day [2]

Day total added up and the day average worked out [2]

Name of the day chosen (for example with CASE) [1]

Day name, maximum, minimum and average output with messages, rounded to 2 decimal places [2]

Week maximum and minimum updated from the day values [2]

Week total added up and the week average worked out [2]

Week maximum, minimum and average output with messages, rounded to 2 decimal places [1]

Comments and meaningful identifiers (MaxDay, MinDay, AvDay, MaxWeek, MinWeek, AvWeek) used [1]

Other correct approaches also score, for example a WHILE loop or 0-based loops.

KEY IDEAUse a loop inside a loop: the inner loop finds the maximum, minimum and total for one day, the outer loop repeats for each day and builds the week results.
TOTAL 15 marks

Official mark scheme

Mark scheme for 2210/23/O/N/23 question 10
VIDEO
▶ Whole paper

Video solution of the whole paper · by O/Alevel CS by Wasiq. 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.