Thread: "for" loop problems....

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    "for" loop problems....

    When I try to compile the following code in borland it tells me that there is incorrect syntax with the for () loop and that the statement is missing. I don't understand why the syntax is wrong, i have put an initializing statement, then a condition, then the increment increaser... Can anyone suggest what I may have done wrong... Any help greatly appreciated.

    note: the program is designed to calculate the time of filling of a tank with varying radi with height by means of trapezoidal integration.

    Code:
     
    
    #include<stdio.h>
    #include<math.h>
    main()
    {
          /*declaring variables*/
          float i,h,r,p;
          float hlevel,llevel;
          float D1, D2, D3;
          float Qo,Qi,dt,w;
          float time;
    
          /*declaring functions*/
          /*float time( float r);*/
    
    
          /*defining variables*/
          #define n 500;
          #define pi 3.1415926535897932384626433832795;
          #define g 9.81;
          
          D1= 6.0;
          D2= 3.7;
          D3= 2.7;
          hlevel= 0.5;
          llevel= 5.0;
          w=(hlevel-llevel)/n;
          
          
          /*getting user input for density*/
          printf("Welcome to the surge tank simulator program\n");
          printf("Please enter the liquid density (kg/m^3):\n");
          printf("p=");
          scanf("%f", &p);
    
          /*trapezoidal integration using for loop*/
    
          for(i=1;i<n;i++)
          {
               h= 0.5+(i*w);
               Qo= 0.000004*(sqrt((p*g*h)));
               Qi= 300;
    
    
               if(h <= D3 && h >= 0.5)
               {    
                       r= 1.5;
               }
    
               if(h <= D2 && > D3)
               {
                       r= (0.5)*(5.7-h);
               }
    
               if(h <= 5.0 && > D2)
               {
                       r= (3.5/3.22)*(h - 2.78);
               }
    
               dt=  ((pi*(r*r))/(Qi-Qo))*w ;
               time= time + dt;
    
           } /*end for... i.e. i does not equal less than or equal to n*/
    
    
               printf( "The high level alarm sounds after %f, hours\n", time);
    } /*end main*/

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    you don't need semicolans after #defines

    Code:
    #define n 500
    #define pi 3.1415926535897932384626433832795
    #define g 9.81
    And I am not sure if its a good idea to have #defines inside your main() (or any other function for that matter). It should be declared outside functions and above main() in this case or in some other header (.h) file which you will have to include using #include directives.

    The function declaration should also be (not necessary though) moved above main() globally.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    >>It should be declared outside functions and above main()

    Most people do but there is no reason why you should or shouldn't.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    Sorry i meant to delete that function hence why it has comment tags... I just removed the ;'s from the defines...

    I think I have it worked out now though... I just noticed that the if ()'s were not written properly...

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    India
    Posts
    14

    for loop proble

    I think there is nothing wrong with for loop. You should use define after header files. There are error in if condition.
    Instead of using
    Code:
    if(h <= D2 && > D3)
    use
    Code:
    if(h <= D2 && h> D3)
    and
    Code:
     if(h <= 5.0 && > D2)
    any way here the complete programme without any error.
    may fullfill your desire.
    Code:
    #include<stdio.h>
    #include<math.h>
    /*defining variables*/
    #define n 500
    #define pi 3.1415926535897932384626433832795
    #define g 9.81
    
    
    int main(void)
    {
          /*declaring variables*/
          int i,h,r,p;
          float hlevel,llevel;
          float D1, D2, D3;
          float Qo,Qi,dt,w;
          float time;
    
          /*declaring functions*/
          /*float time( float r);*/
    
          D1= 6.0;
          D2= 3.7;
          D3= 2.7;
          hlevel= 0.5;
          llevel= 5.0;
          w=(hlevel-llevel)/n;
    
          /*getting user input for density*/
          printf("Welcome to the surge tank simulator program\n");
          printf("Please enter the liquid density (kg/m^3):");
          scanf("%f", &p);
    
          /*trapezoidal integration using for loop*/
    
          for(i=1;i<n;i++)
          {
    	   h= 0.5+(i*w);
    	   Qo= 0.000004*(sqrt((p*g*h)));
    	   Qi= 300;
    
    
    	   if(h <= D3 && h >= 0.5)
    	   {
    		   r= 1.5;
    	   }
    
    	   if(h <= D2 && h> D3)
    	   {
    		   r= (0.5)*(5.7-h);
    	   }
    
    	   if(h <= 5.0 && h> D2)
    	   {
    		   r= (3.5/3.22)*(h - 2.78);
    	   }
    
    	   dt=  ((pi*(r*r))/(Qi-Qo))*w ;
    	   time= time + dt;
    
           } /*end for... i.e. i does not equal less than or equal to n*/
    
    
    	   printf( "The high level alarm sounds after %f, hours\n", time);
    return 0;
    } /*end main*/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  3. "for" loop or "while" loop?
    By s_ny33 in forum C++ Programming
    Replies: 11
    Last Post: 04-24-2005, 01:28 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. problems with greatest to least sorting
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2004, 10:12 PM