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;
}

Only getTotal code is correct. All three other fuctions are wrong.
1. getAverage: use getTotal to obtain a sum of all elements then divide sum by the number of elements!
2. getLargest and getSmallest: why double largest = array[100]; ?! No such element in the array at all. Start from array[0] value then loop from index 1.

Only getTotal code is correct. All three other fuctions are wrong.
1. getAverage: use getTotal to obtain a sum of all elements then divide sum by the number of elements!
2. getLargest and getSmallest: why double largest = array[100]; ?! No such element in the array at all. Start from array[0] value then loop from index 1.

lol I forgot to change the array back to 0, I was just messing around with things to see what would happen. And for the getAverage I can't seem to get it to work properly. Everytime I call getTotal I get some sort of syntax error. I'll tinker around with it again and see if i can get it to work as you've described.

Everytime I call getTotal I get some sort of syntax error.

???

I fixed the function call, now the only problem is that it won't display the month with the largest/smallest data. Here's what it shows, along with the code following.

Enter the rainfall (in inches) for month #1: 2.5
Enter the rainfall (in inches) for month #2: 10.7
Enter the rainfall (in inches) for month #3: 32
Enter the rainfall (in inches) for month #4: 231
Enter the rainfall (in inches) for month #5: 7.8
Enter the rainfall (in inches) for month #6: 5.4
Enter the rainfall (in inches) for month #7: 2.5
Enter the rainfall (in inches) for month #8: 4.87
Enter the rainfall (in inches) for month #9: 5.8
Enter the rainfall (in inches) for month #10: 20.0
Enter the rainfall (in inches) for month #11: 2.6
Enter the rainfall (in inches) for month #12: 3.8

Total rainfall for the year was 328.97 inches.
Average rainfall for the year was 86.57 inches.

The largest amount of rainfall was 231.00 inches in month 13.
The smallest amount of rainfall was 2.50 inches in month 13.
Press any key to continue

#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;

 
	for(int count = 0; count < size; count++)
	{
	total += array[count];
	}

	for (count = 0; count < size; count++)
	{
	average = getTotal(array, size) / array[count];
	}
	return average;
}


double getLargest(double array[], int size, int &count)
{
	double largest = array[0];

	for (count = 1; count < size; count++)
	{ if (array[count] > largest)
	largest = array[count];
	
	}
	return largest;
}

double getSmallest(double array[], int size, int &count)
{
	double smallest = array[0];

	for (count = 1; count < size; count++)
	{ if (array[count] < smallest)
	smallest = array[count];
	}
	return smallest;
}

1. That's because the modification (improvement) of getSmallest and getLargest functions was incorrect. It was wrong solution to assign a role of for loop counter to output parameter count (why "count" if it's an index of? ). Think: the 1st count value is 1 (must be 0) and the last is 12 (then you add 1 and print 13). Make obvious correction.
2. Look again to getAverage code: it's WRONG!!!
Ooh, that's right code:

double getAverage(double array[], int size)
{
    return getTotal(array,size) / size;
}
commented: A little uptight but gave good hints without giving away the answer +1

I was using count because the examples that were shown used count as the name. I understand that that count should be 0, so the loop will end at 12, but i'm still struggling to understand how to get the correct index number with the largest and smallest values.

I should have know the getAverage function.

In getlargest() count will act as the index of the element with the largest rainfall for that year. Since count is passed to getlargest() by reference you don't need to return anything if there are no other requirements of the function. Use some counter variable other than count to control the loop and keep track of count separately.

void getLargest(double array[], int size, int &count)
{
   count = 0;  //assume first element is largest

   for (int i = 1; i < size; i++)
   { 
      if (array[i] > array[count])
      {
        count = i;
      }
   }
}

and back in main() use count to display the result;
cout << "the largerst rainfall was " << array[count];

the getLargest function needs to get both the index for the month and the largest value. I can't change anything in main, as it was given, so it needs to be a double. Do I need a nested for loop?

Figured it out

double getLargest(double array[], int size, int &i)
{
	double largest = array[0];

	for (int count = 0; count < size; count++)
	{ if (array[count] > largest)
	{largest = array[count]; 
	i = count;}
	}
	return largest;

}


double getSmallest(double array[], int size, int &i)
{
	double smallest = array[0];

	for (int count = 0; count < size; count++)
	{ if (array[count] < smallest)
	{smallest = array[count];
	i = count; }
	}
	return smallest;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.