-3

when I run this program is a "Run-Time Check Failure #2 stack around the variable 'numGrades' was corrupted" appears. Also the lowest grade doesn't output the correct answer. Any help would be greatly appreciated!

#include <iostream> // cin, cout

using namespace std;

const int TEN_GRADES = 10;   // pre-defined number of grades

int main()
{
    int numGrades[10];
    double avg, highest = numGrades[0], lowest = numGrades[0], less, greater, grades;
    double sum = 0;

    // greeting message
    cout << "---------------------------------" << endl
         << "  Sandro's Statistics Generator  " << endl
         << "---------------------------------" << endl << endl;

    // requesting number of grades 
    cout << "Hello Professor, how many grades do I need to analyse this time? ";
    cin >> numGrades[10];

    if (numGrades[1] == 0)
    {
        cout << "\nGuess you changed your mind!!!" << endl
             << "Ending program now..." << endl << endl;
        return 0;
    }

    // if user doesn't enter 0 user is ready to begin
    cout << "Okay, I am ready. Start..." << endl;
    for (int count = 0; count < numGrades[10]; count++)
    {
        cin >> numGrades[count];
        sum += numGrades[10];
    }

    // to get the average
    avg = sum / TEN_GRADES;

    // to get the highest and lowest mark

    for (int count = 0; count < TEN_GRADES; count++)
    {
        if (numGrades[count] > highest)
            highest = numGrades[count];
    }

    for (int count = 0; count < TEN_GRADES; count++)
    {
        if (numGrades[count] < lowest)
            lowest = numGrades[count];
    }

    // output requested statistics
    cout << "Here are the requested stats for the " << numGrades << " grades."     << endl
         << "The class average is " << avg << endl
         << "The highest grade is " << highest << endl
         << "The lowest grade is " << lowest << endl;

    return 0;

}

Be Great
  • 11
  • 4
  • 1
    First of all, `cin >> numGrades[10]` does not do what it is supposed to. `numGrades[10]` is out of the bounds of `numGrades` array. – Polb Nov 14 '16 at 00:33
  • I guess you want to read maximum 10 numbers which represent grades, compute the average and find the maximum and the minimum grade? – Polb Nov 14 '16 at 00:36
  • 5
    There are multiple bugs in this shown code. Undefined behavior due to: use of uninitialized variables; running off the end of the array. Not to mentioned a bunch of logical bugs. There are too many problems here. You need to start over from scratch, and [be sure to talk to your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – Sam Varshavchik Nov 14 '16 at 00:36
  • Presicely, I thought numGrades[10] would give me 10 places to hold the entered grades. – Be Great Nov 14 '16 at 00:38
  • The user is asked to enter how many grades are going to be processed. You could declare `numGrades` to be an `int` and `grades` to be an array of `int`s e.g `grades[10]`. Then you can loop from 0 to `numGrades` and do the other operations (however, there are some other changes that need to be done, for instance your variables `highest` and `lowest` are not initialised and comparing them against a grade would be undefined behaviour). – Polb Nov 14 '16 at 00:46

1 Answers1

0

Oh god, I don't even do c++ but I think one of my eyes bled a little.

Please review (or tell whoever coded this to review) how to create and assign values to them.

Then review simple data structures (like arrays) and loops.

One good way to start is to analyze the following WORKING code of your program:

Please mark as correct if it helps, and if you have any questions... Cheers!

#include <iostream>
#include <string>

using namespace std;

const int TEN_GRADES = 10;   // pre-defined number of grades

int main()
{
int numGrades[10];
double avg, highest = 0, lowest = 0, less, greater, grades;
double sum = 0;

// greeting message
cout << "---------------------------------" << endl
     << "  Newbie Statistics Generator  " << endl
     << "---------------------------------" << endl << endl;

// requesting number of grades 
cout << "Hello Professor, please enter 10 grades: "<<endl;

//THIS PART: loops ten times to input the grades
for (int count = 0; count < TEN_GRADES; count ++)
{
    cout << "Grade number "<<count<<":";
    cin >> numGrades[count];

}

//I get what you want to do here, but consider adding another exit condition here, what if the second grade is really 0 ?
if (numGrades[1] == 0)
{
    cout << "\nGuess you changed your mind!!!" << endl
         << "Ending program now..." << endl << endl;
    return 0;
}

// if user doesn't enter 0 user is ready to begin
cout << "Okay, I am ready. Start..." << endl;
for (int count = 0; count < TEN_GRADES; count++)
{
    sum += numGrades[count];
}

// to get the average
avg = sum / TEN_GRADES;

// to get the highest and lowest mark
for (int count = 0; count < TEN_GRADES; count++)
{
    if (numGrades[count] > highest)
        highest = numGrades[count];
}

for (int count = 0; count < TEN_GRADES; count++)
{
    if (numGrades[count] < lowest)
        lowest = numGrades[count];
}
// output requested statistics
cout << "Here are the requested stats for the " << TEN_GRADES << " grades."     << endl
     << "The class average is " << avg << endl
     << "The highest grade is " << highest << endl
     << "The lowest grade is " << lowest << endl;

return 0;
}
rtcode
  • 103
  • 9