如何在我的作业代码中修复此错误?

Living4God1991

作业:从作业 3.0(以前的作业)开始,修改函数 averageGrades(),使其不考虑值为 -1 的成绩。在这种情况下,-1 表示作业尚未完成,因此不应考虑平均值。我正在使用一个名为 testbed 的程序来测试我的工作。它就像用户一样,比较实际和预期的结果。我在一项测试中出现舍入错误并通过了其他测试,我不知道为什么:测试 1 有一个 -1,有 9 个项目需要平均:90 + 86 + 95 + 76 + 92 + 83 + 100 + 87 + 91 = 800 800 / 9 = 88.888 请注意,我们在这里使用整数,因此值会被截断!

1年级:90 2年级:86 3年级:95 4年级:76 5年级:92 6年级:83 7年级:100 8年级:87 9年级:91 10年级:-1 平均成绩:88% 测试1通过。

测试 2 这里的列表中有一个 -1,但它在中间。90 + 86 + 95 + 92 + 83 + 100 + 87 + 91 + 76 = 800 800 / 9 = 88.888

1年级:90 2年级:86 3年级:95 4年级:-1 5年级:92 6年级:83 7年级:100 8年级:87 9年级:91 10年级:76 平均成绩:89%\n Exp:平均成绩:88%\n 测试 2 失败。

测试 3 这是一个特例。由于所有值都是 -1,所以总和为 0。但是,如果您尝试计算平均值,则会得到 0/0。由于我们不能除以零,这将崩溃。您需要使用 IF 语句检查这种情况

1年级:-1 2年级:-1 3年级:-1 4年级:-1 5年级:-1 6年级:-1 7年级:-1 8年级:-1 9年级:-1 10年级:-1平均成绩:---% 测试 3 通过。1/3 测试失败。最后:这是我的代码:

Please help with this C++ assignment I'm working on?

从作业 3.0(以前的作业)开始,修改函数 averageGrades(),使其不考虑值为 -1 的成绩。在这种情况下,-1 表示作业尚未完成,因此不应考虑平均值。我正在使用一个名为 testbed 的程序来测试我的工作。它就像用户一样,比较实际和预期的结果。我在一项测试中出现舍入错误并通过了其他测试,我不知道为什么:测试 1 有一个 -1,有 9 个项目需要平均:90 + 86 + 95 + 76 + 92 + 83 + 100 + 87 + 91 = 800 800 / 9 = 88.888 请注意,我们在这里使用整数,因此值会被截断!

1年级:90 2年级:86 3年级:95 4年级:76 5年级:92 6年级:83 7年级:100 8年级:87 9年级:91 10年级:-1 平均成绩:88% 测试1通过。

测试 2 这里的列表中有一个 -1,但它在中间。90 + 86 + 95 + 92 + 83 + 100 + 87 + 91 + 76 = 800 800 / 9 = 88.888

1年级:90 2年级:86 3年级:95 4年级:-1 5年级:92 6年级:83 7年级:100 8年级:87 9年级:91 10年级:76 平均成绩:89%\n Exp:平均成绩:88%\n 测试 2 失败。

测试 3 这是一个特例。由于所有值都是 -1,所以总和为 0。但是,如果您尝试计算平均值,则会得到 0/0。由于我们不能除以零,这将崩溃。您需要使用 IF 语句检查这种情况

1年级:-1 2年级:-1 3年级:-1 4年级:-1 5年级:-1 6年级:-1 7年级:-1 8年级:-1 9年级:-1 10年级:-1平均成绩:---% 测试 3 通过。1/3 测试失败。最后:这是我的代码:

/***********************************************************************
* Program:
*    Assignment 31, Array Design
*    Sister Unsicker, CS124
* Author:
*    Lanie Molinar
* Summary: 
*    This program gets 10 grades from the user, averages them, and displays the
*    result.
*
*    Estimated:  2.0 hrs   
*    Actual:     1.5 hrs
*      I had some difficulty getting the style checker to not complain about 
*      my NUMGRADES constant. First, it didn't like the _ when I tried to 
*      name it NUM_GRADES, and when I tried using NUM-GRADES, it complained 
*      about there not being white space between operators. It finally stopped 
*      complaining when I used NUMGRADES.
************************************************************************/

#include <iostream>
using namespace std;

#define NUMGRADES 10

/***********************************************************************
* The function getGrades gets 10 grades from the user, passing them to main().
***********************************************************************/
void getGrades(float grades[], int num)
{
   for (int i = 0; i < num; i++)
   {
      cout << "Grade " << i + 1 << ": ";
      cin >> grades[i];
   }
   return;
}

/***********************************************************************
* The function averageGradesGrades averages the grades inputted by the user
* and returns that value to main().
***********************************************************************/
void averageGrades(float grades[], int num)
{
   float sum = 0;
   int notCompleted = 0;
   int i = 0;
   cout.setf(ios::fixed);
   cout.precision(0);
   while (i < num && (i + notCompleted != num))
   {
      if (grades[i] != -1)
      {
         sum += grades[i];
         i++;
      }
      else
         notCompleted++;
   }
   float average = sum / (num - notCompleted)- 1;
   if (notCompleted != num)
      cout << average;
   else
      cout << "---";
   return;
}

/**********************************************************************
*    The main function calls getGrades and averageGrades and displays the 
*   result returned by averageGrades.
***********************************************************************/
int main()
{
   float grades[NUMGRADES];
   getGrades(grades, NUMGRADES);
   cout << "Average Grade: ";
   averageGrades(grades, NUMGRADES);
      cout << "%\n";
   return 0;
}

在发布此内容之前,我尝试了一些方法。我将 - 1 添加到我的“浮动平均值 = sum / (num - notCompleted -1)”中,并修复了一些问题。在此之前,我的错误已经很严重了。我还将这里的许多整数转换为浮点数,这也有帮助,但我所做的一切都无法消除该错误。

Living4God1991

我最终想通了这一点。这是我想出的代码:

/***********************************************************************
* Program:
*    Assignment 31, Array Design
*    Sister Unsicker, CS124
* Author:
*    Lanie Molinar
* Summary:
*    This program gets 10 grades from the user, averages them, and displays 
*    the result. It doesn't count -1 as a completed assignment, so it won't 
*    count it when it computes the average.
*
*    Estimated:  2.0 hrs
*    Actual:     4.0 hrs
*      I had a lot of trouble making my code pass all 3 tests in testbed. I 
*      kept failing at least one every time. After working on it for a while,
*      though, I finally got it working.
************************************************************************/

#include <iostream>
using namespace std;

#define NUMGRADES 10

/***********************************************************************
* The function getGrades gets 10 grades from the user, passing them to main().
***********************************************************************/
void getGrades(int grades[], int num)
{
   for (int i = 0; i < num; i++)
   {
      cout << "Grade " << i + 1 << ": ";
      cin >> grades[i];
   }
   return;
}

/***********************************************************************
* The function averageGradesGrades averages the grades inputted by the user
* and returns that value to main(). It doesn't count -1's as completed 
* assignments, and if the user enters -1 for all grades, it outputs dashes 
* instead of the average.
***********************************************************************/
void averageGrades(int grades[], int num)
{
   int sum = 0;
   int notCompleted = 0;
   int average;
   for (int i = 0; i < num; i++)
   {
      if (grades[i] != -1)
         sum += grades[i];
      else
         notCompleted++;
   }
   if (sum != 0)
      average = sum / (num - notCompleted);
   if (notCompleted != num)
      cout << average;
   else
      cout << "---";
}

/**********************************************************************
*    The main function calls getGrades and averageGrades and displays the
*   result returned by averageGrades.
***********************************************************************/
int main()
{
   int grades[NUMGRADES];
   getGrades(grades, NUMGRADES);
   cout << "Average Grade: ";
   averageGrades(grades, NUMGRADES);
   cout << "%\n";
   return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在我的代码中修复此循环错误(循环估值错误)

来自分类Dev

如何在Java中修复此错误?

来自分类Dev

如何在我的haskell代码中更正此错误:

来自分类Dev

如何修复此代码中的“错误无效参数/选项 - 'Live'”?

来自分类Dev

如何修复此代码块中的错误输入?

来自分类Dev

如何在创建数据数组时修复我的 appsync 中的此错误

来自分类Dev

如何在SQL中的此示例中修复错误?

来自分类Dev

如何在 Android 聊天中修复 ListView 中的此错误

来自分类Dev

如何在Kotlin网络演示中修复此错误?

来自分类Dev

如何在Razor中修复此“语法错误”?

来自分类Dev

如何在Drupal网站中修复此错误?

来自分类Dev

如何在Django中修复此TemplateSyntaxError错误

来自分类Dev

如何在Global.asax中修复此错误WebApiConfig?

来自分类Dev

如何在Drupal网站中修复此错误?

来自分类Dev

如何在我的servlet中修复此NoClassDefFoundError

来自分类Dev

如何在我的servlet中修复此NoClassDefFoundError

来自分类Dev

如何修复我的jQuery代码中的关闭错误

来自分类Dev

如何修复我的代码中的错误,以便从文件制作字典

来自分类Dev

如何在浏览器中修复“我们无法到达此页面”错误或“ err_connection_timed_out”?

来自分类Dev

如何在Xcode 8.1中修复此错误(返回host_statistics)?我想知道设备的可用内存

来自分类Dev

如何在MySQL中修复错误代码1241

来自分类Dev

如何修复WPF中的此绑定错误?

来自分类Dev

如何修复 ExpandableListView 中的此错误?

来自分类Dev

如何在我的Java代码中获取此属性值?

来自分类Dev

我如何在C中实现此Matlab代码?

来自分类Dev

如何修复此错误

来自分类Dev

如何在Java中修复此异常?

来自分类Dev

如何在 Django、Queryset、filet、Attribute Error 中修复此错误,如何改进?

来自分类Dev

如何修复此代码,我不断收到无限循环?

Related 相关文章

  1. 1

    如何在我的代码中修复此循环错误(循环估值错误)

  2. 2

    如何在Java中修复此错误?

  3. 3

    如何在我的haskell代码中更正此错误:

  4. 4

    如何修复此代码中的“错误无效参数/选项 - 'Live'”?

  5. 5

    如何修复此代码块中的错误输入?

  6. 6

    如何在创建数据数组时修复我的 appsync 中的此错误

  7. 7

    如何在SQL中的此示例中修复错误?

  8. 8

    如何在 Android 聊天中修复 ListView 中的此错误

  9. 9

    如何在Kotlin网络演示中修复此错误?

  10. 10

    如何在Razor中修复此“语法错误”?

  11. 11

    如何在Drupal网站中修复此错误?

  12. 12

    如何在Django中修复此TemplateSyntaxError错误

  13. 13

    如何在Global.asax中修复此错误WebApiConfig?

  14. 14

    如何在Drupal网站中修复此错误?

  15. 15

    如何在我的servlet中修复此NoClassDefFoundError

  16. 16

    如何在我的servlet中修复此NoClassDefFoundError

  17. 17

    如何修复我的jQuery代码中的关闭错误

  18. 18

    如何修复我的代码中的错误,以便从文件制作字典

  19. 19

    如何在浏览器中修复“我们无法到达此页面”错误或“ err_connection_timed_out”?

  20. 20

    如何在Xcode 8.1中修复此错误(返回host_statistics)?我想知道设备的可用内存

  21. 21

    如何在MySQL中修复错误代码1241

  22. 22

    如何修复WPF中的此绑定错误?

  23. 23

    如何修复 ExpandableListView 中的此错误?

  24. 24

    如何在我的Java代码中获取此属性值?

  25. 25

    我如何在C中实现此Matlab代码?

  26. 26

    如何修复此错误

  27. 27

    如何在Java中修复此异常?

  28. 28

    如何在 Django、Queryset、filet、Attribute Error 中修复此错误,如何改进?

  29. 29

    如何修复此代码,我不断收到无限循环?

热门标签

归档