Thread: Help with programming assignment

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    Cool Help with programming assignment

    Hello!

    I am fairly new to programming. I am trying to finish this assignment from Hanley's C Program Design for Engineers. It is due on Monday, and I am trying to get it finished up this evening. I think there is either something wrong with my for loop or with the equation I am using.
    When I compile and run my program, I am able to get it to print in a list the volume, but as for the pressure, it is printing out "nan".

    What do I need to tweak?
    Code:
    /****************************************************************************************
    * Name: main
    * Returns: int; 0 for success
    *
    * Purpose:  to compute Van der Waals equation of state for a gas. 
    * Requires:  stdio library function, printf, scanf, 
    * Dependencies:  none
    ****************************************************************************************/
    
    
    /*includes*/
    #include <stdio.h>
    #include <math.h>
    
    /*define contstants for Van der Waals' equation*/
    #define R 0.08206
    #define A 3.592
    #define B 0.0427
    /*******************************************************************
    *  This is the main function
    ********************************************************************/
    void instruct(void);
    
    int main(void)
    {
    
    
      double n_moles; /*the number of moles the user inputs*/
      double int_volume; /*the initial volume as input by the user*/
      double kelvin_temp; /*the temperature in Kelvins as input by user*/
      double increment_vol; /*the increment volume given by the user*/
      double final_vol; /*the final volume given by the user*/
      double pressure; /*the pressure*/
      double volume; /*volume*/
      instruct ();
    
      
    
    
      printf("\n\n\nPlease input the quanity of carbon dioxide (moles)>>");
      scanf( "%lf", &n_moles);
    
      printf("\n\nTemperature (Kelvins)>>");
      scanf( "%lf", &kelvin_temp);
    
      printf("\n\nInitial volume (milliliters)>>");
      scanf("%lf", &int_volume);
    
      printf("\n\nFinal volume (milliliters)>>");
      scanf("%lf", &final_vol);
    
      printf("\n\nVolume increment (milliliters)>>");
      scanf("%lf", &increment_vol);
    
    
      printf("Volume (ml)               Pressure (atm)");
    
    
      /*P =( nRT/(Vnb))-n^2a/V^2*/
      pressure = ( n_moles*R*kelvin_temp/(volume*n_moles*B))-n_moles*n_moles*A/volume*volume ;
    
      {
        
        for (volume =int_volume;
    	 volume  <= final_vol;
    	 volume +=increment_vol)
          
    
    
    
      printf("\n\n%5.4lf         %5.4lf\n",volume, pressure);
    
        }
     return 0;
    
    
    } // end function main () ************************************** 
    
    
    
    /*******************************************************************
    * This is the instruct function--it gives instructions to the user
    *******************************************************************/
    
    
    void instruct (void)
    {
    
    	printf("\n\n\nPlease enter at the prompts ");
    	printf("the number of moles of carbon dioxide ");
    	printf("the absolute\n temperature, the initial volume in millimeters ");
    	printf("the final volume, \nand the increment volume between lines of the table\n\n");
    }
    Thanks for your help!
    Sarah

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to put the computation inside the for loop. (You need to re-compute pressure for each volume, right?)

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
      {
        
        for (volume =int_volume;
    	 volume  <= final_vol;
    	 volume +=increment_vol)
          
    
    
    
      printf("\n\n&#37;5.4lf         %5.4lf\n",volume, pressure);
    
        }
    While that works, I'm pretty sure you meant it more like this:
    Code:
        for (volume =int_volume;
    	 volume  <= final_vol;
    	 volume +=increment_vol)
          
      {
    
      printf("\n\n%5.4lf         %5.4lf\n",volume, pressure);
    
        }
    If you indent the code a little differently, you'll see why. Your version:
    Code:
    {
        for (volume =int_volume; volume  <= final_vol; volume +=increment_vol)
            printf("\n\n%5.4lf         %5.4lf\n",volume, pressure);
    }
    My version:
    Code:
    for (volume =int_volume; volume  <= final_vol; volume +=increment_vol) {
        printf("\n\n%5.4lf         %5.4lf\n",volume, pressure);
    }
    My version also allows you to put more than one statement in the for loop, as you need to. (See above post.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    Thumbs up Thank you!

    Thanks for your comments! My code works now, everything is displayed correctly. Whoo!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM