c语言怎样求一个数组的最大最小和平均值
发布网友
发布时间:2022-05-14 05:07
我来回答
共2个回答
热心网友
时间:2024-02-24 08:14
int a[10] = {4, 2, 3, 4, 5, 6, 7, 9, 8, 3};
int i, max = 0, min = a[0], pinjun = 0, sum = 0;
for (i = 0; i < 10; i++) {
max = max > a[i] ? max : a[i];
min = min < a[i] ? min : a[i];
sum = sum + a[i];
}
pinjun = sum / 10;
printf("最大值:%d\n最小值:%d\n平均值:%d\n", max, min, pinjun);
热心网友
时间:2024-02-24 08:15
#include <stdio.h>
#include <limits.h>
int main()
{
int max=INT_MIN,min=INT_MAX;
float avg=0;
int array[] = {1,2,3,4,5,6,7,8,9,1000};
int arrayLen = 10;
int i = 0;
for (i = 0 ;i < arrayLen; i++){
if ( array[i] > max){
max = array[i];
}
if ( array[i] < min){
min = array[i];
}
avg += array[i]/(float)arrayLen;
}
printf("%d %d %f\n", max,min,avg);
return 0;
}