/*随机产生20个10到50的正整数存到数组中,
(1)求数组中的所有元素最大值、最小值、平均值以及各元素之和(20分)
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[20];
double average;
int max=0;
int mini=100;
int sum=0;
for(int i = 0;i<20;i++)
{
//初始化数组
arr[i] = arc4random_uniform(41) + 10;
//打印元素
printf("%d\n",arr[i]);
//最值
max = max>arr[i]?max:arr[i];
mini = mini<arr[i]?mini:arr[i];
//printf("最大值=%d\n",max);
//printf("最小值值=%d\n",mini);
//和
sum = sum + arr[i];
//printf("和是%d\n",sum);
//平均值
average = (double)sum/20;
//printf("平均数是%f\n",average);
}
printf("最大值=%d\n",max);
printf("最小值值=%d\n",mini);
printf("和是%d\n",sum);
printf("平均数是%f\n",average);
return 0;
}
记住,这只是第一问啊!!!!!! |
|