如何修复作业代码中的此错误
How can I fix this error in my code for an assignment?
分配:从Assignment 3.0(以前的赋值)开始,修改函数averageGrades(),使其不考虑值为-1的分数。在这种情况下,-1表示分配尚未完成,因此不应计入平均值。我正在使用一个名为testbed的程序来测试我的工作。它就像用户一样,比较实际结果和预期结果。我在一次测试中出现了舍入错误,并通过了其他测试,我不知道为什么:测试1对于1-1,有9个项目需要平均:90+86+95+76+92+83+100+87+91=800800/9=88.888请注意,我们在这里使用的是整数,因此值会被截断!
年级1:90二年级:86三年级:95四年级:76五年级:92六年级:83七年级:100八年级:87九年级:9110年级:-1平均品位:88%测试1通过。
测试2这里的列表中有一个-1,但它在在中间。90+86+95+92+83+100+87+91+76=800800/9=88.888
年级1:90二年级:86三年级:95四年级:-1五年级:92六年级:83七年级:100八年级:87九年级:9110年级:76平均成绩:89%实验:平均成绩:88%测试2失败。
测试3这是一个特殊情况。由于所有值都是-1,所以总和为0。然而,如果你试图计算平均值,你会得到0/0。既然我们不能除以零,这个就会崩溃。你需要检查对于这种带有IF语句的情况
1级:-1二年级:-1三年级:-1四年级:-1五年级:-1六年级:-1七年级:-1八年级:-1九年级:-110年级:-1平均品位:---%测试3通过。1/3测试失败。最后:这是我的代码:
Please help with this C++ assignment I'm working on?
从Assignment 3.0(以前的赋值)开始,修改函数averageGrades(),使其不考虑值为-1的分数。在这种情况下,-1表示分配尚未完成,因此不应计入平均值。我正在使用一个名为testbed的程序来测试我的工作。它就像用户一样,比较实际结果和预期结果。我在一次测试中出现了舍入错误,并通过了其他测试,我不知道为什么:测试1对于1-1,有9个项目需要平均:90+86+95+76+92+83+100+87+91=800800/9=88.888请注意,我们在这里使用的是整数,因此值会被截断!
年级1:90二年级:86三年级:95四年级:76五年级:92六年级:83七年级:100八年级:87九年级:9110年级:-1平均品位:88%测试1通过。
测试2这里的列表中有一个-1,但它在在中间。90+86+95+92+83+100+87+91+76=800800/9=88.888
年级1:90二年级:86三年级:95四年级:-1五年级:92六年级:83七年级:100八年级:87九年级:9110年级:76平均成绩:89%实验:平均成绩:88%测试2失败。
测试3这是一个特殊情况。由于所有值都是-1,所以总和为0。然而,如果你试图计算平均值,你会得到0/0。既然我们不能除以零,这个就会崩溃。你需要检查对于这种带有IF语句的情况
1级:-1二年级:-1三年级:-1四年级:-1五年级:-1六年级:-1七年级:-1八年级:-1九年级:-110年级:-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;
}
在发布这篇文章之前,我尝试了一些方法。我在"float average=sum/(num-notCompleted-1)"中添加了-1,这修复了一些问题。在那之前,我犯了很多错误。我还将这里的许多int转换为float,这也有帮助,但我所做的一切都无法消除这个错误。
我最终找到了这个。这是我想出的代码:
/***********************************************************************
* 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;
}
- 错误处理.将系统错误代码映射到泛型
- 为什么我在使用void函数时得到错误代码C2276
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 逻辑运算符上出现错误代码 a')'
- 'val' Arduino 错误代码之前的预期'('
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 根据 GetLastError 直接写入磁盘会导致错误代码 5
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 从 C++ 调用 cURL 命令会返回意外的错误代码,如 1792 和 6656
- WSALookupServiceBegin() 上的错误代码 10022(无效的 arugment)
- C++ Schannel POST 400 错误代码错误请求
- openCL-创建子缓冲区返回错误代码13
- 更新了Runge-Kutta(RK4)C++错误代码中的二阶DE
- 获取有关使用未初始化内存的错误代码
- 排序程序的意外错误代码
- 正在将DJI错误代码记录到流中
- 来自API的错误代码..处理什么是好的做法