IGCSE cs 0478 2023 Jun 21 11

IGCSE cs 0478 2023 Jun 21 11

A one-dimensional (1D) array Days[] contains the names of the days of the week. A two-dimensional (2D) array Readings[] is used to store 24 temperature readings, taken once an hour, for each of the seven days of the week. A 1D array AverageTemp[] is used to store the average temperature for each day of the week.

The position of any day’s data is the same in all three arrays. For example, if Wednesday is in index 4 of Days[], Wednesday’s temperature readings are in index 4 of Readings[] and Wednesday’s average temperature is in index 4 of AverageTemp[]

The temperature readings are in Celsius to one decimal place. Temperatures can only be from –20.0 °C to +50.0 °C inclusive.

Write a program that meets the following requirements:

  • input and validate the hourly temperatures for one week

  • calculate and store the average temperature for each day of the week

  • calculate the average temperature for the whole week

  • convert all the average temperatures from Celsius to Fahrenheit by using the formula Fahrenheit = Celsius * 9 / 5 + 32

  • output the average temperature in Celsius and in Fahrenheit for each day

  • output the overall average temperature in Celsius and in Fahrenheit for the whole week.

You must use pseudocode or program code and add comments to explain how your code works.

You do not need to declare any arrays, variables or constants; you may assume that this has already been done.

All inputs and outputs must contain suitable messages.

All data output must be rounded to one decimal place.

You will need to initialise and populate the array Days[] at the start of the program.

Python版初始化代码,练习的时候使用如下的代码


import string
import random

#################################################
# Declare
#################################################

reading_no = 24
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
readings = []
average_temp = []

#################################################
# Initialize Data (not include)
#################################################

readings = [[0] * 24 for i in range(7)]
average_temp = [0 for i in range(7)]

#################################################
# For you to write
#################################################

图解

Python版答案

import string
import random

#################################################
# Declare
#################################################

day_no = 7
reading_no = 24
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
readings = []
average_temp = []

#################################################
# Initialize Data (not include)
#################################################

readings = [[0] * 24 for i in range(day_no)]
average_temp = [0 for i in range(day_no)]

#################################################
# For you to write
#################################################

week_index = 0
day_index = 0
total_day = 0
total_week = 0
average_week = 0
input_temp_value = 0

for week_index in range(day_no):
    total_day = 0
    for day_index in range(reading_no):
        # input and validate the hourly temperatures
        input_temp_value = float(input("Enter temperature " + str(day_index) + " for " + days[week_index] + ": "))
        while input_temp_value < -20.0 or input_temp_value > 50.0:
            print("Your temperature must be between -20.0 and +50.0 inclusive. Please try again")
            input_temp_value = float(input("Enter temperature " + str(day_index) + "for " + days[week_index]))
        readings[week_index][day_index] = input_temp_value

        total_day = total_day + round(input_temp_value, 1)

    # calculate and store the average temperature for each day of the week
    average_temp[week_index] = round(total_day / 24, 1)

# calculate the average temperature for the whole week
for week_index in range(day_no):
    total_week = total_week + average_temp[week_index]

average_week = round(total_week / 7, 1)

# output the average temperature for each day
for week_index in range(day_no):
    print("The average temperature on ", days[week_index], " was ", average_temp[week_index], " Celsius and ",
          round(average_temp[week_index] * 9 / 5 + 32, 1), " Fahrenheit")

# output the overall average temperature for the whole week
print("The average temperature for the week was ", average_week, " Celsius and ", round(average_week * 9 / 5 + 32, 1),
      " Fahrenheit")

Last updated

Was this helpful?