What is wrong with my functions?

I need this program to average everything input into the array, as well as find the highest and lowest values. Please help!

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
  #include <iostream>
using namespace std;
float temptotal;
float averagetemp = 0;
float temperatures[50];
void average();
void highest();
void lowest();
int main()
{
	average();
	highest();
	lowest();
}
	void average()
	{
		float days = 0;
		cout << "Enter the number of days: ";
		cin >> days;
		if (days > 50)
		{
			cout << "You may only enter temperatures for 50 days." << endl;
		}
		for (int i = 1; i <= days; i++)
		{
				cout << "Enter the temperature for day number " << i << ": ";
				cin >> temperatures[i];
				temptotal += temperatures[i];
				averagetemp = (temptotal / days);
				cout << "The average temperature is: " << averagetemp << endl;
		}
	}
		void highest()
		{
			float max = -9999999999999;
			if (temperatures[50] > max)
				max = temperatures[50];
			cout << "The highest temperature is: " << max << endl;
			
		}
		void lowest()
		{
			float min = 9999999999999;
			if (temperatures[50] < min)
				min = temperatures[50];
			cout << "The lowest temperature is: " << min << endl;
		}
temptotal should be initialized if you're going to use it to accumulate a total.

line 22 - you output a message if they input an invalid number, but still go ahead with the input. This could end up trying to access array elements out of range.

line 24 - array elements run from 0 to the size of the array minus 1, so 0 to 49, not 1 to 50

line 29-30 Do you mean to calculate an average just once or after each entry of temperature?

line 36, line 44 - you're checking against an element that doesn't exist. Seems like you'd want to use another for loop here to go through all the daily temps entered to find the smallest or largest.
I mean to calculate an average just once.
Then the lines calculating and printing the average can be pulled out of the for loop.
Topic archived. No new replies allowed.