array wont output lowest number correctly

so im doing a bit of homework that i thought was pretty easy. but for some reason when i run the code and put the numbers in, no matter what i put in the section for lowest always spits out garbage. i cant figure out why.

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
  
#include <stdafx.h>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//*****************************************************************************

void getRainfall(float &, int);

//*****************************************************************************

int main()
{
	const int SIZE = 12;
	int   month = 0;
	float total = 0,
		  average,
		  rainfall[SIZE],
		  maximum = rainfall[SIZE],
		  minimum = rainfall[SIZE];
		  
	for(int i = 0; i < SIZE; i++)
	{
		getRainfall(rainfall[i], month);
		total += rainfall[i];
		if(rainfall[i] > maximum)
			maximum = rainfall[i];
		if(rainfall[i] < minimum)
			minimum = rainfall[i];
		month++;
	}
	average = total / SIZE;
	cout << "" << endl;
	cout << "Your total is: " << setprecision(1) << showpoint << fixed
		 << setw(10) << total << endl;
	cout << "Average: " << setw(15) << average << endl;
	cout << "Highest: " << setw(15) << maximum << endl;
	cout << "Lowest: " << setw(15) << minimum << endl;
	cin.get();
	cin.get();
	return 0;
}

//******************************************************************************
// This function will receive the amount of rainfall from the user                      *
//******************************************************************************

void getRainfall (float & rain, int date)
{
	const int SIZE = 12,
			  lowest = 0;
	string    months[SIZE] =
	{"Jan", "Feb", "Mar", "Apr",
	 "May", "Jun", "Jul", "Aug",
	 "Sep", "Oct", "Nov", "Dec"};
		do
		{
        cout << "Please enter the rain amount for " << months[date] << ": ";
	cin >> rain;
	if(rain < lowest)
	cout << "That isnt a vaild input!, please try again!" << endl;
	}
	while(rain < lowest);
}
minimum = rainfall[SIZE]; is setting minimum to an undefined memory location. These have garbage values. The garbage value is extremely negative, so nothing you enter will be lower than the minimum.
would it be better if i initialized it as zero? im not sure how to combat that.
Initializing to zero would remove the garbage value. However, if you never go below zero, it will always be the minimum. The best thing would be to set the first value to the max & min always, then compare each consecutive one with them.
thanks for the help, i initialized them to SIZE so they dont have a garbage value and its functioning properly now. i appreciate it.
Does it work properly if all your values are less than size? What about if they are all greater?
so what i went ahead and did was initialize maximum to rainfall[SIZE] and then lower in the program i took the if statement regarding minimum and took it out of the for loop. i then under the for loop made minimum = maximum and wrote a new loop checking minimum. as shown here.

float total = 0,
average,
rainfall[SIZE],
maximum = rainfall[SIZE],
minimum;

for(int i = 0; i < SIZE; i++)
{
getRainfall(rainfall[i], month);
total += rainfall[i];
if(rainfall[i] > maximum)
maximum = rainfall[i];
month++;
}
minimum = maximum;
for(int i = 0; i < SIZE; i++)
{
if(rainfall[i] < minimum)
minimum = rainfall[i];
}
well mine functions, but that is certainly cleaner and a smarter approach. i appreciate the help!
Topic archived. No new replies allowed.