-1

I am writing a program to where user inputs some values and based on those values program goes on with the following algorithm:

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.

I am having problem with the code:

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

}

I want it initially to take the cd=0.34 and then calculate a new cd from Vt and Re. IF the new cd and cd are same then, for i times and d=d+2-> calculate D-> and using D calcualte H,L, L/D for t times. ELSE continue calculating with cd=new_cd.

Its always showing Segmentation Fault. I am guessing the problem is with the values of 'cd' and 'new_cd' OR maybe I am calling the calculate_new_cd function incorrectly in the main function

Where am I doing wrong? Suggestions please!

user694733
  • 15,208
  • 2
  • 42
  • 68
Saumay Paul
  • 405
  • 1
  • 7
  • 21

2 Answers2

0

You are probably experiencing a stack overflow due to infinite recursion. Please refactor function Calculate_new_cd as follows:

  • Get rid of the recursive function call; it is completely useless and it is the main cause for your problem.
  • Make up your mind: use global variables (ugh) or pass cd as a parameter (notice how temp is being overwritten by the very first statement).
Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45
0

This

  if (rounded_cd1 == temp)

is critical.

Comparing floating point numbers for equality is a bad idea, as floating point numbers are not 100% accurate.

  1. In particular float provides a precision of 7 only. Better go for double.
  2. Instead of doing f == g better do (epsilon > fabs(f - g)) with epsilon reflection the accuracy you need and the code is able to achieve (see 1. above), like const double epsilon = 0.00001;
alk
  • 69,737
  • 10
  • 105
  • 255