Calculate Annual Rain Fall and Average Rain Fall a month , and Month with Maximum and Minimum Rain Fall

23:42 0 Comments A+ a-

Problem:
Write a program that lets the user enter the total rainfall 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.

Input Validation: Do not accept negative numbers for monthly rainfall figures.
Solution:
# include <iostream>
using namespace std;
void rainInput(double a[],int size)
{
cout<<"Please enter the Rain Fall for "<<size<<" months : "<<endl;
for(int i=0;i<size;i++)
{
cout<<"Please enter Rain Fall for month "<<i+1<<" (Non-Negative): ";
cin>>a[i];
while (a[i]<0)
{
cout<<"The Rain Fall must be Positive"<<endl;
cout<<"Please enter Rain Fall Again for month "<<i+1<<" (Non-Negative): ";
cin>>a[i];
}
}
}
void totalRainFall(double a[],int size)
{
double total=0;
double avg=0;
for(int i=0;i<size;i++)
{
total=total+a[i];
}
avg=total/12;
cout<<"The Total Rain Fall For The Complete Year is : "<<total<<endl;
cout<<"The Average Rain Fall of a Month is : "<<avg<<endl;
}
void findMaxMin(double a[],int size)
{
double max=a[0];
double min=a[0];
int maxPlace=0;
int minPlace=0;
for(int i=1;i<size;i++)
{
if(a[i]>max)
{
max=a[i];
maxPlace=i;
}
}
for(int j=1;j<size;j++)
{
if(a[j]<min)
{
min=a[j];
minPlace=j;
}
}
cout<<"Maximum Rain Fall is in Month "<<maxPlace+1<<endl;
cout<<"Minimum Rain Fall is in Month "<<minPlace+1<<endl;
}

int main()
{
const int size=12;
double rain[size];
rainInput(rain,size);
totalRainFall(rain,size);
findMaxMin(rain,size);


return 0;

}