#include<iostream>
#include <iomanip>
using namespace std;
// Function prototypes
double getTotal(double [], int);
double getAverage(double [], int);
double getLargest(double [], int, int &);
double getSmallest(double [], int, int &);
int main()
{
const int NUM_MONTHS = 12;
double rainFall[NUM_MONTHS]; // Stores total rainfall for each month
// Input rainfall amounts and store them in the 12 array locations
for (int month = 0; month < NUM_MONTHS; month++)
{
cout << "Enter the rainfall (in inches) for month #";
cout << (month + 1) << ": ";
cin >> rainFall[month];
while (rainFall[month] < 0)
{ cout << "Rainfall must be 0 or more. Please re-enter: ";
cin >> rainFall[month];
}
}
// Display the total rainfall
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Total rainfall for the year was ";
cout << setw(5) << getTotal(rainFall, NUM_MONTHS) << " inches." << endl;
// Display the average rainfall
cout << "Average rainfall for the year was ";
cout << setw(5) << getAverage(rainFall, NUM_MONTHS) << " inches." << endl << endl;
// Display the months with the largest & smallest amounts of rain.
// The variable index is passed by reference to the getLargest &
// getSmallest functions, so they can assign it the subscript of the
// array element having the largest, or smallest, amount of rainfall.
int index;
cout << "The largest amount of rainfall was " << setw(5);
cout << getLargest(rainFall, NUM_MONTHS, index) << " inches in month ";
cout << (index + 1) << "." << endl;
cout << "The smallest amount of rainfall was " << setw(5);
cout << getSmallest(rainFall, NUM_MONTHS, index) << " inches in month ";
cout << (index + 1) << "." << endl;
return 0;
}
double getTotal(double array[], int size)
{
double total = 0;
for(int count = 0; count < size; count++)
{
total += array[count];
}
return total;
}
double getAverage(double array[], int size)
{
double total;
double average;
for(int count = 0; count < size; count++)
{
total += array[count];
}
for (count = 0; count < size; count++)
{
average = getTotal(array, size) / array[count];
}
return average;
}
double getLargest(double array[], int size, int &count)
{
double largest = array[0];
for (count = 1; count < size; count++)
{ if (array[count] > largest)
largest = array[count];
}
return largest;
}
double getSmallest(double array[], int size, int &count)
{
double smallest = array[0];
for (count = 1; count < size; count++)
{ if (array[count] < smallest)
smallest = array[count];
}
return smallest;
}