最高和最低整数c++

Highest and Lowest Integer C++

本文关键字:c++ 整数      更新时间:2023-10-16

当我运行这个程序时,出现了"运行时检查失败#2堆栈周围的变量'numGrades'已损坏"。最低的分数也不会输出正确的答案。任何帮助将非常感激!

#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;

}

哦,天哪,我甚至不会c++,但我觉得我的一只眼睛有点流血。

请检查(或告诉编写此代码的人检查)如何创建和分配值。

然后复习简单的数据结构(如数组)和循环。

一个好的开始方法是分析你的程序的以下工作代码:

如果有帮助,请标记为正确,如果你有任何问题…干杯!

#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;
}