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?
#include "stdafx.h"
#include <iostream>
usingnamespace 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;
}
elseif((average >= 60) && (average < 63))
{
cout<<"The average is a D-."<<endl;
}
elseif((average >= 63) && (average < 67))
{
cout<<"The average is a D."<<endl;
}
elseif((average >= 67) && (average < 70))
{
cout<<"The average is a D+."<<endl;
}
elseif((average >= 70) && (average < 73))
{
cout<<"The average is a C-."<<endl;
}
elseif((average >= 73) && (average < 77))
{
cout<<"The average is a C."<<endl;
}
elseif((average >= 77) && (average < 80))
{
cout<<"The average is a C+."<<endl;
}
elseif((average >= 80) && (average < 83))
{
cout<<"The average is a B-."<<endl;
}
elseif((average >= 83) && (average < 87))
{
cout<<"The average is a B."<<endl;
}
elseif((average >= 87) && (average < 90))
{
cout<<"The average is a B+."<<endl;
}
elseif((average >= 90) && (average < 93))
{
cout<<"The average is an A-."<<endl;
}
elseif((average >= 93) && (average < 97))
{
cout<<"The average is an A."<<endl;
}
elseif((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;
}
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.
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/
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.
//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;
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.
// GradeMean.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
usingnamespace 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;
}
elseif((average >= 60) && (average < 63))
{
cout<<"The average is a D-."<<endl;
}
elseif((average >= 63) && (average < 67))
{
cout<<"The average is a D."<<endl;
}
elseif((average >= 67) && (average < 70))
{
cout<<"The average is a D+."<<endl;
}
elseif((average >= 70) && (average < 73))
{
cout<<"The average is a C-."<<endl;
}
elseif((average >= 73) && (average < 77))
{
cout<<"The average is a C."<<endl;
}
elseif((average >= 77) && (average < 80))
{
cout<<"The average is a C+."<<endl;
}
elseif((average >= 80) && (average < 83))
{
cout<<"The average is a B-."<<endl;
}
elseif((average >= 83) && (average < 87))
{
cout<<"The average is a B."<<endl;
}
elseif((average >= 87) && (average < 90))
{
cout<<"The average is a B+."<<endl;
}
elseif((average >= 90) && (average < 93))
{
cout<<"The average is an A-."<<endl;
}
elseif((average >= 93) && (average < 97))
{
cout<<"The average is an A."<<endl;
}
elseif((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;
}
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...
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.
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
// GradeMean.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
usingnamespace 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;
}
elseif((average >= 60) && (average < 63))
{
cout<<"The average is a D-."<<endl;
}
elseif((average >= 63) && (average < 67))
{
cout<<"The average is a D."<<endl;
}
elseif((average >= 67) && (average < 70))
{
cout<<"The average is a D+."<<endl;
}
elseif((average >= 70) && (average < 73))
{
cout<<"The average is a C-."<<endl;
}
elseif((average >= 73) && (average < 77))
{
cout<<"The average is a C."<<endl;
}
elseif((average >= 77) && (average < 80))
{
cout<<"The average is a C+."<<endl;
}
elseif((average >= 80) && (average < 83))
{
cout<<"The average is a B-."<<endl;
}
elseif((average >= 83) && (average < 87))
{
cout<<"The average is a B."<<endl;
}
elseif((average >= 87) && (average < 90))
{
cout<<"The average is a B+."<<endl;
}
elseif((average >= 90) && (average < 93))
{
cout<<"The average is an A-."<<endl;
}
elseif((average >= 93) && (average < 97))
{
cout<<"The average is an A."<<endl;
}
elseif((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;
}
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.