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
|
#include <iostream>
#include <string>
using namespace std;
int main ()
{
double avgRainfall = 0;
double totalRainfall = 0;
double lowestRainfall;
double highestRainfall;
string lowestMonth;
string highestMonth;
string monthNames[] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double monthlyTotals[12];
char repeat;
do
{
cout << "Enter the rainfall for each month, in inches: " << endl;
//Input and positive number validation
for (int i = 0; i < 12; i++)
{
cout << monthNames[i] << ": ";
cin >> monthlyTotals[i];
while (monthlyTotals[i] < 0)
{
cout << "Please enter a positive number for " << monthNames[i] << endl;
cin >> monthlyTotals[i];
}
}
//Sum
for (int i = 0; i < 12; i++)
{
totalRainfall = totalRainfall + monthlyTotals[i];
}
//Average
avgRainfall = totalRainfall / 12;
//Highest and Lowest months
highestRainfall = monthlyTotals[0];
lowestRainfall = monthlyTotals[0];
for (int i = 0; i < 12; i++)
{
if (monthlyTotals[i] > highestRainfall)
{
highestRainfall = monthlyTotals[i];
highestMonth = monthNames[i];
}
if (monthlyTotals[i] < lowestRainfall)
{
lowestRainfall = monthlyTotals[i];
lowestMonth = monthNames[i];
}
}
//Output
cout << "The total rainfall throughout the year was " << totalRainfall << " inches." << endl;
cout << "The average rainfall for the entire year is " << avgRainfall << " inches." << endl;
cout << "The month with the lowest rainfall is " << lowestMonth << ", with a total of "<< lowestRainfall <<" inches." << endl;
cout << "The month with the highest rainfall is " << highestMonth << ", with a total of "<< highestRainfall <<" inches." << endl;
//Reset the variables for re-run
avgRainfall = 0;
totalRainfall = 0;
lowestRainfall = 0;
highestRainfall = 0;
highestMonth;
lowestMonth;
cout << "\nWould you like to run the program again? (Y/N) ";
cin >> repeat;
cout << endl;
}while(repeat == 'y' || repeat == 'Y');
return 0;
}
|