0

このプログラムの目的は、ファイルからデータを読み取り、そのデータを使用して、6 つの異なる温度での a 値と b 値を使用してガスの圧力を計算することです。現在、プログラムは 5 つのうち最初の a 値と b 値のみを使用していると思われます。したがって、すべての a 値と b 値に対して同じ情報を出力しています。どんな助けでも大歓迎です!ファイルのアップロード方法がわからなかったので、データは次のとおりです。

データ:

0.0341   0.0237
0.244    0.0266
1.36     0.0318
5.46     0.0305
20.4     0.1383

コード:

 #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");
}
4

2 に答える 2

1

よくあるエラーがあります:

        double tempK = (5/9) * (tempF[t]-32) + 273;

整数除算なので、5/9 は常に0です。を使用すると、結果は次のようになります5.0/9.0

                Pressure (atm) using Waals' Ideal Gas Law

L2atm/mol2  L/mol           0F          20F          40F          60F          80F         100F
  0.0341       0.0237      44.4162      46.3557      48.2953      50.2349      52.1745      54.1141
  0.2440       0.0266      45.8010      47.7524      49.7039      51.6554      53.6069      43.8495
  1.3600       0.0318      43.8296      45.8028      47.7759      49.7491      39.8833      39.8833
  5.4600       0.0305      29.2609      31.2286      33.1963      23.3578      23.3578      23.3602
 20.4000       0.1383     -12.7150     -10.1609     -22.9315     -22.9315     -22.9285     -22.9281

結果は異なりますが、最後の行で実際に負圧が見られるべきかどうかはわかりません.


私が見た 2 番目の問題は、特定pressuretempF配列項目へのポインタを に渡すことですが、配列にポインタを渡すことを意図してcomputePressureいたことがわかるかもしれません。( and )には最大 MAXNUMBERGASES 個のアイテムを含める必要がなく、 (and ) を使用computePressureするため、範囲外の配列アクセスを取得することも可能です。 MAXNUMBERGASESまで。tempFpressure&tempF[g]&pressure[g]g

つまり、これらの行を次のように変更します

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

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

. 結果:

                Pressure (atm) using Waals' Ideal Gas Law

L2atm/mol2  L/mol           0F          20F          40F          60F          80F         100F
  0.0341       0.0237      44.4162      46.3557      48.2953      50.2349      52.1745      54.1141
  0.2440       0.0266      43.8495      45.8010      47.7524      49.7039      51.6554      53.6069
  1.3600       0.0318      39.8833      41.8565      43.8296      45.8028      47.7759      49.7491
  5.4600       0.0305      23.3578      25.3255      27.2932      29.2609      31.2286      33.1963
 20.4000       0.1383     -22.9315     -20.3774     -17.8233     -15.2691     -12.7150     -10.1609

注: valgrind エラーはなくなりました。

于 2013-11-06T17:50:08.280 に答える