*Note: The purpose of functions is to allow code to be reusable. You will need t
ID: 3808004 • Letter: #
Question
*Note: The purpose of functions is to allow code to be reusable. You will need this function again for your out-of-lab assignment.
*Note: The purpose of functions is to allow code to be reusable. You will need this function again for your out-of-lab assignment.
Sample Input:
Enter the rainfall for month #1: 3.2
Enter the rainfall for month #2: 5.6
Enter the rainfall for month #3: 4.1
Enter the rainfall for month #4: 2.3
Enter the rainfall for month #5: 5.6
Enter the rainfall for month #6: 3.7
Enter the rainfall for month #7: 2.3
Enter the rainfall for month #8: 4.5
Enter the rainfall for month #9: 3.4
Enter the rainfall for month #10: 2.5
Enter the rainfall for month #11: 2.3
Enter the rainfall for month #12: 5.1
Sample Output:
The rainfalls for the following months were:
Month 1: 3.2
Month 2: 5.6
Month 3: 4.1
Month 4: 2.3
Month 5: 5.6
Month 6: 3.7
Month 7: 2.3
Month 8: 4.5
Month 9: 3.4
Month 10: 2.5
Month 11: 2.3
Month 12: 5.1
The total rainfall for the year was 44.60 inches.
The average rainfall for the year was 3.72 inches.
The month(s) that recorded the highest amount of rain of 5.60 inch(es) was (were):
Months #: 2 5
The month(s) that recorded the lowest amount of rain of 2.30 inch(es) was (were):
Months #: 4 7 11
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
double total(double rainfall[], int n)
{
double tot = 0;
for(int i = 0; i < n; i++)
{
tot += rainfall[i];
}
return tot;
}
double max(double rainfall[], int n)
{
double maxR = rainfall[0];
for(int i = 1; i < n; i++)
{
if (maxR < rainfall[i])
{
maxR = rainfall[i];
}
}
return maxR;
}
double min(double rainfall[], int n)
{
double minR = rainfall[0];
for(int i = 1; i < n; i++)
{
if (minR > rainfall[i])
{
minR = rainfall[i];
}
}
return minR;
}
void printRainfall(double rainfall[], int n)
{
for(int i = 0; i < n; i++)
{
cout << "Month " << (i+1) << ": " << fixed << setprecision(1) << rainfall[i]<<endl;
}
}
int main()
{
double rainfall[12];
for(int i = 0; i < 12; i++)
{
cout << "Enter the rainfall for month #" << (i+1) << ": ";
cin >> rainfall[i];
}
cout << "The rainfalls for the following months were:" << endl;
printRainfall(rainfall, 12);
cout << endl;
double totalR = total(rainfall, 12);
cout << "The total rainfall for the year was " << fixed << setprecision(2) << totalR << " inches." << endl;
cout << "The average rainfall for the year was " << fixed << setprecision(2) << (totalR/12) << " inches." << endl<<endl;
double maxR = max(rainfall, 12);
cout << "The month(s) that recorded the highest amount of rain of " << fixed << setprecision(2) << maxR << " inch(es) was (were): "<<endl;
cout << " Months #: ";
for(int i = 0; i < 12; i++)
{
if(rainfall[i] == maxR)
{
cout << (i+1) << " ";
}
}
cout << endl<<endl;
double minR = min(rainfall, 12);
cout << "The month(s) that recorded the lowest amount of rain of " << fixed << setprecision(2) << minR << " inch(es) was (were): "<<endl;
cout << " Months #: ";
for(int i = 0; i < 12; i++)
{
if(rainfall[i] == minR)
{
cout << (i+1) << " ";
}
}
cout << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.