2012-01-25 7 views
-4

유량을 계산하는 프로그램을 만들고 있지만 프로그램이 실행될 때마다 변수에 입력 할 때마다 틀린 대답을 반환합니다. 나는이 웹 사이트 "http://www.flowmeterdirectory.com/flowcalculator.php"에서 유속을 계산하기위한 공식을 얻었는데, 나는 그것을 프로그램의 결과를 확인하기위한 참고 자료로 사용하고 있지만 결코 작동하지 않는다.C 프로그램이 잘못된 결과를 반환합니다.

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

int flowRateFormula(int,int); //Finally, finds out the flow rate 
int square(int);   

int square(int x) 
{ 
    x=pow(x,2); 

    return x; 
} 

int flowRateFormula(int pipeDiameter,int velocity) 
{ 
    int integer3=0.25*(PI*square(pipeDiameter)*velocity); 

    return integer3; 
} 

int main() 
{ 
    int flowRate; 
    int velocity; 
    int pipeDiameter; 


    printf("Enter velocity of liquid(in./s):"); 
    scanf("%d",&velocity); 
    printf("Velocity=%d in/s.\n",velocity); 

    printf("Enter pipe diameter(inches):"); 
    scanf("%d",&pipeDiameter); 
    printf("Pipe Diameter=%d inches.\n",pipeDiameter); 

    printf("Applying formula for Flow Rate.........\n"); 
    flowRate=flowRateFormula(pipeDiameter,velocity); 

    printf("Pipe Diameter=%d inches.\n",pipeDiameter); 

    printf("Velocity of liquid=%d in/s.\n",velocity); 

    printf("Thus, the flow rate=%d ft/s.\n",flowRate); 

    return 0; 
} 
+0

... 디버거에서 던지고 무슨 일이 일어나는가? – Bart

+1

아, 알겠습니다. 다른 질문에는 "숙제"표시가있어서 학교 생활을 알지 못하는 사이에 우리가 당신을 위해 일하게 해줍니다. 좋은 시도 - 작동하지 않았다. :) –

+1

왜, 왜 사람들은'pow (x, 2)'를 사용하여 숫자를 제곱합니까? –

답변

3

정수형 때문일 가능성이 큽니다. 입력에 따라 정수 캐스팅 중에 반올림되기 때문에 결과가 비슷하고 동일한 결과를 얻을 수 있습니다. 대신 함수에서 double을 사용하십시오.

1

어디서나 int을 사용하고 있기 때문에 많은 오류가 발생합니다. float 또는 double이 더 좋습니다. 이것만으로도 다른 값을 입력 했음에도 동일한 결과를 얻는 이유를 설명 할 수 있습니다. 예를 들어, int flow=0.25*x 모든 x의 flow에 대해 동일한 값을 줄 것이다 여기서 x> -4 X < 당신은 부동 소수점 연산을 수행하고 int로 결과를 반환하는 4

0

. 변수를 부동 소수점으로 변경하거나 복식이 필요했습니다.

1

정확한 결과를 얻으려면 float 또는 double 유형이 필요합니다.

이 시도하십시오

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

double PI = 3.14; 

double flowRateFormula(float, float); 

double flowRateFormula(float pipeDiameter, float velocity) 
{ 
    double p = pow(pipeDiameter, 2); 
    double result = 0.25 * PI * p * velocity; 
    return result; 
} 

int main() 
{ 
    double flowRate; 
    float velocity; 
    float pipeDiameter; 

    printf("Enter velocity of liquid(in./s):"); 
    scanf("%f",&velocity); 
    printf("Velocity=%f in/s.\n",velocity); 

    printf("Enter pipe diameter(inches):"); 
    scanf("%f",&pipeDiameter); 
    printf("Pipe Diameter=%f inches.\n",pipeDiameter); 

    printf("Applying formula for Flow Rate.........\n"); 
    flowRate=flowRateFormula(pipeDiameter,velocity); 

    printf("Pipe Diameter=%f inches.\n",pipeDiameter); 
    printf("Velocity of liquid=%f in/s.\n",velocity); 
    printf("Thus, the flow rate=%f ft/s.\n",flowRate); 

    return 0; 
} 

출력 :이 도움이

$ ./a.out 
Enter velocity of liquid(in./s):10 
Velocity=10.000000 in/s. 
Enter pipe diameter(inches):20 
Pipe Diameter=20.000000 inches. 
Applying formula for Flow Rate......... 
Pipe Diameter=20.000000 inches. 
Velocity of liquid=10.000000 in/s. 
Thus, the flow rate=3140.000000 ft/s. 
$ 
$ 
$ 
$ 
$ ./a.out 
Enter velocity of liquid(in./s):12.34 
Velocity=12.340000 in/s. 
Enter pipe diameter(inches):435.345 
Pipe Diameter=435.345001 inches. 
Applying formula for Flow Rate......... 
Pipe Diameter=435.345001 inches. 
Velocity of liquid=12.340000 in/s. 
Thus, the flow rate=1835912.361516 ft/s. 
$ 

희망!

관련 문제