作业:从作业 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)”中,并修复了一些问题。在此之前,我的错误已经很严重了。我还将这里的许多整数转换为浮点数,这也有帮助,但我所做的一切都无法消除该错误。
我最终想通了这一点。这是我想出的代码:
/***********************************************************************
* 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] 删除。
我来说两句