* 递归求n个数的积 */
/* 下面a[i]的取值是乱码 */
#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 )))
{
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,由于数组作用 */
}
等待分析
最新推荐文章于 2018-03-05 16:03:00 发布