【发布时间】:2018-10-22 20:42:20
【问题描述】:
我正在编写一个程序,用户输入一些值并基于这些值程序继续使用以下算法:
1. Set INITIAL cd=0.34
2. CALCULATE Vt using cd=0.34
3. CALCULATE Re using Vt.
4. CALCULATE new_cd using Re and Vt
5. USING new_cd, REPEAT 2 TO 4 until new_cd=cd
6. IF new_cd=cd THEN:
FOR(i=0;i<=5;i++): //Proceeds for 5 times with value of D
CALCULATE D
FOR(t=1;t<=4;t++):
Calculate H;
Calculate L;
CALCULATE L/D;
7. WITH D=D+2 REPEAT Step 6.
我的代码有问题:
#include<stdio.h>
#include<math.h>
int i,t;
float p1,pg,dm,u,Vt,Re,cd1,d2,y,x,P,Z,Qg,T,Q,h,l,ratio,rounded_cd1,rounded_cd,round_cd1, d, temp;
float cd= 0.34;
float Calculate_new_cd(float temp)
{
temp=cd;
x = ((p1-pg)/pg) * (dm/temp);
Vt = 0.0119*sqrt(x);
Re=0.0049*((pg*dm*Vt)/u);
cd1= ((24/Re) + (3/sqrt(Re)) + 0.34);
rounded_cd1=floor(cd1 * 1000)/1000;
round_cd1=floor(cd1 * 100)/100;
if(rounded_cd1==temp)
{
printf("Cd= %0.3f",rounded_cd1);
printf("Temp= %0.3f",temp);
y = ((pg/(p1-pg))*(rounded_cd1/dm));
d2= 5040*((T*Z*Qg)/P)*sqrt(y);
d = sqrt(d2);
for(i=0;i<5; i++)
{
for(t=1;t<=4;t++)
{
//calculate H
h= (t*Q)/(0.12*d*d);
printf("\n");
printf("For time T: %d \n",t);
printf("Value of D = %0.3f \n",d);
printf("Value of H: %0.3f \n",h);
//calculate L
l=(h+76)/12;
printf("Value of L: %0.3f \n",l);
//calculate L*12/D
ratio= (l*12)/d;
if(ratio>3.0 && ratio < 4.0)
{
printf("Acceptable L/D: %0.2f\n\n\n", ratio);
}
else
{
printf("Unacceptable L/D: %0.2f\n\n\n",ratio);
}
}
d+=2;
}
}
else
{
Calculate_new_cd(round_cd1);
}
}
void main(){
printf("Enter the P1: ");
scanf("%f",&p1);
printf("Enter the Pg: ");
scanf("%f",&pg);
printf("Enter the Dm: ");
scanf("%f",&dm);
printf("Enter the u: ");
scanf("%f",&u);
printf("Enter the T: ");
scanf("%f",&T);
printf("Enter the P: ");
scanf("%f",&P);
printf("Enter the Z: ");
scanf("%f",&Z);
printf("Enter the Q: ");
scanf("%f",&Q);
printf("Enter the Qg: ");
scanf("%f",&Qg);
Calculate_new_cd(cd);
}
我希望它最初采用cd=0.34,然后计算new cd from Vt and Re。 IF 与 new cd and cd 相同,对于 i 次和 d=d+2-> 计算 D-> 并使用 D 计算 H,L, L/D 对于 t 次。 ELSE 继续计算 cd=new_cd。
它总是显示分段错误。我猜问题出在“cd”和“new_cd”的值上,或者我在main 函数中错误地调用了calculate_new_cd 函数
我哪里做错了? 请提出建议!
【问题讨论】:
-
代码不完整。需要MCVE
-
添加了完整的代码!如果它有帮助@MFisherKDX
-
您需要将代码重构为单独的函数。目前是一团糟
-
您可能想阅读以下内容:How to debug small programs
-
请不要进行使当前答案无效的编辑。