// Torrez, Elaine CS1A
// ----------------------------------------------
// WEATHER STATISTICS
// ----------------------------------------------
//
// This program stores weather data for 12 months using
// a structure. For each month, the user enters the total
// rainfall, the high temperature, and the low temperature.
// The program calculates the average monthly rainfall,
// total yearly rainfall, highest temperature and the month
// it occurred, lowest temperature and the month it occurred,
// and the average temperature for the entire year.
//
// ----------------------------------------------
// INPUT
// For each of the 12 months:
// total rainfall
// high temperature
// low temperature
//
// PROCESSING
// Validate input (rain >= 0, temp between -100 and 140)
// Compute total rainfall
// Compute average monthly rainfall
// Identify highest and lowest temperature + month
// Compute average of (high + low)/2 for each month
//
// OUTPUT
// Total rainfall for the year
// Average rainfall
// Highest temperature and month
// Lowest temperature and month
// Average yearly temperature
//
// ----------------------------------------------
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Structure to store weather data
struct Weather {
double rainfall; // Total rainfall for the month
double highTemp; // Highest temperature
double lowTemp; // Lowest temperature
double avgTemp; // Average monthly temperature
};
int main()
{
const int MONTHS = 12;
string monthNames[MONTHS] =
{ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
Weather data[MONTHS];
double totalRain = 0;
double totalAvgTemp = 0;
double highest = -101;
double lowest = 141;
int highMonth = 0;
int lowMonth = 0;
cout << fixed << setprecision(2);
// ----------------------------------------------------
// INPUT SECTION
// ----------------------------------------------------
for (int i = 0; i < MONTHS; i++)
{
cout << "\nEnter data for " << monthNames[i] << ":\n";
// Rainfall validation
do {
cout << " Total rainfall: ";
cin >> data[i].rainfall;
if (data[i].rainfall < 0)
cout << " Error: Rainfall cannot be negative.\n";
} while (data[i].rainfall < 0);
// High temperature validation
do {
cout << " High temperature: ";
cin >> data[i].highTemp;
if (data[i].highTemp < -100 || data[i].highTemp > 140)
cout << " Error: Temp must be between -100 and 140.\n";
} while (data[i].highTemp < -100 || data[i].highTemp > 140);
// Low temperature validation
do {
cout << " Low temperature: ";
cin >> data[i].lowTemp;
if (data[i].lowTemp < -100 || data[i].lowTemp > 140)
cout << " Error: Temp must be between -100 and 140.\n";
} while (data[i].lowTemp < -100 || data[i].lowTemp > 140);
// Average temperature for this month
data[i].avgTemp = (data[i].highTemp + data[i].lowTemp) / 2.0;
// Add to totals
totalRain += data[i].rainfall;
totalAvgTemp += data[i].avgTemp;
// Track highest temp
if (data[i].highTemp > highest) {
highest = data[i].highTemp;
highMonth = i;
}
// Track lowest temp
if (data[i].lowTemp < lowest) {
lowest = data[i].lowTemp;
lowMonth = i;
}
}
// ----------------------------------------------------
// OUTPUT SECTION
// ----------------------------------------------------
cout << "\n----------------------------------------------\n";
cout << "WEATHER STATISTICS REPORT\n";
cout << "----------------------------------------------\n";
cout << "Total Rainfall: " << totalRain << " inches\n";
cout << "Average Monthly Rainfall: " << totalRain / MONTHS << " inches\n";
cout << "Highest Temperature: " << highest
<< " ( " << monthNames[highMonth] << " )\n";
cout << "Lowest Temperature: " << lowest
<< " ( " << monthNames[lowMonth] << " )\n";
cout << "Average Temperature for the Year: "
<< totalAvgTemp / MONTHS << " degrees\n";
return 0;
}