0% found this document useful (0 votes)
6 views3 pages

Weekly Temperature Input and Averages

The document outlines a program for inputting, validating, and processing hourly temperature readings over a week. It calculates daily average temperatures in both Celsius and Fahrenheit, as well as an overall weekly average. The program includes error handling for temperature input and provides formatted output for daily and weekly averages.

Uploaded by

patiencechidemo3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Weekly Temperature Input and Averages

The document outlines a program for inputting, validating, and processing hourly temperature readings over a week. It calculates daily average temperatures in both Celsius and Fahrenheit, as well as an overall weekly average. The program includes error handling for temperature input and provides formatted output for daily and weekly averages.

Uploaded by

patiencechidemo3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// CONSTANTS: MIN_TEMP = -20.0, MAX_TEMP = 50.

0, HOURS_PER_DAY = 24,

DAYS_IN_WEEK = 7 // ARRAYS:

Days[7] (String), Readings[7][24] (Real), AverageTemp[7] (Real)

// VARIABLES: temp_celsius (Real), day_sum (Real), overall_average_c (Real) //


total_weekly_sum_of_daily_averages (Real), is_valid (Boolean)

// ----------------------------------------------------------------------

// 1. INPUT AND VALIDATE HOURLY TEMPERATURES FOR ONE WEEK // -----------


-----------------------------------------------------------

OUTPUT "--- Hourly Temperature Input (Celsius) ---" SET


total_weekly_sum_of_daily_averages TO 0.0

FOR i FROM 0 TO DAYS_IN_WEEK - 1 DO //

Get the name of the current day for messaging day_name = Days[i] OUTPUT "" OUTPUT "-
-- Entering " & HOURS_PER_DAY & " temperatures for " & day_name & " ---"

FOR j FROM 0 TO HOURS_PER_DAY - 1 DO


SET is_valid TO FALSE

WHILE is_valid IS FALSE DO


// Prompt the user for input with the required
temperature range
OUTPUT " Enter temperature for hour " & j & " (" &
MIN_TEMP & "°C to " & MAX_TEMP & "°C): "
INPUT temp_celsius

// Check if the input is within the allowed inclusive


range
IF temp_celsius >= MIN_TEMP AND temp_celsius <=
MAX_TEMP THEN
// Input is valid, store it in the Readings array
Readings[i][j] = temp_celsius
SET is_valid TO TRUE // Exit the validation loop
ELSE
// Output a suitable error message if validation
fails
OUTPUT " ERROR: Temperature must be between " &
MIN_TEMP & "°C and " & MAX_TEMP & "°C. Please try again."
END IF
END WHILE
END FOR
END FOR

// ---------------------------------------------------------------------- // 2 & 3. CALCULATE AND


STORE DAILY AVERAGES, AND CALCULATE WEEKLY AVERAGE // ------------------
----------------------------------------------------

FOR i FROM 0 TO DAYS_IN_WEEK - 1 DO SET day_sum TO 0.0

// Calculate the sum of 24 hourly readings for the current day


FOR j FROM 0 TO HOURS_PER_DAY - 1 DO
day_sum = day_sum + Readings[i][j]
END FOR

// Calculate the daily average temperature


daily_average_c = day_sum / HOURS_PER_DAY

// Store the daily average (Celsius) in the AverageTemp array


AverageTemp[i] = daily_average_c

// Accumulate the daily averages to calculate the overall


weekly average later
total_weekly_sum_of_daily_averages =
total_weekly_sum_of_daily_averages + daily_average_c

END FOR

// Calculate the overall average temperature for the whole week overall_average_c =
total_weekly_sum_of_daily_averages / DAYS_IN_WEEK (7)

// ---------------------------------------------------------------------- // 4. FUNCTION FOR


CONVERSION FROM CELSIUS TO FAHRENHEIT // -------------------------------------------
---------------------------

FUNCTION C_TO_F(celsius : Real) RETURNS Real // Apply the conversion formula:


Fahrenheit = Celsius * 9/5 + 32

RETURN (celsius * (9 / 5)) + 32

END FUNCTION

// ---------------------------------------------------------------------- //

5. OUTPUT DAILY AVERAGE TEMPERATURES (Celsius and Fahrenheit) // ---------------


-------------------------------------------------------

OUTPUT "" OUTPUT


"============================================================"
OUTPUT " SUMMARY OF DAILY AVERAGE TEMPERATURES" OUTPUT
"============================================================"
FOR i FROM 0 TO DAYS_IN_WEEK - 1 DO day_name = Days[i] avg_c =
AverageTemp[i]

// Convert the daily Celsius average to Fahrenheit


avg_f = C_TO_F(avg_c)

// Output the daily average temperature in Celsius and


Fahrenheit (to 1 decimal place)
OUTPUT " " & day_name & " Average:"
OUTPUT " Celsius: " & FORMAT(avg_c, 1) & "°C" //
FORMAT(value, 1) represents rounding to one decimal place
OUTPUT " Fahrenheit: " & FORMAT(avg_f, 1) & "°F"
OUTPUT "--------------------"

END FOR

// ---------------------------------------------------------------------- // 6. OUTPUT OVERALL


AVERAGE TEMPERATURE (Celsius and Fahrenheit) // ------------------------------------------
----------------------------

// Convert the overall weekly average from Celsius to Fahrenheit overall_average_f =


C_TO_F(overall_average_c)

OUTPUT "" OUTPUT


"============================================================"
OUTPUT " OVERALL WEEKLY AVERAGE TEMPERATURE" OUTPUT
"============================================================"

// Output the final overall weekly average in Celsius and Fahrenheit OUTPUT "Overall
Weekly Average Temperature:" OUTPUT " Celsius: " & FORMAT(overall_average_c, 1) &
"°C" OUTPUT " Fahrenheit: " & FORMAT(overall_average_f, 1) & "°F" OUTPUT
"============================================================"

You might also like