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
|
#include <iostream>
#include <iomanip>
using namespace std;
double getMonthTotal();
double getTotal(double[], int);
double findAverage(double[], int);
double findHighest(double[], int, int&);
double findLowest(double[], int, int&);
int main ()
{
const int NUM_MONTHS = 12;
int index = 0;
double months[NUM_MONTHS];
double yearTotal, average, highest, lowest;
cout << "This program will ask you for rain fall totals for 12 months.\n"
<< "Once the date is entered the program will give you a total, an average\n"
<< " and the highest and lowest months.\n";
for (int count = 0; count < NUM_MONTHS; count++)
{
months[count]= getMonthTotal();
}
yearTotal = getTotal(months, NUM_MONTHS);
average = findAverage(months, NUM_MONTHS);
highest = findHighest(months, NUM_MONTHS, index);
lowest = findLowest(months, NUM_MONTHS, index);
cout << "Total Rainfall for year: \t" << yearTotal << endl;
cout << "Average Monthly rainfall: \t" << average <<endl;
cout << "Least rainfall was in month: \t"<<lowest << endl;
cout << "Most rainfall occured in month \t" <<highest <<endl;
system("pause");
}
double getMonthTotal()
{
double monthtotal;
cout << "\nPlease enter a total: ";
cin >> monthtotal;
if (monthtotal < 0)
{ cout <<"Invalid input\n";
getMonthTotal();
}
return monthtotal;
}
double getTotal(double month[], int num_months)
{
double total = 0;
for (int counter = 0; counter < num_months; counter++)
{
total += month[counter];
}
return total;
}
double findAverage( double month[], int num_months)
{
double total = 0;
for (int counter = 0; counter < num_months; counter++)
{
total += month[counter];
}
double average = (total / num_months);
return average;
}
double findHighest(double month[], int num_months, int& index)
{
int highest = month[0];
index = 0;
for (int count = 0; count < num_months; count++)
{
if (month[count] > highest)
highest = month[count];
index = count + 1;
}
return index;
}
double findLowest(double month[], int num_months, int& index)
{
int lowest = month[0];
index = 0;
for (int count = 0; count < num_months; count++)
{
if (month[count] < lowest)
lowest = month[count];
index = count + 1;
}
return index;
}
|