// Torrez, Elaine CS1A Chapter 7, P. 444, #2
// RAINFALL STATISTICS
// *****************************************************************************************
// * *
// *---------------------------------------------------------------------------------------*
// * This program asks the user to enter the total rainfall for each of 12 months. *
// * The program stores the values in an array, then calculates and displays: *
// * - The total rainfall for the year *
// * - The average monthly rainfall *
// * - The month with the highest rainfall *
// * - The month with the lowest rainfall *
// * *
// * INPUT: *
// * rainfall[12] : Rainfall amount for each month *
// * *
// * OUTPUT: *
// * totalRainfall : Total rainfall for the year *
// * avgRainfall : Average monthly rainfall *
// * highestMonth : Month with the most rainfall *
// * lowestMonth : Month with the least rainfall *
// * *
// * INPUT VALIDATION: *
// * Do not accept negative numbers for rainfall amounts. *
// *****************************************************************************************
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
/***** CONSTANTS *****/
const int MONTHS = 12;
/***** VARIABLE DECLARATIONS *****/
double rainfall[MONTHS]; // Array to store rainfall for each month
string monthNames[MONTHS] = { // Month names for display
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
double total = 0.0; // Total rainfall
double average; // Average rainfall
double highest; // Highest rainfall
double lowest; // Lowest rainfall
int highMonth = 0; // Index of highest rainfall month
int lowMonth = 0; // Index of lowest rainfall month
/***** INPUT SECTION *****/
cout << "Enter the total rainfall (in inches) for each month:\n";
for (int i = 0; i < MONTHS; i++)
{
cout << monthNames[i] << ": ";
cin >> rainfall[i];
// Input validation — no negative values allowed
while (rainfall[i] < 0)
{
cout << "Invalid input. Rainfall cannot be negative. Re-enter: ";
cin >> rainfall[i];
}
total += rainfall[i]; // Add to total as we go
}
/***** PROCESSING SECTION *****/
average = total / MONTHS; // Calculate average
// Initialize highest and lowest
highest = lowest = rainfall[0];
for (int i = 1; i < MONTHS; i++)
{
if (rainfall[i] > highest)
{
highest = rainfall[i];
highMonth = i;
}
if (rainfall[i] < lowest)
{
lowest = rainfall[i];
lowMonth = i;
}
}
/***** OUTPUT SECTION *****/
cout << fixed << setprecision(2);
cout << "\n=============================================\n";
cout << " RAINFALL STATISTICS REPORT \n";
cout << "=============================================\n";
cout << "Total rainfall for the year : " << total << " inches\n";
cout << "Average monthly rainfall : " << average << " inches\n";
cout << "Highest rainfall month : " << monthNames[highMonth]
<< " (" << highest << " inches)\n";
cout << "Lowest rainfall month : " << monthNames[lowMonth]
<< " (" << lowest << " inches)\n";
cout << "=============================================\n";
return 0;
}