Variable Corrupted

I keep getting the error "Stack around the variable 'odd' was corrupted." how can I fix this here is the program:

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
111
112
113
114
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

ifstream infile; ofstream outfile;

void highlowavgFunc(int even[], int odd[], double&, double&);
void maxthanavgFunc(double&, double&, int even[], int odd[]);

int main()
{
	int Num, i = 0;
	int even[50], odd[50];
	double avgeven, avgodd;
	infile.open("c:\\Users\\student\\Documents\\data2.txt");
	if (!infile)
	{
		cout << "Error file not found." << endl;
		system("pause");
		exit(1);
	}
	else
		cout << "Success!" << endl;

	outfile.open("c:\\Users\\student\\Documents\\array.txt");

	while (infile)
	{
		infile >> Num;
		if (Num % 2 == 0)
		{
			even[i] = Num;
			i++;
		}
		else if (Num % 2 != 0)
		{
			odd[i] = Num;
			i++;
		}
	}

	highlowavgFunc(even, odd, avgeven, avgodd);
	maxthanavgFunc(avgeven, avgodd, even, odd);

	infile.close();
	outfile.close();

	system("pause");
	return 0;
}

//Find the min, max, total, and average
void highlowavgFunc(int e[], int o[], double& Ae, double& Ao)
{

	int maxeElement = 0;
	int maxoElement = 0;
	int mineElement = 0;
	int minoElement = 0;
	int eventotal = 0;
	int oddtotal = 0;
	int me, mo, Me, Mo;

	for (int element = 0; element < 50; element++)
	{
		if (e[element] > e[maxeElement])
			maxeElement = element;
		if (o[element] > e[maxoElement])
			maxoElement = element;
		if (e[element] < e[mineElement])
			mineElement = element;
		if (o[element] < o[minoElement])
			minoElement = element;
	}
	me = e[maxeElement];
	outfile << "Max Even: " << me << endl;
	mo = o[maxoElement];
	outfile << "Max Odd: " << mo << endl;

	Me = e[mineElement];
	outfile << "Min Even: " << Me << endl;
	Mo = o[minoElement];
	outfile << "Min Odd: " << Mo << endl;

	for (int element = 0; element < 50; element++)
	{
		eventotal += e[element];
		oddtotal += o[element];
	}

	Ae = eventotal / 29;
	outfile << "This is the average for the even array: " << Ae << endl;
	Ao = oddtotal / 21;
	outfile << "This is the average for the odd array: " << Ao << endl;
}

//Find a number higher than the average
void maxthanavgFunc(double& Ae, double& Ao, int e[], int o[])
{
	int element;

	outfile << "These numbers are higher than their respective averages: ";
	for (element = 0; element < 50; element++)
	{
		if (e[element] > Ae)
			outfile << e[element] << "\t";
		if (o[element] > Ao)
			outfile << o[element] << "\t";
	}
	outfile << endl;
}
Make sure you don't run out of bounds when accessing the array elements.
Topic archived. No new replies allowed.