利用数组求平均值
Write a program that uses an array to hold course grades. Array is initialized to zero in the beginning.
Program then asks user to enter student number and then asks
user to enter grade for the student in question.
Program must check that array bounds are not violated.
User terminates input by entering -1 as student number.
In the end program calculates and prints the average of
course grades.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int grades[20];
int index;
int value;
int sum;
int i=0;
float average;
while(1){
printf("Enter student number: ");
fflush( stdin );
scanf("%d", &index);
if (index >= 0 && index < 20)
{
printf("Enter grade for student number %d:", index);
scanf("%d", &value);
grades[index] = value;
}
else if(index==-1)
break;
else
{
printf("You entered an invalid student number\n");
}
sum+=grades[i];
i++;
}
printf("the average grade is %8.3f",average=sum/i);
return 0;
}
利用数组求平均值
输出的数字好大啊
=。=