I've got executable code that is partially working, so i know i've done something wrong in it. In it you type in the rainfall for each month in inches and will display the total, average, largest and smallest rainfalls(with the month). I have the the total, average and largest(except the month shows 13 everytime) and the smallest along with month being 13 are not working. It's probably some simple error here and there but I can't seem to find what I am doing wrong.
I'm assuming that after I've gone through the array the index just adds another value that is making the months both 13 on the smallest/largest stats and I'm not sure what I'm to do to change it.
The main function is fine as it is, it's the others that need help.
#include<iostream>
#include <iomanip>
using namespace std;
// Function prototypes
double getTotal(double [], int);
double getAverage(double [], int);
double getLargest(double [], int, int &);
double getSmallest(double [], int, int &);
int main()
{
const int NUM_MONTHS = 12;
double rainFall[NUM_MONTHS]; // Stores total rainfall for each month
// Input rainfall amounts and store them in the 12 array locations
for (int month = 0; month < NUM_MONTHS; month++)
{
cout << "Enter the rainfall (in inches) for month #";
cout << (month + 1) << ": ";
cin >> rainFall[month];
while (rainFall[month] < 0)
{ cout << "Rainfall must be 0 or more. Please re-enter: ";
cin >> rainFall[month];
}
}
// Display the total rainfall
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Total rainfall for the year was ";
cout << setw(5) << getTotal(rainFall, NUM_MONTHS) << " inches." << endl;
// Display the average rainfall
cout << "Average rainfall for the year was ";
cout << setw(5) << getAverage(rainFall, NUM_MONTHS) << " inches." << endl << endl;
// Display the months with the largest & smallest amounts of rain.
// The variable index is passed by reference to the getLargest &
// getSmallest functions, so they can assign it the subscript of the
// array element having the largest, or smallest, amount of rainfall.
int index;
cout << "The largest amount of rainfall was " << setw(5);
cout << getLargest(rainFall, NUM_MONTHS, index) << " inches in month ";
cout << (index + 1) << "." << endl;
cout << "The smallest amount of rainfall was " << setw(5);
cout << getSmallest(rainFall, NUM_MONTHS, index) << " inches in month ";
cout << (index + 1) << "." << endl;
return 0;
}
double getTotal(double array[], int size)
{
double total = 0;
for(int count = 0; count < size; count++)
{
total += array[count];
}
return total;
}
double getAverage(double array[], int size)
{
double total;
double average;
total = 0;
for(int count = 0; count < size; count++)
{
total += array[count];
}
for (count = 0; count < size; count++)
{
average = total / array[count];
}
return average;
}
double getLargest(double array[], int size, int &count)
{
double largest = array[100];
for (count = 0; count < size; count++)
{ if (array[count] > largest)
largest = array[count];
}
return largest;
}
double getSmallest(double array[], int size, int &count)
{
double smallest = array[100];
for (count = 0; count < size; count++)
{ if (array[count] < smallest)
smallest = array[count];
}
return smallest;
}