"...write a program that reads up to 20 decimal values of weekly rainfall for a given city. The program should ask the user how many weeks of data they want to enter, then uses a function to input the values and stores them in an array. The program must use a function to calculate and return the average rainfall.
The output of the program displays all rainfall values entered followed by the amount above or below the average for each week. Finally, the program displays the smallest and largest rainfall values for the year..."
//Problem set 10; Exercise 1;
//Written by: Dylan Metz
#include <iostream.h>
void GetRain(double[ ],int);
double CalAverageRain(double[], int);
int main()
{
constint SIZE=20;
double Array[SIZE];
double average, smallest, largest;
int weeks; //user input:# of weeks.
cout<<"How many weeks of data would you like to input? ";
cin>>weeks;
//call function to get array values
GetRain(Array,weeks);
//call function to calculate average
average=CalAverageRain(Array,weeks);
//display values followed by amount above of below average & determine largest
//and smallest
smallest=largest=Array[0];
for(int x=0;x<weeks-1;x++)
{
if(Array[x]<smallest)
{
smallest=Array[x];
}
if(Array[x]>largest)
{
largest=Array[x];
}
if(Array[x]>average)
{
cout<<Array[x]<<" is above the average by "<<(Array[x]-average)<<endl;
}
else
{
cout<<Array[x]<<" is below the average by "<<(average-Array[x])<<endl;
}
}
//Display smallest and largest rainfall values
cout<<"The largest is "<<largest<<endl;
cout<<"The smallest is "<<smallest<<endl;
//functions
void getRain(double Array[], int weeks)
{
for(int x=0;x<weeks-1;x++)
{
cout<<"Enter an rainfall amount:";
cin>>Array[x];
}
}
double calcAverageRain(double Array[], int weeks)
{
double sum=0, average;
for(int x=0;x<weeks-1;x++)
{
sum+=Array[x];
}
average = sum/weeks;
return average;
}
return 0;
}