IGCSE cs 0478 2023 Nov 23 10

IGCSE cs 0478 2023 Nov 23 10

A weather station takes temperature readings once an hour for a week. These temperatures are stored in a two-dimensional (2D) array Temperatures[] Each column contains 24 readings for a single day. The first temperature is recorded at 00:00 and the final temperature at 23:00. There are seven columns, one for each day of the week, starting with Monday and ending with Sunday.

The variables MaxDay, MinDay and AvDay are used to store the maximum, minimum, and average temperatures for a day. The variables MaxWeek, MinWeek and AvWeek are used to store the maximum, minimum, and average temperatures for the week.

The array has already been set up and the data stored.

Write a program that meets the following requirements:

  • finds the maximum and minimum temperatures for each day

  • calculates the average temperature for each day

  • outputs for each day:

    • name of the day, for example Monday

    • maximum temperature

    • minimum temperature

    • average temperature

  • finds the maximum and minimum temperatures for the week

  • calculates the average temperature for the week

  • outputs:

    • maximum temperature for the week

    • minimum temperature for the week

    • average temperature for the week.

All temperatures output must be rounded to two decimal places.

You must use pseudocode or program code and add comments to explain how your code works. All inputs and outputs must contain suitable messages.

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

You do not need to initialise the data in the array Temperatures[]

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

import string
import random

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

HOURS_NUM = 24
WEEK_DAY_NUM = 7
temperatures = []

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

for temperature_index in range(WEEK_DAY_NUM):
    temperature_array = [random.randint(120, 400) / 10 for i in range(HOURS_NUM)]
    temperatures.append(temperature_array)

print(temperatures)

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

图解

Python版答案


import string
import random

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

HOURS_NUM = 24
WEEK_DAY_NUM = 7
temperatures = []

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

for temperature_index in range(WEEK_DAY_NUM):
    temperature_array = [random.randint(120, 400) / 10 for i in range(HOURS_NUM)]
    temperatures.append(temperature_array)

print(temperatures)

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

max_day = 0
min_day = 0
av_day = 0
max_week = -1000
min_week = 1000
week_total = 0
av_week = 0

for day_index in range(WEEK_DAY_NUM):
    max_day = temperatures[day_index][0]  # initialise max and min temperatures and total for each day
    min_day = temperatures[day_index][0]
    day_total = 0
    for hour_index in range(HOURS_NUM):
        day_total = day_total + temperatures[day_index][hour_index]
        # update total maximum and minimum
        if temperatures[day_index][hour_index] > max_day:
            max_day = temperatures[day_index][hour_index]
        if temperatures[day_index][hour_index] < min_day:
            min_day = temperatures[day_index][hour_index]

    day_name = ""
    # select message for day
    if day_index == 0:
        day_name = "Monday"
    elif day_index == 1:
        day_name = "Tuesday"
    elif day_index == 2:
        day_name = "Wednesday"
    elif day_index == 3:
        day_name = "Thursday"
    elif day_index == 4:
        day_name = "Friday"
    elif day_index == 5:
        day_name = "Saturday"
    elif day_index == 6:
        day_name = "Sunday"

    # output results for day
    av_day = day_total / HOURS_NUM
    print(day_name)
    print("Maximum temperature ", max_day)
    print("Minimum temperature ", min_day)
    print("Average temperature ", round(av_day, 2))

    # update total maximum and minimum
    if max_day > max_week:
        max_week = max_day
    if min_day < min_week:
        min_week = min_day

    # update total for week
    week_total = week_total + av_day

av_week = week_total / WEEK_DAY_NUM

# output results for week
print("Maximum temperature for week ", max_week)
print("Minimum temperature for week ", min_week)
print("Average temperature for Week ", round(av_week, 2))

Last updated

Was this helpful?