#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define Amount 10
void fun(int *a,int *max,int *min,float *average){
int i,sum=0;
*max=a[0];
*min=a[0];
sum+=a[0];
for(i=1;i<Amount;i++){
if(*max<a[i])
*max = a[i];
if(*min>a[i])
*min = a[i];
sum+=a[i];
}
*average = sum/Amount*1.0;
}
int main(){
int a[10],max,min;
float average=0;
srand((unsigned)time(NULL));
for(int i = 0 ;i<Amount;i++){
a[i]=rand()%100+1;
}
printf("数组初始化:\n");
for(int i = 0 ;i<Amount;i++){
printf("%d ",a[i]);
}
fun(a,&max,&min,&average);
printf("\n最大值:%d最小值%d平均值%.1f\n",max,min,average);
return 0;
}
通过随机数给数组赋值,通过函数求出数组的最大值最小值平均值
最新推荐文章于 2023-03-21 19:33:35 发布