//Diego Martinez CSC5 Chapter 7, P.487, #2
/*******************************************************************************
* CALCULATE RAINFALL PATTERNS
* ______________________________________________________________________________
* This program manages rainfall data collected over a period of 12 months. It
* determines overall patterns in the data by user inputs provided.
*
*
* Computation is based on the Formula:
* Total = R1 + R2 + R3 + ... + R12
* Average = Total / 12
*______________________________________________________________________________
* INPUT
* 12 Real Numbers (No negative Numbers allowed)
*
* OUTPUT
* Total Rainfall for the year
* Average monthly rainfall
* Month with the highest rainfall
* Month with the lowest rainfall
*
*******************************************************************************/
#include <iostream>
using namespace std;
// ===== FUNCTION PROTOTYPES =====
void inputRainfall(double rain[], int size);
void calculateStats(double rain[], int size, double &total, double &average, int &highest, int &lowest);
void displayResults(double rain[], int size, double total, double average, int highest, int lowest);
// ===== MAIN FUNCTION =====
int main()
{
// ===== DATA DICTIONARY =====
const int SIZE = 12; // number of months
double rainfall[SIZE]; // array to store rainfall values
double totalRain; // total yearly rainfall
double avgRain; // average monthly rainfall
int highestMonth; // index of highest rainfall
int lowestMonth; // index of lowest rainfall
// ===== FUNCTION CALLS =====
inputRainfall(rainfall, SIZE);
calculateStats(rainfall, SIZE, totalRain, avgRain, highestMonth, lowestMonth);
displayResults(rainfall, SIZE, totalRain, avgRain, highestMonth, lowestMonth);
return 0;
}
// ===== FUNCTION DEFINITIONS =====
// Function to input rainfall with validation
void inputRainfall(double rain[], int size)
{
for (int i = 0; i < size; i++)
{
do
{
cout << "Enter rainfall for month " << i + 1 << ": ";
cin >> rain[i];
if (rain[i] < 0)
{
cout << "Invalid input. Rainfall cannot be negative.\n";
}
} while (rain[i] < 0);
}
}
// Function to calculate total, average, highest, and lowest
void calculateStats(double rain[], int size, double &total, double &average, int &highest, int &lowest)
{
total = 0;
highest = 0;
lowest = 0;
for (int i = 0; i < size; i++)
{
total += rain[i];
if (rain[i] > rain[highest])
{
highest = i;
}
if (rain[i] < rain[lowest])
{
lowest = i;
}
}
average = total / size;
}
// Function to display results
void displayResults(double rain[], int size, double total, double average, int highest, int lowest)
{
cout << "\nTotal rainfall for the year: " << total << endl;
cout << "Average monthly rainfall: " << average << endl;
cout << "Month with highest rainfall: Month " << highest + 1
<< " (" << rain[highest] << ")" << endl;
cout << "Month with lowest rainfall: Month " << lowest + 1
<< " (" << rain[lowest] << ")" << endl;
}