Run-Time Check Failure #2 - Stack around the variable 'grade' was corrupted?

Nov 16, 2012 at 5:53pm
I wrote a program to calculate the average of 10 grades a user inputs. The program works and gives me the average, but at the end when it's supposed to close I get this error: "Run-Time Check Failure #2 - Stack around the variable 'grade' was corrupted."

I've posted the code below. Also, I have to make it calculate median and mode as well. I think I can figure out median, but can anyone help me to get started on mode?


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
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
 {
	//Create an array with 10 elements
	float grade[10];
	int count;
	float tscore = 0;
	//Set precision of Average, will be displayed with a decimal
	double average;
	for (count = 1; count <= 10; count++)
	 {
		cout << "Enter Grade " << count << ": ";
		 {
		cin >> grade[count];
	 }}

	for(count = 1; count <= 10; count++)
		tscore = tscore + grade[count];
	average = tscore/10;
	
	//Assign letter grade corresponding to number grade
	if((average >= 0) && (average < 60))
	{
		cout<<"The average is an F."<<endl;
	}
	else if((average >= 60) && (average < 63))
	{
		cout<<"The average is a D-."<<endl;
	}
	else if((average >= 63) && (average < 67))
	{
		cout<<"The average is a D."<<endl;
	}
	else if((average >= 67) && (average < 70))
	{
		cout<<"The average is a D+."<<endl;
	}
	else if((average >= 70) && (average < 73))
	{
		cout<<"The average is a C-."<<endl;
	}
	else if((average >= 73) && (average < 77))
	{
		cout<<"The average is a C."<<endl;
	}
	else if((average >= 77) && (average < 80))
	{
		cout<<"The average is a C+."<<endl;
	}
	else if((average >= 80) && (average < 83))
	{
		cout<<"The average is a B-."<<endl;
	}
	else if((average >= 83) && (average < 87))
	{
		cout<<"The average is a B."<<endl;
	}
	else if((average >= 87) && (average < 90))
	{
		cout<<"The average is a B+."<<endl;
	}
	else if((average >= 90) && (average < 93))
	{
		cout<<"The average is an A-."<<endl;
	}
	else if((average >= 93) && (average < 97))
	{
		cout<<"The average is an A."<<endl;
	}
	else if((average >= 97) && (average <= 100))
	{
		cout<<"The average is an A+."<<endl;
	}
	else
	{
		cout<<"The average is not between 0 and 100, please reenter the grades with values between 0 and 100."<<endl;
	}

	//Output
	cout << "The average of the ten grades you entered is: " << average << endl;

	system("PAUSE"); 
	return 0;
}
Nov 16, 2012 at 6:02pm
Array indices go from 0 to n-1, not from 1 to n.
Nov 16, 2012 at 7:11pm
Thanks working now. Just one minor problem, it now says Enter Grade 0 for the first grade. Is there any simple way I can change that? If not, it's fine how it is.

Also, how could I get the mode? Not asking for any code, just point me in the right direction. I know I'm going to have to use count, but not sure how to implement it.

Thanks
Last edited on Nov 16, 2012 at 7:20pm
Nov 16, 2012 at 7:34pm
You could change it to cout << "Enter Grade " << count+1 << ": ";

As for the mode, there's an STL algorithm called "count" that will return the amount of times something occurs in a range. Might be worth taking a look at http://www.cplusplus.com/reference/algorithm/count/
Last edited on Nov 16, 2012 at 7:34pm
Nov 16, 2012 at 9:21pm
Still trying to figure that out, almost have median done though.

I need to move the median = ... and the cout below to within the loop, but not sure where. Haven't gotten a right answer anywhere I put it while testing.

Uses bubble sorting to order and then I take the middle two numbers (since its 10, even) and add them up and divide by 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	//Calculate Median
	bool done;
	double temp;
	double median;
	while (done == false)
	{
	done = true;  //Done is a boolean value that is set to true by default, if it's left unchanged then we are done
	for (int i = 0; i < 9; i++)
		{
			if (grade[i] > grade[i+1])  //If the next value in the array is smaller than the current value, then we need to swap them.
				{
					temp = grade[i+1];  //Store the second value into a temporary variable first so we don't lose it
					grade[i+1] = grade[i];  //Then set the second value to the current value.
					grade[i] = temp;  //Afterwards place the stored temporary value into the current value
					done = false;   //Since we had to change a value, we are obviously not done, so when the loop completes, it will do another iteration.
	}
	}
	}
	median = (grade[5] + grade[6])/2;
	cout << "The median of the ten grades you entered is: " << median << endl;
Nov 16, 2012 at 10:05pm
Why would you want to move median = and cout into the loop? You only want to calculate it and output it once.
Nov 16, 2012 at 10:14pm
I guess that would be wrong too. Right now I'm getting a Runtime error and it's outputting a median, but the median is wrong. It's taking the median of grade[5] and grade[6] without ordering them.

Full code: (Median begins on line 89)
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
115
// GradeMean.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
 {
	float grade[10]; //Create an array with 10 elements
	int count;
	float tscore = 0;
	double average;
	
	//Input Grades
	for (count = 0; count <= 9; count++)
	 {
		cout << "Enter Grade " << count+1 << ": ";
		 {
		cin >> grade[count];
	 }}

	//Calculate Average
	for(count = 0; count <= 9; count++)
		tscore = tscore + grade[count];
	average = tscore/10;
	
	//Assign letter grade corresponding to number grade
	if((average >= 0) && (average < 60)) 
	{
		cout<<"The average is an F."<<endl;
	}
	else if((average >= 60) && (average < 63))
	{
		cout<<"The average is a D-."<<endl;
	}
	else if((average >= 63) && (average < 67))
	{
		cout<<"The average is a D."<<endl;
	}
	else if((average >= 67) && (average < 70))
	{
		cout<<"The average is a D+."<<endl;
	}
	else if((average >= 70) && (average < 73))
	{
		cout<<"The average is a C-."<<endl;
	}
	else if((average >= 73) && (average < 77))
	{
		cout<<"The average is a C."<<endl;
	}
	else if((average >= 77) && (average < 80))
	{
		cout<<"The average is a C+."<<endl;
	}
	else if((average >= 80) && (average < 83))
	{
		cout<<"The average is a B-."<<endl;
	}
	else if((average >= 83) && (average < 87))
	{
		cout<<"The average is a B."<<endl;
	}
	else if((average >= 87) && (average < 90))
	{
		cout<<"The average is a B+."<<endl;
	}
	else if((average >= 90) && (average < 93))
	{
		cout<<"The average is an A-."<<endl;
	}
	else if((average >= 93) && (average < 97))
	{
		cout<<"The average is an A."<<endl;
	}
	else if((average >= 97) && (average <= 100))
	{
		cout<<"The average is an A+."<<endl;
	}
	else
	{
		cout<<"The average is not between 0 and 100, please reenter the grades with values between 0 and 100."<<endl;
	}

	//Output
	cout << "The average of the ten grades you entered is: " << average << endl;

	//Calculate Median
	bool done;
	double temp;
	double median;
	while (done == false)
	{
	done = true;  //Done is a boolean value that is set to true by default, if it's left unchanged then we are done
	for (int i = 0; i < 9; i++)
		{
			if (grade[i] > grade[i+1])  //If the next value in the array is smaller than the current value, then we need to swap them.
				{
					temp = grade[i+1];  //Store the second value into a temporary variable first so we don't lose it
					grade[i+1] = grade[i];  //Then set the second value to the current value.
					grade[i] = temp;  //Afterwards place the stored temporary value into the current value
					done = false;   //Since we had to change a value, we are obviously not done, so when the loop completes, it will do another iteration.
	}
	}
	}
	median = (grade[5] + grade[6])/2;
	cout << "The median of the ten grades you entered is: " << median << endl;

	//Calculate Mode
	

	system("PAUSE"); 
	return 0;
}
Last edited on Nov 16, 2012 at 10:15pm
Nov 16, 2012 at 10:31pm
Array indices still go from 0 to n-1. grade[5] and grade[6] are the 6th and 7th elements, respectively.
Nov 16, 2012 at 10:36pm
I know, I changed it to 5 and 6 from 4 and 5. It would be 4 and 5 if I didn't add 1 to count, so it should be 5 and 6, so I don't think that's the problem. Unless I'm missing something, which is definitely possible...

thanks
Nov 16, 2012 at 10:48pm
I don't see where count comes into play in a expression like grade[5].
In any case, if what you're trying to say is that the grades don't get sorted, then that's because you're not initializing done, so it could be either true or false initially.
Nov 16, 2012 at 10:51pm
You're got it backwards, cmorris1441.

In regular numbers:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |

Array indices:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

Look vertically to see how the numbers correspond, If you want the 4th and 5th elements of the regular numbers, you'll want to use indices 3 and 4.
so median = (grade[3] + grade[4])/2
Last edited on Nov 16, 2012 at 10:51pm
Nov 16, 2012 at 10:56pm
You're right, just changed it and entered 91-100 randomly (once each) and got a median of 94 though...

Also, I'm getting an error:
Run-Time Check Failure #3 - The variable 'done' is being used without being initialized.

Program still runs when I click continue.
Last edited on Nov 16, 2012 at 10:59pm
Nov 16, 2012 at 11:05pm
The variable 'done' is being used without being initialized.

Believe it or not, but that's directly related to the fact that you're not initializing 'done'.
Nov 16, 2012 at 11:35pm
It is now, I have 2 programs and was looking at the wrong one. Here's what I now have, still not getting the median correct.

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
115
// GradeMean.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
 {
	float grade[10]; //Create an array with 10 elements
	int count;
	float tscore = 0;
	double average;
	
	//Input Grades
	for (count = 0; count <= 9; count++)
	 {
		cout << "Enter Grade " << count+1 << ": ";
		 {
		cin >> grade[count];
	 }}

	//Calculate Mean
	for(count = 0; count <= 9; count++)
		tscore = tscore + grade[count];
	average = tscore/10;
	
	//Assign letter grade corresponding to number grade
	if((average >= 0) && (average < 60)) 
	{
		cout<<"The average is an F."<<endl;
	}
	else if((average >= 60) && (average < 63))
	{
		cout<<"The average is a D-."<<endl;
	}
	else if((average >= 63) && (average < 67))
	{
		cout<<"The average is a D."<<endl;
	}
	else if((average >= 67) && (average < 70))
	{
		cout<<"The average is a D+."<<endl;
	}
	else if((average >= 70) && (average < 73))
	{
		cout<<"The average is a C-."<<endl;
	}
	else if((average >= 73) && (average < 77))
	{
		cout<<"The average is a C."<<endl;
	}
	else if((average >= 77) && (average < 80))
	{
		cout<<"The average is a C+."<<endl;
	}
	else if((average >= 80) && (average < 83))
	{
		cout<<"The average is a B-."<<endl;
	}
	else if((average >= 83) && (average < 87))
	{
		cout<<"The average is a B."<<endl;
	}
	else if((average >= 87) && (average < 90))
	{
		cout<<"The average is a B+."<<endl;
	}
	else if((average >= 90) && (average < 93))
	{
		cout<<"The average is an A-."<<endl;
	}
	else if((average >= 93) && (average < 97))
	{
		cout<<"The average is an A."<<endl;
	}
	else if((average >= 97) && (average <= 100))
	{
		cout<<"The average is an A+."<<endl;
	}
	else
	{
		cout<<"The average is not between 0 and 100, please reenter the grades with values between 0 and 100."<<endl;
	}

	//Output
	cout << "The average of the ten grades you entered is: " << average << endl;

	//Calculate Median
	bool done = false;
	double temp;
	double median;
	while (done == false)
	{
	done = true;  //Done is a boolean value that is set to true by default, if it's left unchanged then we are done
	for (int i = 0; i < 9; i++)
		{
			if (grade[i] > grade[i+1])  //If the next value in the array is smaller than the current value, then we need to swap them.
				{
					temp = grade[i+1];  //Store the second value into a temporary variable first so we don't lose it
					grade[i+1] = grade[i];  //Then set the second value to the current value.
					grade[i] = temp;  //Afterwards place the stored temporary value into the current value
					done = false;   //Since we had to change a value, we are obviously not done, so when the loop completes, it will do another iteration.
	}
	}
	}
	median = (grade[3] + grade[4])/2;
	cout << "The median of the ten grades you entered is: " << median << endl;

	//Calculate Mode
	

	system("PAUSE"); 
	return 0;
}
Last edited on Nov 16, 2012 at 11:36pm
Nov 16, 2012 at 11:58pm
The two middle elements are the ones with indices 4 and 5.
Nov 17, 2012 at 12:01am
Okay, the confusion ends here. I didn't count to the middle of the array properly.
median = (grade[4] + grade[5])/2
Reference my little diagram above. Numbers correspond vertically. We both should have seen that. Lol.
Nov 17, 2012 at 12:05am
Wasn't thinking when I corrected it earlier. Thanks. Median and Mean both work now.

Thanks Athar and Thumper for all the help.
Nov 17, 2012 at 3:02am
Oops, disregard. :)
Last edited on Nov 17, 2012 at 3:04am
Topic archived. No new replies allowed.