//Devin Scheu CS1A Chapter 11, P. 646, #4
//
/**************************************************************
*
* CALCULATE AND DISPLAY WEATHER STATISTICS
* ____________________________________________________________
* This program determines weather statistics for a year,
* including average monthly rainfall, total rainfall, highest
* and lowest temperatures, and average temperature.
* ____________________________________________________________
* INPUT
* weatherData : Weather data for each of the 12 months
*
* OUTPUT
* weatherSummary : The formatted display of weather statistics
*
**************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct WeatherData {
double totalRainfall;
double highTemperature;
double lowTemperature;
double averageTemperature;
};
int main () {
//Variable Declarations
const int MONTHS = 12; //OUTPUT - Number of months in a year
WeatherData weatherData[MONTHS]; //INPUT - Weather data for each of the 12 months
string monthNames[MONTHS] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
double totalRainfallYear = 0; //OUTPUT - The total rainfall for the year
double averageRainfallYear; //OUTPUT - The average monthly rainfall
double highestTemp = -1000; //OUTPUT - The highest temperature for the year
int highestTempMonth = 0; //OUTPUT - The month of the highest temperature
double lowestTemp = 1000; //OUTPUT - The lowest temperature for the year
int lowestTempMonth = 0; //OUTPUT - The month of the lowest temperature
double totalAvgTemp = 0; //OUTPUT - The total of monthly average temperatures
double avgOfAvgTemps; //OUTPUT - The average of all monthly average temperatures
string weatherSummary; //OUTPUT - The formatted display of weather statistics
//Prompt for Input with Loop
for (int i = 0; i < MONTHS; i++) {
weatherData[i].averageTemperature = 0; // Initialize to avoid uninitialized use
cout << "Enter total rainfall for " << monthNames[i] << " (in inches): ";
cin >> weatherData[i].totalRainfall;
while (weatherData[i].totalRainfall < 0) {
cout << "\nError: Please enter a non-negative amount: ";
cin >> weatherData[i].totalRainfall;
}
cout << weatherData[i].totalRainfall << " inches" << endl;
cout << "Enter high temperature for " << monthNames[i] << " (in °F): ";
cin >> weatherData[i].highTemperature;
while (weatherData[i].highTemperature < -100 || weatherData[i].highTemperature > 140) {
cout << "\nError: Please enter a temperature between -100 and 140°F: ";
cin >> weatherData[i].highTemperature;
}
cout << weatherData[i].highTemperature << "°F" << endl;
cout << "Enter low temperature for " << monthNames[i] << " (in °F): ";
cin >> weatherData[i].lowTemperature;
while (weatherData[i].lowTemperature < -100 || weatherData[i].lowTemperature > 140) {
cout << "\nError: Please enter a temperature between -100 and 140°F: ";
cin >> weatherData[i].lowTemperature;
}
cout << weatherData[i].lowTemperature << "°F" << endl;
//Calculate average temperature
weatherData[i].averageTemperature = (weatherData[i].highTemperature + weatherData[i].lowTemperature) / 2.0;
//Update yearly statistics
totalRainfallYear += weatherData[i].totalRainfall;
totalAvgTemp += weatherData[i].averageTemperature;
if (weatherData[i].highTemperature > highestTemp) {
highestTemp = weatherData[i].highTemperature;
highestTempMonth = i;
}
if (weatherData[i].lowTemperature < lowestTemp) {
lowestTemp = weatherData[i].lowTemperature;
lowestTempMonth = i;
}
}
//Calculate Yearly Averages
averageRainfallYear = totalRainfallYear / MONTHS;
avgOfAvgTemps = totalAvgTemp / MONTHS;
//Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
//Output Result
cout << fixed << setprecision(2);
cout << left << setw(25) << "Total Rainfall:" << right << setw(15) << totalRainfallYear << " inches" << endl;
cout << left << setw(25) << "Average Monthly Rainfall:" << right << setw(15) << averageRainfallYear << " inches" << endl;
cout << left << setw(25) << "Highest Temperature:" << right << setw(15) << highestTemp << "°F in " << monthNames[highestTempMonth] << endl;
cout << left << setw(25) << "Lowest Temperature:" << right << setw(15) << lowestTemp << "°F in " << monthNames[lowestTempMonth] << endl;
cout << left << setw(25) << "Average of Monthly Averages:" << right << setw(15) << avgOfAvgTemps << "°F" << endl;
} //end of main()