scanf函数为什么取不到双精度浮点数?

meitianjiaban meitianjiaban 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

/* 递归求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,由于数组作用 */
}
}
...全文
评论 6
收藏 已收藏
分享
返回顶部
meitianjiaban
C语言 70,039 成员 243,237 内容