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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int NUM_MONTHS = 12;
const int STRING_SIZE = 10;
void wettestMonth(double [], string []);
void driestMonth(double[], string []);
void totalRain(double[]);
void averageRain(double[]);
void zeroArray(double []);
int main()
{
string months[NUM_MONTHS] =
{ "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
double rain[NUM_MONTHS] = {
0.40, 0.94, 3.21, 3.74, 1.73, 1.03, 1.27, 2.58, 6.98, 6.90, 2.80, 2.53};
int count;
cout << "Austin, Tx Rainfall 2009:" << endl;
cout << endl;
for (int month = 0; month < NUM_MONTHS; month++)
{
cout << setw(9) << left << months[month] << " has ";
cout << rain[month] << " inches of rainfall" << endl;
}
totalRain(rain);
averageRain(rain);
driestMonth(rain, months);
wettestMonth(rain, months);
return 0;
}
void wettestMonth(double rain[], string months[])
{
string month[12];
double highest = rain[0];
int R;
for (int i = 0; i < 12; i++)
{
if(rain[i] > highest)
{
month[i] = months[i];
highest = rain[i];
R = i;
}
}
cout << "Highest Rainfall: " << highest << " inches." << endl;
for(int i = 0; i < 12; ++i)
{
if(month[i] != "")
cout << "Month with highest rainfall: " << month[i] << endl;
}
}
void driestMonth(double rain[], string months[])
{
string month[12];
double lowest = rain[0];
month[0] = months[0];
for(int i = 1; i < 12; ++i)
{
if(rain[i] < lowest) {
month[i] = months[i];
lowest = rain[i];
}
else if(rain[i] == lowest)
{
month[i] = months[i];
}
}
cout << "Lowest Rainfall: " << lowest << " inches" << endl;
for(int i = 0; i < 12; ++i)
{
if(month[i] != "")
cout << "Month with lowest rainfall: " << month[i] << endl;
}
}
void totalRain(double rain[])
{
double total = 0;
for (int index = 0; index < NUM_MONTHS; index++)
total += rain[index];
cout << "Total Rainfall: " << total << " inches." << endl;
}
void averageRain(double rain[])
{
int count = 0;
double avgRain = 0;
double rainSum = 0;
for (count =0; count <=11; count++)
{
rainSum = rainSum + rain[count];
}
avgRain = rainSum / 12;
cout<< "Average Rainfall: " << avgRain << " inches." << endl;
}
| |