Storing index value

I have code that has the user input rainfall in inches for 12 months and then totals, averages and finds the largest and smallest rainfall and month corrolated with it. In the getLargest/getSmallest functions they need to store the index so when it finds the largest and smallest, it also uses the index to display what month it was in. I have an example of what it looks like, as well as the code below. I put in random numbers

Output:
Enter the rainfall (in inches) for month #1: 13.8
Enter the rainfall (in inches) for month #2: 54.2
Enter the rainfall (in inches) for month #3: 74.3
Enter the rainfall (in inches) for month #4: 75.4
Enter the rainfall (in inches) for month #5: 15.0
Enter the rainfall (in inches) for month #6: 64.6
Enter the rainfall (in inches) for month #7: 14.8
Enter the rainfall (in inches) for month #8: 85.3
Enter the rainfall (in inches) for month #9: 25.7
Enter the rainfall (in inches) for month #10: 96.9
Enter the rainfall (in inches) for month #11: 35.1
Enter the rainfall (in inches) for month #12: 50.5

Total rainfall for the year was 605.60 inches.
Average rainfall for the year was 50.47 inches.

The largest amount of rainfall was 96.90 inches in month 13. <-- problem here
The smallest amount of rainfall was 13.80 inches in month 13. <-- and here
Press any key to continue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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)
{   
	return getTotal(array,size) / size;
}


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

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

	}
	return largest;
}

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

	for (i = 0; i < size; i++)
	{ if (array[i] < smallest)
	smallest = array[i];
	}
	return smallest;
}
Don't use i to iterate. Use something else, and every time there's a new max/min, set i to the current value of the index.
so replace i in the counter to count or something? and then i would equal count? This is rough, but something along that?
1
2
3
4
5
6
7
8
9
10
11
12
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;

}



EDIT: Got it, thanks for the hints
Last edited on
Topic archived. No new replies allowed.