Problem Description

Suppose a meteorology station records the temperature and humidity every hour of every day and stores the data for the past ten days in a text file named weather.txt (copy from shared directory).

Each line of the file consists of four numbers that indicate the day, hour, temperature, and humidity. The contents of the file could be like:

1 1 76.4 0.92
1 2 77.7 0.93
. . .
10 23 97.7 0.71
10 24 98.7 0.74

Note the lines in the file are not necessarily in increasing order of day or hour. For example, the file could appear as shown below:

5 23 77.7 0.93
9 8 77.7 0.93
. . .
5 18 77.7 0.93
2 23 77.7 0.93

Your task is to write a program that calculates the average daily temperature and humidity for the ten days. Open and read the file using an ifstream object into a three-dimensional array named data. The first index of data ranges from 0 t 9 and represents 10 days; the second index ranges from 0 to 23 and represents 24 hours; and the third index ranges from 0 to 1 and represents temperature and humidity, as depicted below.

Data [ day ] [ hour ] [ temperature|humidity ]

Note the days are numbered from 1 to 10 and the hours from 1 to 24 in the file. Because the array index starts from 0, data[0][0][0] stores the temperature for day 1 at hour 1 and data[9][23][1] stores the humidity for day 10 at hour 24.

Hints:

  • Create a three-dimensional array for storing temperature and humidity.
  • Write a loop to read data into the array.
  • Write a loop to add all temperatures for each hour in a day and all humidity for each hour.
  • Display the average daily temperature and humidity.

Sample output

Day Avg Temp Avg Hum
0 77.770800 0.929583
1 77.312500 0.929583
. . .
9 79.354200 0.912500
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.