//输入5个学生的成绩(保证分数在0~100)之间,算出平均分,最高分,最低分,然后输出
//计算平均值
int average(int array[],int n)
{
int sum = 0;
for (int i = 0; i <= n ; i++) {
sum += array[i];
}
return sum/5;
}
//计算最大值
int Max(int array[],int n)
{
int max = array[0];
for (int i = 0; i <= n; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
//计算最小值
int low(int array[],int n){
int min =array[0];
for (int i = 0;i <= n;i++){
if(array[i]< min){
min = array[i];
}
}
return min;
}
int main(){
int score[5] = {-1,-1,-1,-1,-1};
for (int i = 0;i < 5;i++) {
while (score[i] < 0 || score[i] > 100) {
printf("请输入第%d个学生的成绩:\n",i+1);
scanf("%d",&score[i]);
}
}
int length = sizeof(score)/sizeof(int);
printf("%d\n",length);
int a = average(score, length);
int b = Max(score, length);
int c = low(score,length);
printf("平均分是%d,最高分是%d,最低分是%d\n",a,b,c);
return 0;
}
为什么最低分输出是对的,而最高分和平均分输出缺是错的啊 |
|