输出double类型的变量时出现问题

TwenteMaster 2011-11-30 10:26:46
我用的编译器是MinGW 4.6.1
源程序:
/* Exercise 5.1 Get the reciprocals */
#include <stdio.h>

int main(void)
{
double values[5] = {0.0}; /* Store five doubles */
double reciprocals[5] = {0.0}; /* Store the reciprocals of the five values */
double sum = 0.0; /* Store the sum of reciprocals */

printf("Enter five doubles you want: \n");
for(int i = 0 ; i < 5 ; i++)
{
scanf("%lf" , &values[i]);
}

/* Get the reciprocals */
for(int i = 0 ; i < 5 ; i++)
{
reciprocals[i] = 1.0 / values[i];
printf("values[%d] is: %.3lf\n" , i , values[i]);
}

/* Display the sum of the reciprocals */
for(int i = 0 ; i < 5 ; i++)
{
sum += reciprocals[i];
}
printf("The sum of reciprocals is: %.3lf\n" , sum);
return 0;
}
程序运行后输出的结果全部是0.000,请问这是为什么?
...全文
151 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
TwenteMaster 2011-11-30
  • 打赏
  • 举报
回复
我是看到书上写的:double类型的要用%lf,我该为%f后程序就没有问题了,我也不知道这是为什么,难道书上也出错了?
[Quote=引用 8 楼 nnrroo 的回复:]

C/C++ code
/* Exercise 5.1 Get the reciprocals */
#include <stdio.h>

int main(void)
{
double values[5] = {0.0}; /* Store five doubles */
double reciprocals[5] = {0.0}; /* Store the reciproc……
[/Quote]
狂且 2011-11-30
  • 打赏
  • 举报
回复
需要, 这 for() 里的 i 是局部变量. 作用域只是在 for() 内部.
到下一个 for() 就不起作用了.
[Quote=引用 6 楼 lost_goast 的回复:]

i变量需要在每一个for()里面都要重新定义一次吗??
[/Quote]
狂且 2011-11-30
  • 打赏
  • 举报
回复
/* Exercise 5.1 Get the reciprocals */
#include <stdio.h>

int main(void)
{
double values[5] = {0.0}; /* Store five doubles */
double reciprocals[5] = {0.0}; /* Store the reciprocals of the five values */
double sum = 0.0; /* Store the sum of reciprocals */

printf("Enter five doubles you want: \n");
for(int i = 0 ; i < 5 ; i++)
{
scanf("%lf" , &values[i]); //取值的时候用 %lf 就行.
}

/* Get the reciprocals */
for(int i = 0 ; i < 5 ; i++)
{
reciprocals[i] = 1.0 / values[i];
printf("values[%d] is: %.3f\n" , i , values[i]); // 改成 %.3f 就可以了.
}

/* Display the sum of the reciprocals */
for(int i = 0 ; i < 5 ; i++)
{
sum += reciprocals[i];
}
printf("The sum of reciprocals is: %.3f\n" , sum); // 同上

return 0;
}

/*
**********************************************************************
我用CodeBlocks, GNU-GCC 编译的. 我也不清楚为啥获取值的时候用 %lf,
打印的时候就用 %f. 不然也会出现你这个问题, 输出是 0. 求高手
讲解. 另外, 没改正之前, VS2008运行你的程序没有问题. 结果正常.
**********************************************************************
*/
月夜csdn 2011-11-30
  • 打赏
  • 举报
回复
#include <stdio.h>

int main(void)
{
double values[5] = {0.0}; /* Store five doubles */
double reciprocals[5] = {0.0}; /* Store the reciprocals of the five values */
double sum = 0.0; /* Store the sum of reciprocals */
int i; //把后边用到的i提前声明在了这里

printf("Enter five doubles you want: \n");
for(i = 0 ; i < 5 ; i++)
{
scanf("%lf" , &values[i]);
}

/* Get the reciprocals */
for(i = 0 ; i < 5 ; i++)
{
reciprocals[i] = 1.0 / values[i];
printf("values[%d] is: %.3lf\n" , i , values[i]);
}

/* Display the sum of the reciprocals */
for(i = 0 ; i < 5 ; i++)
{
sum += reciprocals[i];
}
printf("The sum of reciprocals is: %.3lf\n" , sum);
return 0;
}


我用VC6.0试了下。应该是没问题。一切正常
lost_goast 2011-11-30
  • 打赏
  • 举报
回复
i变量需要在每一个for()里面都要重新定义一次吗??
Athenacle_ 2011-11-30
  • 打赏
  • 举报
回复
gcc表示毫无问题。。MinGW怎么就出错了呢?

wangxiao@Main-WangXiao ~$ ./a.out
Enter five doubles you want:
1.2
2.3
3.4
4.5
5.6
values[0] is: 1.200
values[1] is: 2.300
values[2] is: 3.400
values[3] is: 4.500
values[4] is: 5.600
The sum of reciprocals is: 1.963

無_1024 2011-11-30
  • 打赏
  • 举报
回复

#include <stdio.h>

int main(void)
{
double values[5] = {0.0}; /* Store five doubles */
double reciprocals[5] = {0.0}; /* Store the reciprocals of the five values */
double sum = 0.0; /* Store the sum of reciprocals */
int i = 0;
printf("Enter five doubles you want: \n");
for(i = 0 ; i < 5 ; i++)
{
scanf("%lf" , &values[i]);
}

/* Get the reciprocals */
for( i = 0 ; i < 5 ; i++)
{
reciprocals[i] = 1.0 / values[i];
printf("values[%d] is: %.3lf\n" , i , values[i]);
}

/* Display the sum of the reciprocals */
for( i = 0 ; i < 5 ; i++)
{
sum += reciprocals[i];
}
printf("The sum of reciprocals is: %.3lf\n" , sum);
return 0;
}
//VC6.0运行正常的
赵4老师 2011-11-30
  • 打赏
  • 举报
回复
printf("values[%d] is: %lg\n" , i , values[i]);
单步调试和设断点调试是程序员必须掌握的技能之一。

xxweilw 2011-11-30
  • 打赏
  • 举报
回复
还有后面的int i没问题吗,把后边两个int i = 0变成i=0吧
xxweilw 2011-11-30
  • 打赏
  • 举报
回复
你输入值的时候是不是没回车啊,输入一个回车一次

69,371

社区成员

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

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