Array Problem

Dec 8, 2014 at 8:02pm
Ok so my program is working properly except for some nasty large, negative number it outputs and I don't know why.
I have narrowed down the problem to these lines of code:

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
void highlowavgFunc(int e[], int o[], double& Ae, double& Ao)
{

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

	for (int element = 0; element < 50; element++)
	{
		if (e[maxeElement] < e[element])
			maxeElement = element;
		if (o[maxoElement] < o[element])
			maxoElement = element;
		if (e[mineElement] > e[element])
			mineElement = element;
		if (o[minoElement] > o[element])
			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; //The number outputs here
	Mo = o[minoElement];
	outfile << "Min Odd: " << Mo << endl; //The number outputs here 


The number it outputs is -858993460 and that's not even remotely close to the number I was looking for.
Last edited on Dec 8, 2014 at 8:04pm
Dec 8, 2014 at 8:09pm
The first time the code gets to line 14, the value of maxeElement is still -10, which means you are accessing the memory that just so happens to be before the array. In other words, you are accessing out of bounds.
Dec 10, 2014 at 5:05am
ok so how would I fix this then? or would there be another way to initialize maxeElement, maxoElement, mineElement, minoElement
Last edited on Dec 10, 2014 at 5:14am
Dec 10, 2014 at 5:13am
Either check within the loop to see if the values you're using are invalid indexes, or just initialize them to some valid index that makes sense: 0, perhaps.
Dec 10, 2014 at 5:36am
ok so that fixed one of the numbers, here is the updated code:

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

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

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

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

	while (infile)
	{
		for (int i = 0; i < 50; i++)
			infile >> even[i];
		for (int i = 0; i < 50; i++)
		{
			if (even[i] % 2 != 0)
			{
				odd[i] = even[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; //Updated values
	int maxoElement = 0; //Updated values
	int mineElement = 0; //Updated values
	int minoElement = 0; //Updated values
	int eventotal = 0;
	int oddtotal = 0;
	int me, mo, Me, Mo;

	for (int element = 0; element < 50; element++)
	{
		if (e[maxeElement] < e[element])
			maxeElement = element;
		if (o[maxoElement] < o[element])
			maxoElement = element;
		if (e[mineElement] > e[element])
			mineElement = element;
		if (o[minoElement] > o[element]) //The weird number is here
			minoElement = element; //The weird number is here
	}
	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 = eventotal + e[element];
		oddtotal = 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;
}





Last edited on Dec 10, 2014 at 5:38am
Topic archived. No new replies allowed.