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
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
double getTotal(double[], int);
double getAverage(double[], int);
double getLowest(double[], int, int&);
double getHighest(double[], int, int&);
int main()
{
const int SIZE = 12;
string months[SIZE] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"};
double rainfall[SIZE],
total,
average;
int lowest;
int count;
for (count = 0; count < SIZE; count++)
{
cout << "What is the rainfall amount for the month of " << months[count] << "?" << endl;
cin >> rainfall[count];
while (rainfall[count] < 0)
{
cout << "You entered an invalid number please enter a value greater than 0." << endl;
cin >> rainfall[count];
}
}
total = getTotal(rainfall, SIZE);
cout << "The total rainfall for the year is: " << total << endl;
average = getAverage(rainfall, SIZE);
cout << "The average monthly rainfall is: " << average << endl;
lowest = getLowest(rainfall, SIZE, count);
cout << "lowest rainfall amount is: " << rainfall[lowest] << ", which fell in the month of: " << months[lowest] << endl;
return 0;
}
double getTotal(double rainfall[], int SIZE)
{
double total = 0;
for (int count = 0; count < SIZE; count++)
total += rainfall[count];
return total;
}
double getAverage(double rainfall[], int SIZE)
{
double average = 0;
double total;
total = getTotal(rainfall, SIZE);
average = total / SIZE;
return average;
}
double getLowest(double rainfall[], int SIZE, int &count)
{
double lowest;
lowest = rainfall[0];
for (count = 1; count < SIZE; count++)
{
if (rainfall[count] < lowest)
lowest = rainfall[count];
}
return lowest;
}
|