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
|
#include <iostream>
using namespace std;
int main()
{
const int NUM_MONTHS = 12; //number of values
int values[NUM_MONTHS]; //each value
int month; //loop counter
int lowest, highest, rainfall, count;
double average;
double total = 0;
//Array with the names of the months
string months[NUM_MONTHS] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//Input rainfall for each month
for(int month = 1; month <= NUM_MONTHS; month++)
{
cout << "Enter the total rainfall for " << months[month-1] << ": ";
cin >> rainfall;
//Input validation - enter a value over or equal to 0
if ( rainfall < 0 )
{
cout << "The number you have entered is invalid." << endl;
cout << "Please reenter: ";
cin >> rainfall;
}
//Calculate total rainfall for the year
total += rainfall;
}
//Calculate the average monthly rainfall
average = total/12;
//Find the months with the highest rainfall and lowest rainfall
highest = values[0];
lowest = values[0];
for(int month = 0; month < NUM_MONTHS; month++)
{
if(values[month] > highest)
highest = values[month];
if(values[month] < lowest)
lowest = values[month];
}
//Display total rainfall for year, monthly average, highest and lowest months
cout << "\n\nThe total rainfall for the year is: " << total << " inches"
<< "\nThe average monthly rainfall is: " << average << " inches";
cout << "\nThe month with the highest rainfall is: " << highest
<< "\nThe month with the lowest rainfall is: " << lowest << endl;
system("pause");
return 0;
}
|