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
|
#include <iostream>
#include <iomanip>
using namespace std;
double getMonthTotal(); // Function Prototype
double findYearTotal(double[], int);// Function Prototype
double findAvg(double[], int);// Function Prototype
double findHighest(double[], int);// Function Prototype
double findLowest(double[], int);// Function Prototype
int main ()
{
const int NUM_MONTHS = 12; // Constant for total months
double months[NUM_MONTHS]; // Double array to pass values for each month
double yearTotal, average, highest, lowest; // Variable to hold values the functions will send back
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++) //Loops a call to get 12 months of data from the user by calling the getMonthTotal function
{ months[count]= getMonthTotal(); }
yearTotal = findYearTotal(months, NUM_MONTHS); // Gets the total of all rainfall by calling the findYearTotal function
average = findAvg(months, NUM_MONTHS); // Gets the average monthly rainfall by calling the findAvg function
highest = findHighest(months, NUM_MONTHS); // Gets the month with the most rain by calling findHighest function
lowest = findLowest(months, NUM_MONTHS); // Gets the month with the least rain by calling the findLowest function
cout <<"Total Rainfall for year: \t" << yearTotal << endl; // Displays total for year
cout << "Average Monthly rainfall: \t" << average <<endl; // Displays monthly aveage
cout << "Least rainfall was in month: \t"<<lowest << endl;
cout <<"Most rainfall occured in month \t" <<highest <<endl;
char ch;
cout <<"Please press enter to exit\n";
cin.ignore();
cin.get(ch);
return 0;
}
double getMonthTotal() // This function gets a monthly total each time it's called
{
double monthtotal;
cout << "\nPlease enter a total;\n";
cin >> monthtotal;
if (monthtotal < 0)
{ cout <<"Invalid input\n";
getMonthTotal();
}
return monthtotal;
}
double findYearTotal(double month[], int num_months) // This function totals monthly data when called
{
double total = 0;
for (int counter = 0; counter < num_months; counter++)
{
total += month[counter];
}
return total;
}
double findAvg( double month[], int num_months) // This function averages all monthly data when called
{
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) // This function determines the highest value in an array
{
int highest = month[0];
int 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) // This function determins the lowest value in an array
{
int lowest = month[0];
int index = 0;
for (int count = 0; count < num_months; count++)
{
if (month[count] < lowest)
lowest = month[count];
index = count + 1;
}
return index;
}
|