求c语言程序讲解: 用键盘输入一组数据,求最大值,最小值,和,积,平均值
#include<stdlib.h>
#include<stdio.h>
void main()
{
char ch = 'y'; //用户选择
int a = 0; //保存输入的数据
int i = 0; //统计输入的数据个数
float sum = 0.0,product = 1.0; //和,积
float average = 0.0; //平均值
int max = 0,min = 0; //最大值,最小值
for(i=0;ch == 'y';i++)
{
printf("请输入数据(整型):");
scanf("%d",&a);
sum = sum + a;
// printf("sum:%f\t",sum);
product = product * a;
// printf("product:%f\n",product);
if(i==0 || a > max)
max = a;
if(i==0 || a < min)
min = a;
printf("\n");
printf("是否需要继续输入?(是/y,不是/n):");
scanf("%s",&ch);
printf("\n");
if(ch == 'n')
{
//printf("i:%d\n",i);
average = sum/(i+1);
printf("最大值:%d\t最小值:%d\t平均值:%f\n和:%f\t积:%f\n",max,min,average,sum,product);
}
}
}
#define N 6
main()
{
int i,imax=0,imin=0,sum=0,a[N];
float aver = 0;
double multi=1;
imax=imin=sum=a[0];
printf("请输入6个数:");
for (i=0;i< N;i++)
{
scanf("%d",&a[i]);
sum +=a[i];
multi *=a[i];
}
//求最大最小值
for (i=0;i< N;i++)
{
if (imax <a[i])
{
imax = a[i];
}
if (imin >a[i])
{
imin = a[i];
}
}
aver = (float) sum/N;
printf("最大值为:%d,最小值为:%d,和为:%d,积为:%-10.0f,平均值为:%-5.2f。\n",imax,imin,sum,multi,aver);
}
main ()
{
int a[10];
int i, max,min,sum,p;
float avg;
printf("输入数据");
for(i = 0;i < 10;i ++)
scanf("%d",&a[i]);
max = min = sum = a[0];
p =1;
for(i = 1; i < 10;i ++)
{
sum += a[i];
p *= a[i];
if(max <a[i])
max = a[i];
if(min > a[i])
min = a[i];
}
avg = sum/10;
printf("最大值是 %d,最小值是 %d,和是 %d,积是 %d,平均值是 %f",max,min,sum,p,avg);
}