练习 03-06-01
有一个一维数组,存放十个学生的成绩,每一个函数,当主函数调用此函数时,能求出平均分、最低分和最高分。
输出样例:
The original array:
118 70 123 103 18 66 76 78 133 141
The max = 141;
The min = 18;
The average = 92.60
--------------------------------
Process exited after 0.02273 seconds with return value 0
请按任意键继续. . .
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int Max=0,Min=0;
int main()
{
srand((unsigned)time(NULL));
float average(int array[]);
int a[10],i;
printf("The original array:\n");
for(i=0;i<10;++i){
a[i]=rand()%150;
printf("%5d",a[i]);
}
float ave;
ave=average(a);
printf("\nThe max = %d;\nThe min = %d;\nThe average = %.2f",Max,Min,ave);
return 0;
}
float average(int array[])
{
int i;
float ave,sum=0.0;
Max=array[0],Min=array[0];
for(i=0;i<10;++i){
if(Max<array[i]){
Max=array[i];
}else if(Min>array[i]){
Min=array[i];
}
sum+=array[i];
}
ave=sum/10;
return ave;
}