1个回答
2013-07-16
展开全部
函数返回平均值,最大值和最小值通过指针参数返回。考虑到输入个数不合理的情况,当个数n小于等于0时,返回值、最大值和最小值都设为0。
#include <stdio.h>
double fun(double a[], int n, double *max, double *min)
{
double sum;
int i;
if(n <= 0)
{
*max = *min = 0;
return 0;
}
sum = *max = *min = a[0];
for(i = 1; i < n; i++)
{
sum += a[i];
if(a[i] > *max)
*max = a[i];
else if(a[i] < *min)
*min = a[i];
}
return sum / n;
}
int main()
{
double a[10] = {5.3, 6.9, 7, 8.5, 9.2, 3, 4.5, 7.1, 3.8, 2.6};
double ave, max, min;
ave = fun(a, 10, &max, &min);
printf("平均数:%lf\n最大数:%lf\n最小数:%lf\n", ave, max, min);
return 0;
}
#include <stdio.h>
double fun(double a[], int n, double *max, double *min)
{
double sum;
int i;
if(n <= 0)
{
*max = *min = 0;
return 0;
}
sum = *max = *min = a[0];
for(i = 1; i < n; i++)
{
sum += a[i];
if(a[i] > *max)
*max = a[i];
else if(a[i] < *min)
*min = a[i];
}
return sum / n;
}
int main()
{
double a[10] = {5.3, 6.9, 7, 8.5, 9.2, 3, 4.5, 7.1, 3.8, 2.6};
double ave, max, min;
ave = fun(a, 10, &max, &min);
printf("平均数:%lf\n最大数:%lf\n最小数:%lf\n", ave, max, min);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询