【问题标题】:Looping and replacing the value of a variable in a loop statement in C?在C中的循环语句中循环和替换变量的值?
【发布时间】: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 ReIFnew 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
  • 请不要进行使当前答案无效的编辑。

标签: c function loops


【解决方案1】:

由于无限递归,您可能会遇到堆栈溢出。 请重构函数Calculate_new_cd如下:

  • 摆脱递归函数调用;它完全没用,它是您问题的主要原因。
  • 下定决心:使用全局变量(呃)或将cd 作为参数传递(注意temp 是如何被第一条语句覆盖的)​​。

【讨论】:

    【解决方案2】:

    这个

      if (rounded_cd1 == temp)
    

    很关键。

    比较浮点数是否相等是个坏主意,如floating point numbers are not 100% accurate

    1. 特别是float 仅提供7 的精度。最好选择double
    2. 与其做f == g 不如做(epsilon &gt; fabs(f - g))epsilon 反射你需要的准确度代码能够达到(见上面的1.),比如const double epsilon = 0.00001;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-22
      • 2012-08-28
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 2018-05-12
      相关资源
      最近更新 更多