所以我有这个程序应该在使用数组的50项测验中询问5名学生用户。它必须输出测验的平均分数和最高分,还必须显示找到该元素(最高分)的索引。到目前为止,我已经找到了如何找到平均值,我也有最高的解码,但它给了我错误的输出..这是我到目前为止的进展。
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
const int input = 5;
int student[input];
int sum=0;
int ave=0;
cout<<"Please enter the score of "<<input<<" student\n";
for(int i=0; i<input; i++)
{
cout<<"student "<<i+1<<":";
cin>>student[i];
sum+=student[i];
ave= sum/input;
}
int highest=1;
int a=student[0];
for(i=1; i<a; i++)
{
if(student[i]<highest)
highest=student[i];
}
cout<<"Average score of the student is "<<ave<<"."<<endl;
cout<<"Highest score is "<<highest<<"."<<endl;
getch();
return 0;
}
谁能告诉我这里的错误在哪里?自昨晚以来我一直在解决这个问题... :(
答案 0 :(得分:1)
您正在寻找最高或最低?
if(student[i] < highest)
highest = student[i]; // ?
这是什么:
int a = student[0]; // example the user enters 100 then
for(i = 1; i < a; i++) // i < 100 reading outbands of the array
声明一个变量来存储最高值的索引并将其更正为:
int index = 0;
for(i = 0; i < input; i++)
{
if(student[i] > highest) // > not <
{
highest = student[i];
index = i;
}
}
cout << "Average score of the student is " << ave << "."<<endl;
cout << "Highest score is " <<highest << "." << endl;
cout << "index of highest score: " << index << endl;