Trouble with Array and function.

Good afternoon,
I have an assignment that I am having some trouble with, The program needs to receive data on 12 months of rainfall, then display the total rainfall for the year, the average monthly rainfall then the month with the highest amount and lowest amount. My problem is that the average monthly is always 6.5.

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
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

double getTotal(double [], int);
void getRainfall(string [], double [], int);
double getLowest(double [], int, int&);
double getHighest(double [], int, int&);
double getAverage(double [], int);

int main()
{
	// Inititalize variables
	const int MAXRECS = 12;
	int lrIndex;
	int hrIndex;
	string monthNames[MAXRECS] = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
	double rainFall[MAXRECS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
	double total, average, lowRain, highRain;
	
	average = getAverage(rainFall, MAXRECS);
	getRainfall(monthNames, rainFall, MAXRECS);
	total = getTotal(rainFall, MAXRECS);
	lowRain = getLowest(rainFall, MAXRECS, lrIndex);
	highRain = getHighest(rainFall, MAXRECS, hrIndex);

	cout << "\n Highest rain is " << highRain;
	cout << "\n whick occurred in the month of : " << monthNames[hrIndex] << endl;
	cout << "\nLowest rain is " << lowRain;
	cout << "\n whick occurred in the month of : " << monthNames[lrIndex] << endl;
	cout << "\nthe total is : " <<total << endl;
	cout << "\nthe average is : " <<average << endl;

	return 0;
}

double getHighest(double rainfall[], int MAX, int& index)
{
	double highest = rainfall[0];
	index = 0; // in case the first is the highest kind of a "reset"
	for (int x = 0; x < MAX; ++x)
	{
		if (rainfall[x] > highest){
			highest = rainfall[x];
			index = x;
		}
	}

	return highest;
}


double getLowest(double rainfall[], int MAX, int& index)
{
	double lowest = rainfall[0];
	index = 0; // in case the first is the lowest kind of a "reset"
	for (int x = 0; x < MAX; ++x)
	{
		if (rainfall[x] < lowest){
			lowest = rainfall[x];
			index = x;
		}
	}

	return lowest;
}

double getAverage(double rainfall [], int MAX)
{
	double total = 0; 
	double average = 0;

	for (int x = 0; x < MAX; ++x)
	{
		total += rainfall[x];
		average = total / MAX;
	}

	return average;
}

double getTotal(double rainfall[], int MAX)
{	
	double total = 0; 
	
	for (int x = 0; x < MAX; ++x)
		total += rainfall[x];
	
	return total;
}

void getRainfall(string months[], double rainfall[], int MAX)
{ 
	for(int x = 0; x < MAX; ++x) 
	{
		cout << "Enter rainfall amont for " << months[x] << ":";
		cin >> rainfall[x];
	}
}

You are calculating average befor reading any values. So it gives you an average of {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} — 6.5
Thank you MiiNiPaa, I forgot I had those there. Now that i removed them I am having a other problem, my average is always -9.25596e+061

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

// Assignment 10.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

double getTotal(double [], int);
void getRainfall(string [], double [], int);
double getLowest(double [], int, int&);
double getHighest(double [], int, int&);
double getAverage(double [], int);

int main()
{
	// Inititalize variables
	const int MAXRECS = 12;
	int lrIndex;
	int hrIndex;
	string monthNames[MAXRECS] = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
	double rainFall[MAXRECS];
	double total, average, lowRain, highRain;
	
	average = getAverage(rainFall, MAXRECS);
	getRainfall(monthNames, rainFall, MAXRECS);
	total = getTotal(rainFall, MAXRECS);
	lowRain = getLowest(rainFall, MAXRECS, lrIndex);
	highRain = getHighest(rainFall, MAXRECS, hrIndex);

	cout << "\n Highest rain is " << highRain;
	cout << "\n whick occurred in the month of : " << monthNames[hrIndex] << endl;
	cout << "\nLowest rain is " << lowRain;
	cout << "\n whick occurred in the month of : " << monthNames[lrIndex] << endl;
	cout << "\nthe total is : " <<total << endl;
	cout << "\nthe average is : " <<average << endl;

	return 0;
}

double getHighest(double rainfall[], int MAX, int& index)
{
	double highest = rainfall[0];
	index = 0; // in case the first is the highest kind of a "reset"
	for (int x = 0; x < MAX; ++x)
	{
		if (rainfall[x] > highest){
			highest = rainfall[x];
			index = x;
		}
	}

	return highest;
}


double getLowest(double rainfall[], int MAX, int& index)
{
	double lowest = rainfall[0];
	index = 0; // in case the first is the lowest kind of a "reset"
	for (int x = 0; x < MAX; ++x)
	{
		if (rainfall[x] < lowest){
			lowest = rainfall[x];
			index = x;
		}
	}

	return lowest;
}

double getAverage(double rainfall [], int MAX)
{
	double total = 0; 
	double average = 0;

	for (int x = 0; x < MAX; ++x)
	{
		total += rainfall[x];
		average = total / MAX;
	}

	return average;
}

double getTotal(double rainfall[], int MAX)
{	
	double total = 0; 
	
	for (int x = 0; x < MAX; ++x)
		total += rainfall[x];
	
	return total;
}

void getRainfall(string months[], double rainfall[], int MAX)
{ 
	for(int x = 0; x < MAX; ++x) 
	{
		cout << "Enter rainfall amont for " << months[x] << ":";
		cin >> rainfall[x];
	}
}





You are still calculating average before you read values. But now instead of predefined values it is reading some junk left after initialization.
I finally saw what you were talking about, now it is working perfect. Thank you I cant believe some little mistakes cost me so many hours. lol
Tks again.
Topic archived. No new replies allowed.