简体   繁体   English

空气中声速的 C 程序没有给出正确的结果

[英]C program for speed of sound in air not giving correct results

The program is:该计划是:

#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);
}

The program instructions in the link below .下面链接中的程序说明。 enter image description here在此处输入图片说明

Your mistake is that the denominator should be inside the square root according to the instructions.你的错误是按照说明分母应该在平方根内。

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

should be应该

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

EDIT: Also as mentioned by the other answer your variable tempy was never initialized, you need to assign it with the return value of temp() .编辑:也如其他答案所述,您的变量tempy从未初始化过,您需要为它分配temp()的返回值。

tempy = temp();

You get the user input here您在此处获得用户输入

float temp(void)
{
    float t;

    scanf("%f", &t);

    return(t);
}

But you don't do anything with the returned value但是你对返回的值没有做任何事情

 temp();
 speed = calc_speed(tempy);

So you call Cacl_speed with an uninitialized value Try所以你用一个未初始化的值调用 Cacl_speed Try

tempy = temp();
speed = calc_speed(tempy);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM