gpt4 book ai didi

c - 为什么我的程序打印 0 而不是计算出的正弦值?

转载 作者:行者123 更新时间:2023-11-30 21:14:42 25 4
gpt4 key购买 nike

#include <cs50.h>
#include <stdio.h>

float sine(float a, float b);

int main()
{
printf ("Choose an option:\n1- Sin\n2-Cos\n3-Tan\n");
int option = get_int();
if (option==1)
{
printf("What is the hypotnuse of the triangle?\n");
float hypotnuse = get_float();
printf("What is the opposite side of the triangle?\n");
float opposite = get_float();
sine(opposite, hypotnuse);
printf ("The answer is %f", sine);

}
// else … cosine, tangent
}

float sine(float a, float b)
{
return a/b;
}

无论我为 hypotnuseopposite 提供什么值,我得到的输出都是 0.000000。请解释一下代码有什么问题?

最佳答案

sine 是函数 sine() 的地址。

您必须使用函数sine()的返回值:

float result = sine(opposite, hypotnuse);
printf("The answer is %f\n", result);

在 C 语言中有不同的方法可以做到这一点,另一个例子是像 jonathan-leffler 提议的那样“即时”使用结果:

printf("sine(%f, %f) = %f\n", opposite, hypotnuse, sine(opposite, hypotnuse));

这是有效的,因为对函数 sine() 的调用结果作为参数直接发送到 printf()

关于c - 为什么我的程序打印 0 而不是计算出的正弦值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com