I want to design 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 2-D array of type double. 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:
Here is the code I have written, I continue getting 0 returned I'm not sure what I'm doing wrong.
const int months = 12;
const int rain = 1;
void getData(double[months][rain], int, int);
double totalRainfall(double[months][rain], int, int);
double averageRain(double, int);
double driestMonth(double[months][rain], int, int);
int main()
{
double totalRain;
double averageRainfall;
int leastMonth;
double rainfall[months][rain];
getData(rainfall, months, rain);
totalRain = totalRainfall(rainfall, months, rain);
averageRainfall = averageRain(totalRain, months);
leastMonth = driestMonth(rainfall, months, rain);
cout << averageRainfall << endl;
cout << leastMonth;
}
void getData(double rainfall[months][rain], int months, int rain)
{
double rainAmount;
for(int row = 0; row < months; row++)
{
for(int column = 0; column < rain; column++)
{
cout << "enter rain for month " << (row+1) << "--->";
cin >> rainAmount;
while(rainAmount <= 0)
{
cout << "rainfall must be greater then 0" << endl;
cout << "enter rainfall ---> ";
cin >> rainAmount;
}
rainfall[row][column] = rainAmount;
}
}
}
double totalRainfall(double rainfall[months][rain], int months, int rain)
{
double totalRain = 0;
for (int column = 0; column < months; column++)
{
for(int row = 0; row < rain; row++)
{
totalRain += rainfall[column][row];
}
}
return totalRain;
}
double averageRain(double totalRain, int months)
{
double averageRainfall;
averageRainfall = totalRain / months;
return averageRainfall;
}
double driestMonth(double rainfall[months][rain], int months, int rain)
{
double leastRainMonth = rainfall[0][0];
int leastMonth;
for(int row = 0; row < months; row++)
{
for(int column = 0; column < rain; rain++)
{
if(rainfall[row][column] < leastMonth)
{
leastRainMonth = rainfall[row][column];
leastMonth = row;
}
}
}
return leastMonth;
}