如何在不使用c ++的数组的情况下找到某些输入的平均值

时间:2014-04-11 00:34:13

标签: c++

请不要将其复制并粘贴为您自己的作品

这是我到目前为止所要求的,它要求用户提供考试分数,并根据分数将其分类。跑完后,我添加了每个类别的分数。现在我试图在跑步后找到每个类别的平均分数。

{
    int score = 0;
    int repeat = 0;

    cout << "How many exam scores?" << endl;
    cin >> repeat;

    int outstanding = 0;
    int satisfactory = 0;
    int unsatisfactory = 0;
    int sum = 0;

    cout << "Please enter an exam score:Don't Copy and Paste for Homework " << endl;
    cin >> score;

    for(int count = 0; count < repeat; count++)
    {
         sum += score
         if ( (score <= 100) && (score >= 90) )
          outstanding++;
           else if ( (score <= 89) && (score >=60) )
             satisfactory++;
             else if ( (score >= 0) && (score <= 59) )
               unsatisfactory++;
                 else if (score > 100)
                   cout << "ERROR EXAM THEY CHEATED GRADE INVALID" << endl;
         cout << endl<< "Enter the next score: ";
         cin >> score;


    }
    cout << "__________________________________________" << endl;
    cout << "The number of Outstanding scores is: " << outstanding << endl;
    cout << "The average of Outstanding scores is: " << outstanding/sum << endl;
    cout << "__________________________________________" << endl;
    cout << "The number of Satisfactory scores is: " << satisfactory << endl;
    cout << "The number of Satisfactory scores is: " << satisfactory/sum << endl;
    cout << "__________________________________________" << endl;
    cout << "The number of Unsatisfactory scores is: " << unsatisfactory << endl;
    cout << "The number of Unsatisfactory scores is: " << unsatisfactory/sum << endl;
    cout << "__________________________________________" << endl;


    system("PAUSE");
    return EXIT_SUCCESS;
}

2 个答案:

答案 0 :(得分:0)

为此,您只需添加一些变量即可。使用int sumOutstanding = 0, sumSatisfactory = 0, sumUnsatisfactory = 0;

for(int count = 0; count < repeat; count++)
    {
         if ( (score <= 100) && (score >= 90) ) {
           outstanding++;
           sumOutstanding += score;
         } else if ( (score <= 89) && (score >=60) ) {
           satisfactory++;
           sumSatisfactory += score;
         } else if ( (score >= 0) && (score <= 59) ) {
           unsatisfactory++;
           sumUnsatisfactory += score;
          } else if (score > 100)
           cout << "ERROR EXAM tHEY cHEATED GRADE INVALID" << endl;
         cout << endl<< "Enter the next score: ";
         cin >> score;


    }

然后输出平均值,就像你已经使用和整数一样。

答案 1 :(得分:0)

您只需添加一些变量来记录每次迭代中不同变量的总和。使用int sumOutstanding = 0sumSatisfactory = 0sumUnsatisfactory = 0作为上一个答案;在for循环的每次迭代中,您都可以跟踪每个参数的总和,最后将它们除以count

for(int count = 0; count < repeat; count++)
    {
         if ( (score <= 100) && (score >= 90) ) {
           outstanding++;
           sumOutstanding += score;
         } else if ( (score <= 89) && (score >=60) ) {
           satisfactory++;
           sumSatisfactory += score;
         } else if ( (score >= 0) && (score <= 59) ) {
           unsatisfactory++;
           sumUnsatisfactory += score;
          } else if (score > 100)
           cout << "ERROR EXAM tHEY cHEATED GRADE INVALID" << endl;
         cout << endl<< "Enter the next score: ";
         cin >> score;
    }