在C语言中,我的函数输出总是0.000000。是因为这两个输入是int吗?
我知道你通常不打算把所有的代码都写出来,但这很简短,可以帮助解决这个问题。谁能解释一下为什么输出为0,以及我如何更改代码以输出一个圆锥体的体积在C语言中,我的函数输出总是0.000000。是因为这两个输入是int吗?,c,function,int,output,C,Function,Int,Output,我知道你通常不打算把所有的代码都写出来,但这很简短,可以帮助解决这个问题。谁能解释一下为什么输出为0,以及我如何更改代码以输出一个圆锥体的体积 #include <stdio.h> float ConeVolume(int height, int radius); float ConeVolume(int height, int radius) { float pi; pi = 3.14159; float third; third = (1/3)
#include <stdio.h>
float ConeVolume(int height, int radius);
float ConeVolume(int height, int radius)
{
float pi;
pi = 3.14159;
float third;
third = (1/3);
float vol;
vol = third * pi * radius * radius * height;
return vol;
}
int main()
{
float x = ConeVolume(12,10);
printf("%.4f \n", x);
}
#包括
浮点数(整数高度、整数半径);
浮点圆锥曲线(整数高度、整数半径)
{
浮点数;
pi=3.14159;
浮动第三;
三是(1/3),;
浮标体积;
vol=第三个*pi*半径*半径*高度;
返回卷;
}
int main()
{
浮点数x=圆锥曲线(12,10);
printf(“%.4f\n”,x);
}
编辑:感谢所有这么快回答的人。这里的社区很棒
1/3
是整数除法,结果总是0
要将此值计算为浮点变量,可以执行以下操作
1./3
或
或
或者更明确
(float)1/(float)3
例如。试试这个
#include <stdio.h>
float ConeVolume(int height, int radius)
{
float pi, vol;
pi = 3.14159;
vol = (pi * radius * radius * height) / 3;
return vol;
}
void main()
{
float x = ConeVolume(12,10);
printf("%.4f \n", x);
system("pause");
}
#包括
浮点圆锥曲线(整数高度、整数半径)
{
浮点数pi,vol;
pi=3.14159;
体积=(圆周率*半径*半径*高度)/3;
返回卷;
}
void main()
{
浮点数x=圆锥曲线(12,10);
printf(“%.4f\n”,x);
系统(“暂停”);
}
使用所有警告和调试信息编译(gcc-Wall-Wextra-g
)。然后通过单步执行程序来使用调试器(gdb
)。你会很快发现你的bug(比在这里询问更快)。SOI上有无数重复的bug(不是很严重),它不是在那里运行的/
操作符:),但随后你展示了其他变体,我想很明显你在划分浮动:)啊,是的,错过了/
-运算符。。。dxxm,再次失败;-)@Johanneschaub litbIt不是/
运算符,它是应用于1.
和3
(float)1/(float)3
#include <stdio.h>
float ConeVolume(int height, int radius)
{
float pi, vol;
pi = 3.14159;
vol = (pi * radius * radius * height) / 3;
return vol;
}
void main()
{
float x = ConeVolume(12,10);
printf("%.4f \n", x);
system("pause");
}