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
|
#include<iostream>
#include<string>
using namespace std;
struct weather
{
double rainfall;
double highTemp;
double lowTemp;
double avgTemp;
};
int main()
{
weather months[12];
double total = 0, highest, lowest, avgsum;
int highmonth, lowmonth;
string month[12] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
for (int i = 0; i<12; i++)
{
cout << "Enter the total rainfall amount for month " << month[i] << ": ";
cin >> months[i].rainfall;
cout << "Enter high temperature: ";
cin >> months[i].highTemp;
while (months[i].highTemp < -100 || months[i].highTemp > 140)
{
cout << "ERROR: Temperature must be in the range of " << "-100 through 140.\n";
cout << "\tHigh Temperature: ";
cin >> months[i].highTemp;
}
cout << "Enter low temperature: ";
cin >> months[i].lowTemp;
while (months[i].lowTemp < -100 || months[i].lowTemp > 140)
{
cout << "ERROR: Temperature must be in the range of "
<< "-100 through 40.\n";
cout << "\tLow Temperature: ";
cin >> months[i].lowTemp;
}
}
//Calculate monthly average temperature and total rainfall for the year.
for (int i = 0; i<12; i++)
{
total = total + months[i].rainfall;
months[i].avgTemp = (months[i].highTemp + months[i].lowTemp) / 2;
}
highest = months[0].highTemp;
lowest = months[0].lowTemp;
for (int i = 1; i<12; i++)
{
if (months[i].highTemp>highest)
{
highest = months[i].highTemp;
highmonth = i;
cout << highmonth;
}
if (months[i].lowTemp<lowest)
{
lowest = months[i].lowTemp;
lowmonth = i;
cout << highmonth;
}
}
avgsum = 0;
//Calculate the average.
for (int i = 0; i<12; i++)
{
avgsum = avgsum + months[i].avgTemp;
}
//Display data.
cout << "Average monthly rainfall: " << total / 12 << endl;
cout << "Total monthly rainfall: " << total << endl;
cout << "Highest rainfall in " << month[highmonth] << " is: " << highest << endl;
cout << "Lowest rainfall in " << month[lowmonth] << " is: " << lowest << endl;
cout << "The average of all the monthly average temperatures is: " << avgsum / 12 << endl;
system("pause");
return 0;
}
|