Question: Here is the program in C++ about Weather Statistics. This program uses a structure to store the following weather data for a particular month Total
Here is the program in C++ about Weather Statistics. This program uses a structure to store the following weather data for a particular month
Total Rainfall
High Temperature
Low Temperature
Average Temperature
The program uses an array of 12 structures to hold weather data for an entire year. When the program runs, it asks the user to enter data (Total Rainfall, High Temperature, Low Temperature) for each month. (The average temperature should be calculated) Once the data are entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year and (the months they occurred in), and the average of all the monthly average temperatures.
Note: the temperature should be in the range [-100, 140]
Please revise this program as following:
When user input high temperature and low temperature for each month, the high temperature should be higher or equal to low temperature, if not, the program will display ERROR: high temperature should be higher or equal to the low temperature and asks user to input high temperature and low temperature again, if the new high temperature is still lower than the low temperature, the program will display ERROR: high temperature should be higher or equal to the low temperature and asks user to input high temperature and low temperature again, until the current high temperature is higher or equal to the low temperature, the program will continue to execute the next statement.
Note: the temperature should be in the range [-100, 140]
Here is the original program:
// Weather Statistics
#include
using namespace std;
// Constant for the number of months
const int NUM_MONTHS = 12;
// Declaration of the WeatherInfo structure
struct WeatherInfo
{
double rain; // Total rainfall
double high; // High temperature
double low; // Low temperature
double averageTemp; // Average temperature
};
int main()
{
// Create an array of WeatherInfo structures
WeatherInfo year[NUM_MONTHS];
int index = 0; // Loop counter
// Get the weather data for each month.
cout << "Enter the following information: ";
for (index = 0; index < NUM_MONTHS; index++)
{
// Get the rainfall.
cout << "Month " << (index + 1) << endl;
cout << "\tTotal Rainfall: ";
cin >> year[index].rain;
// Get the high temperature.
cout << "\tHigh Temperature: ";
cin >> year[index].high;
// Validate the high temperature.
while (year[index].high < -100 || year[index].high > 140)
{
cout << "ERROR: Temperature must be in the range "
<< "of -100 through 140. ";
cout << "\tHigh Temperature: ";
cin >> year[index].high;
}
// Get the low temperature.
cout << "\tLow Temperature: ";
cin >> year[index].low;
// Validate the high temperature.
while (year[index].low < -100 || year[index].low > 140)
{
cout << "ERROR: Temperature must be in the range "
<< "of -100 through 140. ";
cout << "\tLow Temperature: ";
cin >> year[index].low;
}
// Calculate the average temperature.
year[index].averageTemp =
(year[index].high + year[index].low) / 2.0;
}
// Calculate total annual rainfall
double totalRain = 0;
for (index = 0; index < NUM_MONTHS; index++)
totalRain += year[index].rain;
// Calculate average monthly rainfall
double aveMonthRain = totalRain / NUM_MONTHS;
// Calculate the average monthly average temperature
double aveTotal = 0, aveAve;
for (index = 1; index < NUM_MONTHS; index++)
aveTotal += year[index].averageTemp;
aveAve = aveTotal / NUM_MONTHS;
// Find the highest & lowest temperatures
double highest, lowest, highMonth = 0, lowMonth = 0;
highest = year[0].high;
lowest = year[0].low;
for (index = 1; index < NUM_MONTHS; index++)
{
if (year[index].high > highest)
{
highest = year[index].high;
highMonth = index;
}
if (year[index].low < lowest)
{
lowest = year[index].low;
lowMonth = index;
}
}
// Display findings.
cout << " Total Rainfall: " << totalRain << endl;
cout << "Average Monthly Rain: " << aveMonthRain << endl;
cout << "Average Monthly Average Temperature: " << aveAve << endl;
cout << "Highest Temperature: " << highest;
cout << " (Month " << (highMonth + 1) << ") ";
cout << "Lowest Temperature: " << lowest;
cout << " (Month " << (lowMonth + 1) << ") ";
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
