尝试编写一个简单的程序,它取六个测试等级,由用户输入,降低最低,然后显示其余五个的平均值。该程序编译良好,但输出错误的答案。任何指向逻辑错误发生位置的指针都会有所帮助。我使用这些值作为样本等级: 85.2 92.3 78.0 51.5 91.6 87.0 返回的值应为86.82,但程序返回80.08。
<script>
var theimagestwo = new Array();
Draggablenumber[1] = $("#draggable1");
DraggableNumber[2] = $("#draggable2");
答案 0 :(得分:-1)
你的问题可能来自于你使用const int
进行双精度除法的事实。如果您将SIZE
转换为double
,并在末尾用.0写下剩余的双数字,那么它可以正常工作:
#include <iostream>
using namespace std;
const int SIZE = 6;
double grades[SIZE];
double low(double[]);
int main()
{
double preavg = 0.0;
double avg = 0.0;
cout << "This program will take an input of six test grades\n"
<< "and determine the average of the highest five grades.\n\n"
<< "Please input the first test grade\n";
cin >> grades[0];
cout << "Please input the second test grade\n";
cin >> grades[1];
cout << "Please input the third test grade\n";
cin >> grades[2];
cout << "Please input the fourth test grade\n";
cin >> grades[3];
cout << "Please input the fifth test grade\n";
cin >> grades[4];
cout << "Please input the sixth test grade\n";
cin >> grades[5];
for (int i = 0; i < SIZE; i++)
{
preavg += grades[i];
}
avg = (preavg - low(grades)) / ((double)SIZE - 1.0);
cout << "Your average grade is " << avg << "\n";
system("PAUSE");
return 0;
}
double low(double grades[SIZE])
{
double lowest;
lowest = 100.0;
for (int i = 0; i < SIZE; i++)
{
if (grades[i] < lowest)
lowest = grades[i];
}
return lowest;
}