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

meitianjiaban 2011-09-21 08:14:13

/* 递归求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,由于数组作用 */
}
}
...全文
173 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shexinwei 2011-09-21
  • 打赏
  • 举报
回复
其他子程序没看,关键问题在于内存分配错误,分配的是int型空间,而应该是double类型

#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,由于数组作用 */
}
}
meitianjiaban 2011-09-21
  • 打赏
  • 举报
回复
知道了双精度必须是"%lf"。调试成功

[Quote=引用 2 楼 jake443403168 的回复:]
C/C++ code


#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……
[/Quote]
meitianjiaban 2011-09-21
  • 打赏
  • 举报
回复
知道了双精度必须是"%lf"。调试成功
[Quote=引用 1 楼 athenacle_ 的回复:]
scanf("%lf",&a[i]);
[/Quote]
meitianjiaban 2011-09-21
  • 打赏
  • 举报
回复
代码重发遍,上面代码有点小问题
/* 递归求n个数的积 */
#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 )))
{

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,由于数组作用 */
}
Jake443403168 2011-09-21
  • 打赏
  • 举报
回复

#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,由于数组作用 */
}
}

Athenacle_ 2011-09-21
  • 打赏
  • 举报
回复
scanf("%lf",&a[i]);

69,380

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧