69,380
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
double Ave(double *,int );
int main()
{
int i=0,n=0;
double aveval=0.0;
double *a=NULL;
printf("Please enter the count of numbers :\n");
scanf("%d",&n);
if(NULL==(a=(int *)malloc( sizeof(int)*n ))) //修改为: a=(double *)malloc( sizeof(double)*n )
{
printf(" Application space failure. ");
exit(1);
}
printf("Please input %d numbers:\n",n);
for(i=0;i<n;i++)
{
printf("It's the %dth number:",i+1);
scanf("%f",&a[i]); /* 这儿有问题 */
}
for(i=0;i<n;i++)
{
printf("It's the %dth number:%f\n",i+1,a[i]);/* */
}
aveval=Ave(a,n-1);
free(a);
printf("avevalue=%f\n\n",aveval);
printf("Please press any key to quit..\n");
return 0;
}
double Ave(double *a,int n)
{
if(n==0)
return a[0];
else
{
return (a[n]+Ave(a,n-1)*n)/(n+1); /* 个数必须加1,由于数组作用 */
}
}
#include <stdio.h>
#include <stdlib.h>
double Ave(double *,int );
int main()
{
int i=0,n=0;
double aveval=0.0;
double *a=NULL;
printf("Please enter the count of numbers :\n");
scanf("%d",&n);
if(NULL==(a=(double *)malloc( sizeof(double)*n )))//写double把,你的a是double类型的
{
printf(" Application space failure. ");
exit(1);
}
printf("Please input %d numbers:\n",n);
for(i=0;i<n;i++)
{
printf("It's the %dth number:",i+1);
scanf("%lf",&a[i]); /* 这儿有问题 */ //双精度用%lf,单精度用f
}
for(i=0;i<n;i++)
{
printf("It's the %dth number:%f\n",i+1,a[i]);/* */
}
aveval=Ave(a,n-1);
free(a);
printf("avevalue=%f\n\n",aveval);
printf("Please press any key to quit..\n");
return 0;
}
double Ave(double *a,int n)
{
if(n==0)
return a[0];
else
{
return (a[n]+Ave(a,n-1)*n)/(n+1); /* 个数必须加1,由于数组作用 */
}
}