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