Question 1
15 marksA weather station records the temperature, in °C, every hour for one week. The readings are stored in the 2D array Temperature[1:7, 1:24] of type REAL, where Temperature[2, 13] is the reading at hour 13 of day 2. The 1D array DayName[1:7] stores the names of the days, "Monday" to "Sunday". Both arrays have already been declared and contain the data.
Write a program that:
- finds, for each day, the highest and lowest temperature and calculates the average temperature
- outputs, for each day, the day name and its highest, lowest and average temperature, with the average rounded to one decimal place
- finds and outputs the name of the day with the greatest range of temperatures (highest minus lowest)
- counts and outputs the number of readings in the week that were below 0 °C
- then allows the user to input a day number, validated to be from 1 to 7, and outputs all 24 readings for that day with their hour numbers.
You must use pseudocode or program code and add comments to explain how your code works. All inputs and outputs must contain suitable messages. Declare any variables you use.
Model answer
// Temperature and DayName are already declared and contain the data
DECLARE Day, Hour, FreezingCount, WidestDay, Choice : INTEGER
DECLARE High, Low, Total, Range, WidestRange : REAL
FreezingCount ← 0
WidestRange ← -1.0
WidestDay ← 1
FOR Day ← 1 TO 7
// Start each day with its first reading as both highest and lowest
High ← Temperature[Day, 1]
Low ← Temperature[Day, 1]
Total ← 0.0
FOR Hour ← 1 TO 24
IF Temperature[Day, Hour] > High
THEN
High ← Temperature[Day, Hour]
ENDIF
IF Temperature[Day, Hour] < Low
THEN
Low ← Temperature[Day, Hour]
ENDIF
IF Temperature[Day, Hour] < 0
THEN
FreezingCount ← FreezingCount + 1
ENDIF
Total ← Total + Temperature[Day, Hour]
NEXT Hour
OUTPUT DayName[Day], ": highest ", High, ", lowest ", Low, ", average ", ROUND(Total / 24, 1)
// Keep the day with the greatest range
Range ← High - Low
IF Range > WidestRange
THEN
WidestRange ← Range
WidestDay ← Day
ENDIF
NEXT Day
OUTPUT "Day with the greatest range: ", DayName[WidestDay]
OUTPUT "Number of readings below 0 °C: ", FreezingCount
// Show all the readings for a day chosen by the user
OUTPUT "Enter a day number (1 to 7)"
INPUT Choice
WHILE Choice < 1 OR Choice > 7 DO
OUTPUT "The day number must be from 1 to 7. Enter it again"
INPUT Choice
ENDWHILE
FOR Hour ← 1 TO 24
OUTPUT "Hour ", Hour, ": ", Temperature[Choice, Hour], " °C"
NEXT Hour
Explanation
The highest and lowest are reset to the day's first reading at the start of each day, so every day is judged on its own readings; starting them at 0 would fail on a day that is entirely below or above 0 °C. The freezing count, by contrast, is set once before the loops because it covers the whole week. The final part validates the day number before it is used as an index, so the program can never try to read a row outside the array.
Mark scheme
-
1 mark
MP1 declares the variables used with suitable data types
Mark as Cambridge: credit each feature that is present and correctly used in a working solution; a solution in pseudocode, Python, Visual Basic or Java is accepted. max 15
-
1 mark
MP2 initialises the freezing counter to 0 and the greatest range (e.g. to -1 or the first day's range) before the loops
-
1 mark
MP3 outer loop for the 7 days
-
1 mark
MP4 sets the highest and lowest to the first reading of the day (or suitable extreme values) and the total to 0 for each day
-
1 mark
MP5 inner loop for the 24 hours, using Temperature[Day, Hour] with correct indexes
-
1 mark
MP6 updates the highest when a reading is greater than it
-
1 mark
MP7 updates the lowest when a reading is less than it
-
1 mark
MP8 adds each reading to the day's total
-
1 mark
MP9 counts readings below 0
-
1 mark
MP10 calculates the average as total ÷ 24 after the inner loop, rounded to 1 decimal place
-
1 mark
MP11 outputs the day name with its highest, lowest and average temperatures
-
1 mark
MP12 calculates each day's range as highest - lowest
-
1 mark
MP13 compares the range with the greatest so far and stores the day (index or name) of the greatest
-
1 mark
MP14 outputs the name of the day with the greatest range and the freezing count after the loops
-
1 mark
MP15 inputs a day number with a prompt
-
1 mark
MP16 validates the day number is between 1 and 7, re-inputting with an error message until valid
-
1 mark
MP17 loops through the 24 hours outputting each hour number and reading for the chosen day
-
1 mark
MP18 comments explain the code and identifiers are meaningful