Cambridge O Level Computer Science 2210 · 8.2 Arrays

Q49 · 2210/21/M/J/23 Q11

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

Q492210/21/M/J/23original Q11Structured · 15 marks
Question 11 from 2210/21/M/J/23

Worked solution

Use a nested loop: the outer loop goes through the 7 days, the inner loop through the 24 hours. Use the array and variable names from the question.

// set up the days of the week
Days[1] ← "Sunday"
Days[2] ← "Monday"
Days[3] ← "Tuesday"
Days[4] ← "Wednesday"
Days[5] ← "Thursday"
Days[6] ← "Friday"
Days[7] ← "Saturday"
// input and validate 24 hourly temperatures for each day
FOR WeekLoop ← 1 TO 7
    TotalDayTemp ← 0
    FOR DayLoop ← 1 TO 24
        OUTPUT "Enter temperature ", DayLoop, " for ", Days[WeekLoop]
        INPUT InTemp
        // range check, repeat until between -20.0 and 50.0
        WHILE InTemp < -20.0 OR InTemp > 50.0 DO
            OUTPUT "Temperature must be from -20.0 to 50.0. Please try again"
            INPUT InTemp
        ENDWHILE
        Readings[WeekLoop, DayLoop] ← InTemp
        TotalDayTemp ← TotalDayTemp + InTemp
    NEXT DayLoop
    // average for the day, rounded to 1 decimal place
    AverageTemp[WeekLoop] ← ROUND(TotalDayTemp / 24, 1)
NEXT WeekLoop
// average for the whole week
TotalWeekTemp ← 0
FOR WeekLoop ← 1 TO 7
    TotalWeekTemp ← TotalWeekTemp + AverageTemp[WeekLoop]
NEXT WeekLoop
AverageWeekTemp ← ROUND(TotalWeekTemp / 7, 1)
// output each day in Celsius and Fahrenheit
FOR WeekLoop ← 1 TO 7
    OUTPUT "Average on ", Days[WeekLoop], ": ", AverageTemp[WeekLoop], " Celsius, ", ROUND(AverageTemp[WeekLoop] * 9 / 5 + 32, 1), " Fahrenheit"
NEXT WeekLoop
// output the weekly average
OUTPUT "Average for the week: ", AverageWeekTemp, " Celsius, ", ROUND(AverageWeekTemp * 9 / 5 + 32, 1), " Fahrenheit"

Days[] set up with the seven day names [1]

Nested loops for 7 days and 24 hourly readings, with prompts [2]

Range check from -20.0 to 50.0 that asks again for invalid input [2]

Valid temperatures stored in Readings[] [1]

Daily total worked out, divided by 24, rounded to 1 decimal place and stored in AverageTemp[] [2]

Weekly average worked out and rounded to 1 decimal place [1]

Celsius converted to Fahrenheit with the given formula and rounded [1]

Each day output in Celsius and Fahrenheit with messages [1]

Weekly average output in Celsius and Fahrenheit with a message [1]

Meaningful identifiers used throughout [1]

Comments that explain how the code works [1]

Solution is in a logical order and does every task [1]

Other correct programs score too, for example filling Days[] with a loop and INPUT, or using a different loop type.

KEY IDEAUse nested loops for days and hours, total inside the loop, divide after it, and round every output to one decimal place.
TOTAL 15 marks

Official mark scheme

Mark scheme for 2210/21/M/J/23 question 11
VIDEO
▶ Whole paper

Video solution of the whole paper · by ZAINEMATICS. 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.