空气中声速的C程序不能给出正确的结果

空气中声速的C程序不能给出正确的结果,c,C,该计划是: #include <stdio.h> #include <math.h> #define s 1086 /* Function Prototypes */ void directions(void); float temp(void); float calc_speed(float temp); void display(float temp, float calc_speed); void main() { float speed; f

该计划是:

#include <stdio.h>
#include <math.h>

#define s 1086

/* Function Prototypes */
void directions(void);
float temp(void);
float calc_speed(float temp);
void display(float temp, float calc_speed);

void main()
{
    float speed;
    float tempy;

    /* Calling Functions */
    directions();
    temp();
    speed = calc_speed(tempy);
    display(tempy, speed);
}

/* Sub Program for directions */
void directions(void)
{
    printf("Enter the temperature T in farienheit> ");
}

/* Sub program for temp */
float temp(void)
{
    float t;

    scanf("%f", &t);

    return(t);
}

/* Calculating speed */
float calc_speed(float temp)
{
    float ss;

    ss = s * (sqrt(5* temp +297)/(247));

    return(ss);
}

/* Displaying Results */
void display(float temp, float calc_speed)
{
    printf("The speed of sound at %f fareinhiet is : %f", temp, calc_speed);
}
#包括
#包括
#定义s 1086
/*功能原型*/
无效指示(无效);
浮动温度(无效);
浮子计算速度(浮子温度);
无效显示(浮动温度、浮动计算速度);
void main()
{
浮动速度;
浮躁;
/*调用函数*/
方向();
温度();
速度=计算速度(tempy);
显示(速度、速度);
}
/*方向子程序*/
无效方向(无效)
{
printf(“在farienheit>中输入温度T”);
}
/*temp子程序*/
浮动温度(无效)
{
浮动t;
scanf(“%f”、&t);
返回(t);
}
/*计算速度*/
浮子计算速度(浮子温度)
{
浮动ss;
ss=s*(sqrt(5*温度+297)/(247));
返回(ss);
}
/*显示结果*/
无效显示(浮动温度、浮动计算速度)
{
printf(“在%f时的声速为:%f”,温度,计算速度);
}

下面链接中的程序说明。

您的错误是,根据说明,分母应该在平方根内

ss = s * (sqrt(5* temp +297)/(247));
应该是

ss = s * (sqrt((5* temp +297)/(247)));
编辑:正如另一个答案所提到的,您的变量
tempy
从未初始化过,您需要使用
temp()
的返回值为其赋值


您可以在这里获得用户输入

float temp(void)
{
    float t;

    scanf("%f", &t);

    return(t);
}
但您不会对返回的值做任何操作

 temp();
 speed = calc_speed(tempy);
所以用一个未初始化的值调用Cacl_speed 试一试


旁白:请不要将代码的空格加倍。虽然需要校对,但这里的代码很难阅读。
sqrt(5*temp+297)/(247)
->
sqrt(5*temp+297)/(247))
很多人可能会认为没有给出正确的结果是不清楚的,因此,如果你能在问题主体中描述一下你想实现什么,以及你现在得到了什么,那就更好了。
 temp();
 speed = calc_speed(tempy);
tempy = temp();
speed = calc_speed(tempy);