Help! Right Math, Wrong Output

• 03-13-2005
verd
Help! Right Math, Wrong Output
Hey everybody,

I'm having trouble again- I'm a beginning programming student in a C For Engineers class.... And right now we're working on for loops... But for some reason, the program isn't doing what it's supposed to do.

The problem statement is overly complicated, but basically, the program is supposed to take a couple of inputs, (initial volume, final volume, volume increment, temperature, and moles of CO2), apply them to a formula, and find the pressure of a gas.

I'll include the problem as it was given to me:

The pressure of gas changes as the volume and temperature of the gas vary. Write a program that uses the Van der Waals equation of state for a gas,

[Click here to see an image of the formula]

to display in tabular form the relationship between the pressure and the volume of n moles of carbon dioxide at a constant absolute temperature (T). P is the pressure in atmospheres and V is the volume in liters. The Van der Waals constants for carbon dioxide are a = 3.592 L2 * atm/mol2 and b = 0.0427 L/mol. Use 0.08206 L * atm/mol * K for the gas constant R. Inputs to the program include n, the Kelvin temperature, the initial and final volumes in milliliters, and the volume increment between lines of the table. Your program will display a table that varies the volume of the gas from the initial to the final volume in steps prescribed by the volume increment. Here is a sample run:

Please enter at the prompts the number of moles of carbon dioxide, the absolute temperature, the initial volume in milliliters, the final volume, and the increment volume between lines of the table.

Quantity of carbon dioxide (moles) > 0.02
Temperature (kelvin) > 300
Initial volume (milliliters) > 400
Final volume (milliliters) > 600
Volume increment (milliliters) > 50

0.0200 moles of carbon dioxide at 300 kelvin

Volume (ml) Pressure (atm)

400 1.2246
450 1.0891
500 0.9807
550 0.8918
600 0.8178

I've manipulated everything correctly algebraically, so the math is correct... I just can't get it to display a correct output.

Here is what I've done:
(Please, help me find my errors... I have no idea what I'm doing wrong)

Code:

```#include <stdio.h> #define a 3.592 #define b .0427 #define R .03206 #define mL_L .001 #define L_mL 1000.0 int main() {       double initialVol, finalVol, Vol, Volincrement, pressure, nmoles;       int temp, exit;             printf("Please enter at the prompts the number of moles of\ncarbon dioxide, the absolute temperature, the initial\nvolume  in milliliters, teh final volume, and the increment volume between  lines of the table.\n\n");       printf("Quantity of carbon dioxide (moles)> ");       scanf("%lf", &nmoles);       printf("\nTemperature (kelvin)> ");       scanf("%d", &temp);       printf("\nInitial volume (milliliters)> ");       scanf("%lf", &initialVol);       printf("\nFinal Volume (milliliters)> ");       scanf("%lf", &finalVol);       printf("\nVolume increment (milliliters)> ");       scanf("%lf", &Volincrement);             printf("\n\n%.4f moles of carbon dioxide at %d kelvin", nmoles, temp);             printf("Volume  (mL)          Pressure (atm)");                         for(Vol = initialVol; Vol <= finalVol; Vol += Volincrement)       {               printf("  %.2f", Vol);                             Vol *= mL_L;               pressure = (nmoles * R * temp)/(Vol - b * nmoles) - (a * nmoles * nmoles)/(Vol * Vol);                                                       printf("          %.4f\n", pressure);                             }                             printf("Exit program? (y/n)> ");               scanf("lf", exit);                                           return (0);             }```
• 03-13-2005
Dave Evans
Quote:

Originally Posted by verd
Hey everybody,

I'm having trouble again-

For starters, use something else for your loop counter:

Code:

```  for(Vol = initialVol; Vol <= finalVol; Vol += Volincrement)       {               printf("  %.2f", Vol);                             Vol *= mL_L;  /* Loop counter is Vol */```

Then put in some debug statements to print out all of your constants and user input values before you enter the loop to make sure the formula is working on what you think it should be.

Regards,

Dave
• 03-13-2005
verd
Quote:

Originally Posted by Dave Evans
For starters, use something else for your loop counter:

Alright, I'll try that... However, I don't quite understand what that means... Are you telling me not to use the variable Vol?

I don't understand- sorry, I'm quite a beginner...

Quote:

Originally Posted by Dave Evans
Then put in some debug statements to print out all of your constants and user input values before you enter the loop to make sure the formula is working on what you think it should be.

I don't know what a debug statement is either- I've tried pulling all of the print statements out of the loop, and on my own, I substituted the variables that I thought would end up in the formula into the formula, and I got the right answer.

I'm sorry, I don't understand... (Really, I'm sorry, this is all really new to me)
• 03-13-2005
verd
It obviously seems like I'm doing something wrong with my loop...

Code:

`for(Vol = initialVol; Vol <= finalVol; Vol += Volincrement)`
This is what the above code, as far as I know, translates into:

| When Vol is equivalent to whatever the initial volume is | And Vol is less than or equal to whatever the final volume is |

The statement in the brackets will loop

| At the end of the loop, Vol will be raised whatever Volincrement is equal to |

Am I wrong?
• 03-13-2005
Dave Evans
Quote:

Originally Posted by verd
It obviously seems like I'm doing something wrong with my loop...

Code:

`for(Vol = initialVol; Vol <= finalVol; Vol += Volincrement)`
This is what the above code, as far as I know, translates into:

| When Vol is equivalent to whatever the initial volume is | And Vol is less than or equal to whatever the final volume is |

The statement in the brackets will loop

| At the end of the loop, Vol will be raised whatever Volincrement is equal to |

Am I wrong?

But inside your loop you change the value of Vol to use in your formula! This royally screws up your loop.

One way to fix it is to change the loop variable name to something else, maybe like this:

Code:

`      double loopVol; /* near the beginning of main() */`
Now you would change your loop to something like this
Code:

```       for(loopVol = initialVol; loopVol <= finalVol; loopVol += Volincrement)       {         printf("  %.2f", loopVol);         Vol = loopVol * mL_L;         pressure = (nmoles * R * temp)/(Vol - b * nmoles) -                   (a * nmoles * nmoles)/(Vol * Vol);                                     printf("          %.4f\n", pressure);                 }```

Regards,

Dave
• 03-13-2005
verd
Wow. That fixed everything

Thank you...

Now what exactly was happening, I don't quite understand.

The user inputted milliliters for Vol, and in order to make the calculation, Vol needed to be in liters... So the mL_L was just a converstion. How did loopVol and Vol fix the problem?
• 03-13-2005
Karthur
You started into the loop ok, (should have printed the first line), but while you were there you multiplied Vol by 1000, then left the inside of the loop, incremented by your 50 and tested to see whether to do it again - an obvious NO! You can't mess with your loop controls unless you know what you are doing and want the strange effect ( maybe an early exit, or a start over, or a newly calculated limit value, or you just like obfuscated code ).
• 03-13-2005
quzah
Also, your scanf call at the end is wrong. You need the address-of operator before non-pointer variables for scanf to work correctly. Not to mention the fact that it doesn't actually control the program's outcome in any way.

At any rate, reread Dave's post. You were trying to control the loop with a value check against something that you constantly change every time through the loop, plus you increment it each pass also:
Code:

```for(Vol = initialVol; Vol <= finalVol; Vol += Volincrement )       {               printf("  %.2f", Vol);                             Vol *= mL_L;               pressure = (nmoles * R * temp)/(Vol - b * nmoles) - (a * nmoles * nmoles)/(Vol * Vol);                                                       printf("          %.4f\n", pressure);                             }```
Quzah.
• 03-14-2005
verd
Alright, cool... So I fixed the scanf, and ran the code through a windows compiler (I was running it through the gcc compiler on a mac). The program runs and quits.... I've had this problem before, but usually the "End program" fixes it. I can't see the output, it closes too quickly.

Does anyone know how to avoid this problem? Or is the program crashing because of an error that I made?
(I'm using Dev-C++)

Thanks again for all of the help!
• 03-14-2005
verd
I forgot to include my new code, sorry:

Code:

```#include <stdio.h> #define a 3.592 #define b .0427 #define R .03206 #define mL_L .001 int main() {       double initialVol, finalVol, Vol, Volincrement, pressure, nmoles, loopVol;       int temp, exit;             printf("Please enter at the prompts the number of moles  of\ncarbon dioxide, the absolute temperature, the initial\nvolume in  milliliters, teh final volume, and the increment volume between lines  of the table.\n\n");       printf("\nQuantity of carbon dioxide (moles)> ");       scanf("%lf", &nmoles);       printf("Temperature (kelvin)> ");       scanf("%d", &temp);       printf("Initial volume (milliliters)> ");       scanf("%lf", &initialVol);       printf("Final Volume (milliliters)> ");       scanf("%lf", &finalVol);       printf("Volume increment (milliliters)> ");       scanf("%lf", &Volincrement);             printf("\n\n%.4f moles of carbon dioxide at %d kelvin", nmoles, temp);             printf("Volume  (mL)          Pressure (atm)");                               for(loopVol = initialVol; loopVol <= finalVol; loopVol += Volincrement)               {                   printf("  %.2f", loopVol);                   Vol = loopVol * mL_L;                   pressure = (nmoles * R * temp)/(Vol - b * nmoles) - (a * nmoles * nmoles)/(Vol * Vol);                                               printf("          %.4f\n", pressure);                       }                       printf("Exit program? (y/n)> ");         scanf("lf", &exit);                                     return (0);             }```
• 03-15-2005
Dave Evans
Quote:

Originally Posted by verd
I forgot to include my new code, sorry:

Code:

`        scanf("lf", &exit);`

Change the above to

Code:

`      scanf("%lf", &exit);`
(Did you notice that the answers from your program do not agree with the sample run that you showed in your first post?)

Regards,

Dave
• 03-15-2005
Dave Evans
Quote:

Originally Posted by verd

I don't know what a debug statement is either- I've tried pulling all of the print statements out of the loop, and on my own, I substituted the variables that I thought would end up in the formula into the formula, and I got the right answer.

I'm sorry, I don't understand... (Really, I'm sorry, this is all really new to me)

Put printf statements at strategic places to make sure all constants and variables for the formula have the right values.

Or at least proofread your program to make sure the constants are correct.

Regards,

Dave
• 03-15-2005
verd
Yeah... So... I had the correct answer on the mac, and then hopped onto the pc. Something happened, for some reason, the c file got messed up in transit, and I pretty much had to rewrite it. What was messing up that output was my misreading a handwritten "8" as a "3" in one of the constants. I fixed that, and now the program runs just fine.

Haha, thanks though, for all of the help!
It's much appreciated!!! Really... it is. To me, C is like turning around for a second, only to find that when you turn back around, you're in some foreign country with weird customs and a language you've never heard of before...

I'm getting the hang of it, but there are plenty more stupid errors I'm going to have to make before I actually have a decent understanding of the language. (i.e. the "%" before the "lf" in the exit prompt...)

Thanks again for all of your help!!