我最近尝试制作一个计算最高考试成绩,最低考试成绩和所有考试成绩平均值的计划。
到目前为止,我的计划已经能够做到这一切;然后,我试着看看我是否可以计算所有平均值中的最高值,并且所有AVERAGES中的最低值,我无法弄明白。
我已经尝试了很多东西,到目前为止还没有任何工作。现在我得到的只是所有的考试成绩,他们的平均成绩,最高的考试成绩和最低,但不是平均成绩的最高和最低。我能做些什么来更好地解决这个问题?
任何提示,建议都非常感谢。我将在下面列出我迄今为止所做的工作。
#include <iostream>
#include <iomanip>
using namespace std;
const int amount = 3;
int count;
int Total = 0;
int TestScore1[amount], TestScore2 [amount], TestScore3 [amount];
double average[amount];
int main()
{
for(int count = 0; count < amount; count++)
{
cout<<"Enter Test Score " << (count + 1) << ": " <<endl;
cin>> TestScore1[count];
cin>>TestScore2[count];
cin>>TestScore3[count];
}
cout<<"Your test result are: \n Test 1: Test 2: Test 3: Average: "<<endl;
for(int count = 0; count < amount; count++)
{
Total = 0;
double average = 0;
Total += TestScore1[count];
Total += TestScore2[count];
Total += TestScore3[count];
average = Total / amount;
cout << " " << TestScore1[count]
<< " \t" << TestScore2[count]
<<" \t "<< TestScore3[count] <<"\t" << average <<endl;
}
double highest = TestScore1[0];
for(int count = 1; count < amount; count++)
{
if(TestScore1[count] > highest)
highest = TestScore1[count];
}
cout<<"Test 1 Highest: " <<highest <<endl;
double highest2 = TestScore2[0];
for(int count = 1; count < amount; count++)
{
if(TestScore2[count] > highest)
highest2 = TestScore2[count];
}
cout<<"Test 2 Highest: " <<highest2 <<endl;
double highest3 = TestScore3[0];
for(int count = 1; count < amount; count++)
{
if(TestScore3[count] > highest)
highest3 = TestScore3[count];
}
cout<<"Test 3 Highest: " <<highest3 <<endl;
double Lowest = TestScore1[0];
for(int count = 1; count < amount; count++)
{
if(TestScore1[count] < Lowest)
Lowest = TestScore1[count];
}
cout<<"Test 1 Lowest: " <<Lowest <<endl;
double Lowest2 = TestScore2[0];
for(int count = 1; count < amount; count++)
{
if(TestScore2[count] < Lowest2)
Lowest2 = TestScore2[count];
}
cout<<"Test 2 Lowest: " <<Lowest2 <<endl;
double Lowest3 = TestScore3[0];
for(int count = 1; count < amount; count++)
{
if(TestScore3[count] < Lowest3)
Lowest3 = TestScore3[count];
}
cout<<"Test 3 Lowest: " <<Lowest3 <<endl;
}
答案 0 :(得分:2)
在第二个循环中,您声明一个名为average
的变量double average = 0;
然后为其指定平均值。此变量隐藏您在外部声明的数组,也称为average。您需要从程序中删除此变量声明并使用
直接写入数组average[count] = Total / amount;
这样,实际存储了平均值,可以在以后访问。