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
|
/*Write a modular program that analyzes a year’s worth of rainfall data. In addition to main,
the program should have a getData function that accepts the total rainfall for each of 12
months from the user, and stores it in a double array. It should also have four value-returning
functions that compute and return to main the totalRainfall, averageRainfall,
driestMonth, and wettestMonth. These last two functions return the number of the
month with the lowest and highest rainfall amounts, not the amount of rain that fell those
months. Notice that this month number can be used to obtain the amount of rain that fell
those months.
This information should be used either by main or by a displayReport
function called by main to print a summary rainfall report similar to the following:
2010 Rain Report for Neversnows County
Total rainfall: 23.19 inches
Average monthly rainfall: 1.93 inches
The least rain fell in January with 0.24 inches.
The most rain fell in April with 4.29 inches.
Input validation: Do not accept rainfall amounts less than 0.*/
#include <iostream>
using namespace std;
double YearData[12]{0,0,0,0,0,0,0,0,0,0,0,0};
string Months[12]{"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
int driest;
int wettest;
double sum;
double average;
int counterd;
int counterw;
double averageRainfall()
{
double sum = 0;
double average = 0;
for (int i = 0; i < 12; i++)
{
sum += YearData[i];
}
average = sum / 12;
return average;
}
double totalRainfall()
{
double sum = 0;
for (int i = 0; i < 12; i++)
{
sum += YearData[i];
}
return sum;
}
double driestMonth() //Lowest number of the 12
{
int driest = YearData[0];
int counterd = 0;
for (int i = 0; i < 12; i++)
{
counterd++;
if (YearData[i] < driest)
{
driest = YearData[i];
}
}
return driest;
}
double wettestMonth() //Greatest number of the 12
{
int wettest = YearData[0];
int counterw = 0;
for (int i = 0; i < 12; i++)
{
counterw++;
if (YearData[i] > wettest)
{
wettest = YearData[i];
}
}
return wettest;
}
double getData()
{
cout << "Enter the total rainfall for each of the 12 months this year: " << endl;
for (int i = 0; i < 12; i++)
{
cin >> YearData[i];
}
}
double printInformation()
{
cout << "\nRain Report" << endl;
cout << "-----------" << endl;
cout << "Total rainfall: " << sum << endl;
cout << "Average monthly rainfall: " << average << " inches" << endl;
cout << "The least rain fell in " << counterd << " with " << driest << " inches" << endl;
cout << "The most rain fell in " << counterw << " with " << wettest << " inches" << endl;
}
int main()
{
getData();
wettest = wettestMonth();
driest = driestMonth();
sum = totalRainfall();
average = averageRainfall();
printInformation();
}
|