gpt4 book ai didi

c - 程序不会使用正确的数组值

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:34 26 4
gpt4 key购买 nike

此程序的目的是从文件中读取数据,然后使用该数据使用六个不同温度下的 a 和 b 值计算气体压力。目前在打印时,我认为该程序只使用五个中的第一个 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");
}

最佳答案

您的代码中有一些错误。

当您将 tempF 和压力数组传递给 computePressure 时,我认为您不想传递第 gth 元素的地址。看起来您的代码需要数组开头的地址。它应该看起来像这样:

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

在传递第 g 个压力元素的地址的 printGasInfo 调用中,您遇到了同样的问题。它应该更改为与此类似的内容:

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

我看到的另一个问题是计算 computePressure 中的 tempK。比率 5/9 将使用整数计算并将返回 0,因此 tempK 将始终得到 273 的值,这不是您想要的。您需要将其更改为使用 float/double 常量。此外,正确转换为开尔文应该使用 273.15 的偏移量。 tempK 计算应如下所示:

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

我通过这些更改获得的输出是:

                            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

关于c - 程序不会使用正确的数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19818571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com