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
|
[output]#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void tableFormat(string,string,string,string);
void tableFormat(double,double,double,double);
int main ()
{
double avgRain = 0;
double rainSum = 0;
int count = 0;
double monthlyTotals[12];
string outRainSum;
double lowpoint=100000;
double highpoint=0;
string lowMonth;
string highMonth="January";
string monthNames[] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
cout << "Please enter the amount of rainfall for each month, and press enter after each" << endl;
for (count = 0; count <= 11; count++)
{
cout<< monthNames[count] << " : ";
cin >> monthlyTotals[count];
while (monthlyTotals[count] < 0)
{
cout << "Please reenter a positive number for the month of " << monthNames[count] << endl;
cin >> monthlyTotals[count];
}
}
for (count =0; count <=11; count++)
rainSum = rainSum + monthlyTotals[count];
avgRain = rainSum / 12;
for (count = 0; count <=11; count++)
{ cout << endl;
cout << monthNames[count] << "\t" << monthlyTotals[count] << endl;
}
highpoint = monthlyTotals[0];
for (count=0; count<=11; count++)
{
if (monthlyTotals[count] >= highpoint)
{
highpoint = monthlyTotals[count];
highMonth = monthNames[count];
}
}
lowpoint = monthlyTotals[0];
for (count=0; count<=11; count++)
{
if (monthlyTotals[count] <= lowpoint)
{
lowpoint = monthlyTotals[count];
lowMonth = monthNames[count];
}
}
tableFormat("Total","Average","Lowpoint","Highpoint");
tableFormat(rainSum, avgRain, lowpoint,highpoint);
cout << "The month(s) with the lowest rainfall is ";
for (count=0; count <=11; count++)
if (monthlyTotals[count] == lowpoint)
cout << monthNames[count] << ", ";
cout << "with a rainfall of: "<< lowpoint << "." << endl;
cout << "The month(s) with the highest rainfall is ";
for (count=0; count <=11; count++)
if (monthlyTotals[count] == highpoint)
cout << monthNames[count] << ", ";
cout << " with a rainfall of: "<< highpoint <<"." << endl;
return 0;
}
void tableFormat(string colA,string colB,string colC,string colD)
{
cout <<setw(15) << fixed << colA <<setw(15) << fixed << colB <<setw(15) << fixed << colC <<setw(15) << fixed << colD <<endl;
}
void tableFormat(double colA,double colB,double colC,double colD)
{
cout << setprecision(3)<<setw(15) << fixed << colA <<setw(15) << fixed << colB <<setw(15) << fixed << colC <<setw(15) << fixed << colD << endl;
}
|