Search code examples
carrayscomputation

Program won't use correct array values


The purpose of this program is to read data in from a file and then use that data to compute the pressure of the gases using the a and b values at six different temperatures. Currently when printing out, the program I believe uses only the first a and b values out of five, and therefore is printing out the same information for every a and b value. Any help would be appreciated! I could't figure out how to upload the file so here is the data:

Data:

0.0341   0.0237
0.244    0.0266
1.36     0.0318
5.46     0.0305
20.4     0.1383

Code:

 #include <stdio.h>
 #include <math.h>
 #define R 0.08314472
 #define MAXNUMBERGASES 10
 #define NUMBERTEMPS 6
 #define FILENAME "gasValues.txt"

//prototype functions
int getGasValues (double a[], double b []);
void printHeaders (double tempF[]);
void computePressure (double tempF[], double pressure[], double moles,
                  double volume, double a, double b);
void printGasInfo (double a, double b, double pressure[]);





int main()
{
int moles = 2; //mol (n)
int volume = 1; //Liters (V)
double a[MAXNUMBERGASES]; //L^2bar/mol^2
double b[MAXNUMBERGASES]; //L/mol
int numberGases, g;
double tempF[] = {0, 20, 40, 60, 80, 100};
double pressure[NUMBERTEMPS];




numberGases = getGasValues (a, b);

if (numberGases < 1) printf ("Error. No data read from file\n");

else
{
  printHeaders(tempF);


  for (g = 0; g < numberGases; g++)
  {
     computePressure (&tempF[g], &pressure[g], moles, volume, a[g], b[g]);

     printGasInfo (a[g], b[g], &pressure[g]);

  }
}


return 0;
}



int getGasValues (double a[], double b[])
{
FILE *gasFile; //file pointer
int g = 0; //counter for number of gases

gasFile = fopen("gasValues.txt", "r");

if (gasFile == NULL){
  printf("File could not be opened. Program terminated.\n");
  return 0; //end program if file cannot be opened or found
}
else

  while ((fscanf (gasFile, "%lf" "%lf", &a[g], &b[g])) != EOF) g++;

return g;
}

void printHeaders (double tempF[NUMBERTEMPS])
{
printf ("\t\t\t    Pressure (atm) using Waals' Ideal Gas Law\n\n");
printf ("L2atm/mol2 \tL/mol");

int t = 0;
for (t = 0; t < NUMBERTEMPS; t++)
{
  printf ("  %10.0lfF"  ,tempF[t]);
}
printf ("\n");
}


void computePressure (double tempF[NUMBERTEMPS], double pressure[NUMBERTEMPS], double moles, double volume, double a, double b)
{
int t=0;

for (t = 0; t < 6; t++) 
{  
  double tempK = (5/9) * (tempF[t]-32) + 273;
  double part1 = (moles * R * tempK);
  double part2 = volume - (moles*b);
  double part3 = (a*(pow(moles,2)))/(volume*volume);
  double part4 = part1/part2;
  pressure[t] = part4 - part3;
}   
}

void printGasInfo (double a, double b, double pressure[NUMBERTEMPS])
{
int p = 0;


printf("%8.4lf"     "%13.4lf", a, b);
for (p = 0; p < NUMBERTEMPS; p++)
{
  printf ("     %8.4lf", pressure[p]);
}
printf ("\n");
}

Solution

  • You have a few errors in your code.

    When you are passing the tempF and pressure array to computePressure I don't think you wanted to pass the address of the gth element. It looks like your code is expecting the address of the start of the array. It should look something like this:

    computePressure (tempF, pressure, moles, volume, a[g], b[g]);
    

    You have the same problem in the call to printGasInfo where you have passed the address of the gth element of pressure. It should be change to something similar to this:

    printGasInfo (a[g], b[g], pressure);
    

    The other problem I see is in the calculation of tempK in computePressure. The ratio 5/9 will be calculated using integers and will return 0 so tempK will always result in a value of 273 which is not what you intended. You need to change that to use float/double constants. Also the correct conversion to Kelvin should use an offset of 273.15. The tempK calculation should look like this:

    double tempK = (5.0/9.0) * (tempF[t]-32) + 273.15;
    

    The output I get with these changes are:

                                Pressure (atm) using Waals' Ideal Gas Law
    
    L2atm/mol2      L/mol           0F          20F          40F          60F          80F         100F
      0.0341       0.0237      44.4423      46.3819      48.3215      50.2611      52.2007      54.1403
      0.2440       0.0266      43.8758      45.8273      47.7788      49.7303      51.6817      53.6332
      1.3600       0.0318      39.9100      41.8831      43.8563      45.8294      47.8026      49.7757
      5.4600       0.0305      23.3844      25.3521      27.3198      29.2875      31.2551      33.2228
     20.4000       0.1383     -22.8971     -20.3429     -17.7888     -15.2347     -12.6805     -10.1264