Try to declare variables where they are used. Otherwise the code will be less readable.
The array rainfall is not yet initialized
double rainfall[ARRAY_SIZE];
//...
double highest = rainfall[0];
double lowest = rainfall[0];
So using its elements with indeterminate values for the variables highest
and lowest
does not make sense.
Declare and initialize the variables just before the loop where they are calculated.
double highest = rainfall[0];
double lowest = rainfall[0];
for(int x = 0; x < ARRAY_SIZE; x++)
{
if(highest < rainfall[x])
{
highest = rainfall[x];
}
if(lowest > rainfall[x])
{
lowest = rainfall[x];
}
}
In this loop
for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << " Month " << index+1 << ": " ;
cin >> rainfall[index];
total_year += rainfall[index];
if(rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
}
move the statement
total_year += rainfall[index];
after the if statement.
for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << " Month " << index+1 << ": " ;
cin >> rainfall[index];
if(rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
total_year += rainfall[index];
}
I would substitute the if statement for a while statement like
while (rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
but before using the variable total_year
you have to initialize it
double total_year = 0.0;
The variable monthly_average
is not used in the code. So its declaration can be removed.
Take into account that there are the following algorithms in C++ std::min_element
, std::max_element
, std::minmax_element
that can be used to find minimum and maximum alements in an array or other container.