![]() |
| | #1 |
| Registered User Join Date: Jan 2005
Posts: 28
| Help! Right Math, Wrong Output 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, 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);
}
|
| verd is offline | |
| | #2 | |
| Registered User Join Date: Mar 2004
Posts: 536
| Quote:
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 Last edited by Dave Evans; 03-13-2005 at 03:34 PM. | |
| Dave Evans is offline | |
| | #3 | ||
| Registered User Join Date: Jan 2005
Posts: 28
| Quote:
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:
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) | ||
| verd is offline | |
| | #4 |
| Registered User Join Date: Jan 2005
Posts: 28
| It obviously seems like I'm doing something wrong with my loop... Code: for(Vol = initialVol; Vol <= finalVol; Vol += Volincrement) | 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? |
| verd is offline | |
| | #5 | |
| Registered User Join Date: Mar 2004
Posts: 536
| Quote:
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() */ 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 | |
| Dave Evans is offline | |
| | #6 |
| Registered User Join Date: Jan 2005
Posts: 28
| 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? |
| verd is offline | |
| | #7 |
| Registered User Join Date: Mar 2005
Posts: 36
| 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 ). |
| Karthur is offline | |
| | #8 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,643
| 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);
}
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #9 |
| Registered User Join Date: Jan 2005
Posts: 28
| 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! |
| verd is offline | |
| | #10 |
| Registered User Join Date: Jan 2005
Posts: 28
| 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);
}
|
| verd is offline | |
| | #11 | |
| Registered User Join Date: Mar 2004
Posts: 536
| Quote:
Code: scanf("%lf", &exit);
Regards, Dave Last edited by Dave Evans; 03-15-2005 at 09:38 AM. | |
| Dave Evans is offline | |
| | #12 | |
| Registered User Join Date: Mar 2004
Posts: 536
| Quote:
Or at least proofread your program to make sure the constants are correct. Regards, Dave | |
| Dave Evans is offline | |
| | #13 |
| Registered User Join Date: Jan 2005
Posts: 28
| 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!! |
| verd is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Something Wrong with my function in Linux! | Matus | C Programming | 5 | 04-30-2008 10:00 PM |
| 2d game | JordanCason | Game Programming | 5 | 12-08-2007 10:08 PM |
| Why does it output 0-5 and not the selected partNumbers? What is wrong? | Npratt23 | C++ Programming | 3 | 09-14-2005 09:20 PM |
| a lil help.. prog's done but wrong output | gamer4life687 | C++ Programming | 5 | 09-13-2005 02:32 AM |
| Leap year program prints wrong output | Guti14 | C Programming | 8 | 08-24-2004 11:56 AM |