1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Function prototypes
void getRainfall(double[], int); //To retrieve the user input.
double getTotal(double[], int); //To total the rainfall amounts.
double getAverage(double[], int); //To get the average rainfall.
double getLowest(double[], int, int&); //Returns the lowest value, provides the index of the lowest value in the last parameter.
double getHighest(double[], int, int&); //Returns the highest value, provides the index of the highest value in the last parameter.
//Global Variable
const int NUM_MONTHS = 12;
//Array names
double rainfall[NUM_MONTHS];
string month[NUM_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; //Month array to hold names of months
int main()
{
//Declare variables
double total, average;
int low, high;
string lowMonth, highMonth;
//Call Functions
getRainfall(rainfall, NUM_MONTHS); //To retrieve the user input.
total = getTotal(rainfall, NUM_MONTHS); //To total the rainfall amounts.
average = getAverage(getTotal, NUM_MONTHS); //To get the average rainfall.
lowMonth = getLowest(rainfall, NUM_MONTHS, low); //Returns the lowest value, provides the index of the lowest value in the last parameter.
highMonth = getHighest(rainfall, NUM_MONTHS, high); //Returns the highest value, provides the index of the highest value in the last parameter.
//Display the following:
cout << "The total rainfall for the year is: " << total << endl;
cout << "The average rainfall for the year is: " << fixed << showpoint << setprecision(2) << average << endl;
cout << "Least amount of rainfall fell in: " << highMonth << endl;
cout << "Most amount of rainfall fell in: " << lowMonth << endl;
return 0;
}
//********************************************************************
// void getRainfall(double[], int) *
// To retrieve the user input for the total rainfall for each month.*
//********************************************************************
void getRainfall(double rainfall[], int NUM_MONTHS)
{
int index; //Loop counter
for (index = 0; index < NUM_MONTHS; index++)
{
cout << "Enter rainfall for " << month[index] << ": ";
cin >> rainfall[index];
}
}
//********************************************************************
// double getTotal(const double amount[], int size) *
// To total the rainfall amounts. *
//********************************************************************
double getTotal(double amount[], int NUM_MONTHS)
{
double total = 0; //Accumulator
for (int index = 0; index < NUM_MONTHS; index++)
total += amount[index];
return total;
}
//********************************************************************
// double getAverage(double rainfall[], int NUM_MONTHS); *
// To get the average rainfall. *
//********************************************************************
double getAverage(double getTotal, int NUM_MONTHS)
{
//double total = 0;
double average;
//for (int count = 0; count < NUM_MONTHS; count++)
// total += rainfall[count];
average = getTotal / NUM_MONTHS;
return average;
}
//*******************************************************************************************
// double getLowest(double amount[], int size) *
// Returns the lowest value, provides the index of the lowest value in the last parameter. *
//*******************************************************************************************
double getLowest(double amount[], int NUM_MONTHS, int &low)
{
low = amount[0]; //Variable to hold lowest value.
int lowMonth = 0; //Set low value to intial rainfall value. //Variable to return month element location.
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] < low)
{
low = amount[index];
lowMonth = index;
}
}
return lowMonth;
}
//*********************************************************************************************
// double getHighest(double amount[], int size) *
// Returns the highest value, provides the index of the highest value in the last parameter. *
//*********************************************************************************************
double getHighest(double amount[], int NUM_MONTHS, int &high)
{
high = amount[0]; //Variable to hold highest value
int highMonth = 0; //Set high value to intial rainfall value. //Variable to return month element location.
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] > high)
{
high = amount[index];
highMonth = index;
}
}
return highMonth;
}
|