簡體   English   中英

空氣中聲速的 C 程序沒有給出正確的結果

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

該計划是:

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

下面鏈接中的程序說明。 在此處輸入圖片說明

你的錯誤是按照說明分母應該在平方根內。

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

應該

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

編輯:也如其他答案所述,您的變量tempy從未初始化過,您需要為它分配temp()的返回值。

tempy = temp();

您在此處獲得用戶輸入

float temp(void)
{
    float t;

    scanf("%f", &t);

    return(t);
}

但是你對返回的值沒有做任何事情

 temp();
 speed = calc_speed(tempy);

所以你用一個未初始化的值調用 Cacl_speed Try

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM