Hello all, I am doing a rainfall statistics problem, My issue is I am not sure how to link the array rain[]; to the other functions. rain[] is the input of rain in numbers for months from 1-12.
Here is the problem and my code, thanks all.
Write a program that lets the user enter the total rainfall in inches for each of 12 months into an array of
doubles. The program should calculate and display the total rainfall for the year, the average monthly
rainfall, and the months with the highest and lowest amounts.
Your program should include 5 functions:
- inputData takes the array and its size. The function should ask the user to fill the array in a clear
way.
- total: takes the array and its size. The function should return the total rainfall for the year.
- average: takes the array and its size. The function should return the average monthly rainfall.
- highest: takes the array and its size. The function should return the month that has the highest
rainfall amount.
- lowest: takes the array and its size. The function should return the month that has the lowest
rainfall amount.
Input Validation: Do not accept negative numbers for monthly rainfall amounts.
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
|
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int months = 12;
string monthname[months]= { "Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
void inputArray() {
double rain[months];
for (int i = 0; i < months; i++)
{
cout << "How many inches of rain does " << monthname[i] << " have? \n";
cin >> rain[i];
while (rain[i] < 0)
{
cout << "Please enter a number greater than 0." << endl;
cin >> rain[i];
}
}
}
float RainMax(float maxi) {
string highmonth;
for (int i = 1; i < months; ++i)
{
if (rain[i] > maxi)
{
highmonth = monthname[i];
maxi = rain[i];
}
}
return maxi;
}
float RainMin(float mini) {
string lowmonth;
for (int i = 1; i < months; ++i)
{
if (rain[i] < mini)
{
lowmonth = monthname[i];
mini = rain[i];
}
return mini;
}
}
float Average(float output) {
double total = 0;
double avg = total / months * (1.0);
for (int i = 0; i < months; i++)
{
total += rain[i];
cout << monthname[i] << "has " << rain[i] << " inches of rainfall.\n";
}
return output;
cout << "The Average is: <<" << avg << " ";
}
float TotRain(float total) {
double total = 0;
for (int i = 0; i < months; i++)
{
total += rain[i];
cout << monthname[i] << "has " << rain[i] << " inches of rainfall.\n";
}
cout << "The Average is: <<" << total << " ";
return total;
}
float Average(float);
float RainMin(float);
float RainMax(float);
void inputArray();
int main() {
void inputArray();
}
|