Question 1 Report
A greenhouse temperature monitoring system takes a reading every hour for 24 hours. An array Temps[0:23] stores the readings (index represents the hour).
Temps = [18, 17, 16, 15, 15, 16, 18, 20, 22, 24, 26, 28,
30, 31, 30, 28, 26, 24, 22, 20, 19, 18, 18, 17](a) Write pseudocode to find the highest and lowest temperatures and the hours they occurred. [2]
(a) The algorithm finds the highest and lowest temperatures along with the hours at which they occurred by maintaining tracking variables initialised to the first element.
DECLARE Highest : REAL
DECLARE Lowest : REAL
DECLARE HighHour : INTEGER
DECLARE LowHour : INTEGER
DECLARE h : INTEGER
Highest ← Temps[0]
Lowest ← Temps[0]
HighHour ← 0
LowHour ← 0
FOR h ← 1 TO 23
IF Temps[h] > Highest THEN
Highest ← Temps[h]
HighHour ← h
ENDIF
IF Temps[h] < Lowest THEN
Lowest ← Temps[h]
LowHour ← h
ENDIF
NEXT h
OUTPUT "Highest: ", Highest, " at ", HighHour, ":00"
OUTPUT "Lowest: ", Lowest, " at ", LowHour, ":00"Both Highest and Lowest are initialised to Temps[0] (the reading at midnight, 18 degrees). [1] The loop starts from index 1 and compares each subsequent reading. [1] When a higher temperature is found, both the value and its hour index are updated. [1] When a lower temperature is found, both Lowest and LowHour are updated. [1]
From the data: the highest temperature is 31 at hour 13 (1:00 PM). The lowest temperatures are 15 at hours 3 and 4; since the first occurrence is found first, the output is hour 3 (3:00 AM).
Everything you need to excel in your exams