Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
最高和最低整数C++_C++_Integer_Highest - Fatal编程技术网

最高和最低整数C++

最高和最低整数C++,c++,integer,highest,C++,Integer,Highest,当我运行此程序时,会出现运行时检查失败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, highes

当我运行此程序时,会出现运行时检查失败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;
}

首先,cin>>numGrades[10]没有做它应该做的事情。numGrades[10]超出了numGrades数组的范围。我猜您希望读取最多10个表示等级的数字,计算平均值并找到最大和最小等级?此代码中存在多个错误。未定义的行为:使用未初始化的变量;从数组的末尾运行。更不用说一堆逻辑错误了。这里的问题太多了。你需要从头开始。我之前以为numGrades[10]会给我10个位置来保存输入的分数。用户被要求输入要处理的分数。您可以将numGrades声明为int,将grades声明为int数组,例如grades[10]。然后,您可以从0循环到numGrades并执行其他操作。但是,还需要进行一些其他更改,例如,您的变量“最高”和“最低”没有初始化,将它们与等级进行比较将是未定义的行为。