gpt4 book ai didi

c - 运行程序返回 -1.#QNAN0

转载 作者:太空宇宙 更新时间:2023-11-04 03:32:16 25 4
gpt4 key购买 nike

在使用输入文件计算总和时,该文件包含两列数字以及第一列上方的整数导向器,当我创建第二列数字的总和时,我遇到了一个错误, conductivity, 它不是一个数字,并且返回-1.#QNAN0,我试过更改变量名和更改顺序,我知道这与这个问题无关。另外,当我运行程序时,屏幕上显示了电导率值,但由于某种原因无法汇总。

/* Main Function */
int main(void) {
/* Declare and initialize Variables */
double temp, conduc, tempsqrd, temptconduc, sumtemp, sumconduc;
double sumtempsqrd, sumtemptconduc, slope, yint;
int i, ndata;
FILE *therm, *out;

/* Open Files */
therm = fopen(inputfile, "r");
out = fopen(outputfile, "w");

/* print output headings */
printf("******************************************");
printf("\n TEMPERATURE vs THERMAL CONDUCTIVITY"
"\n by Tyrus Sonneborn"
"\n\nTemp Conduct Temp Sqrd Temp*Conduct");
fprintf(out, "******************************************");
fprintf(out, "\n TEMPERATURE vs THERMAL CONDUCTIVITY"
"\n by Tyrus Sonneborn"
"\n\nTemp Conduct Temp Sqrd Temp*Conduct");

/* Verify input file and read control number */
if (therm == NULL) {
printf("\n\n\n\n ERROR OPENING INPUT FILE.");
printf("\n\n PROGRAM TERMINATED.\n\n\n");
return 0;
} else {
/* Read control number */
fscanf(therm, "%i", &ndata);

/* Compute the conductivity data and sums. */
for (i = 1; i <= ndata; i++) {
fscanf(therm, "%lf %lf", &temp, &conduc);
sumconduc = conduc + sumconduc;
tempsqrd = pow(temp, 2);
temptconduc = temp * conduc;
sumtemp = temp + sumtemp;
sumtempsqrd = sumtempsqrd;
sumtemptconduc = sumtemptconduc;
printf("\n %3f %4.0f %6.0f %6.0f",
temp, conduc, tempsqrd, temptconduc);
fprintf(out, "\n %3f %5.1f %6.0f %6.0f",
temp, conduc, tempsqrd, temptconduc);
}
}
/* computing the slope and y intercept */
slope = (sumtemp * sumconduc - ndata * sumtemptconduc) / (pow(sumtemp, 2) - ndata * (pow(sumtemp, 2)));
yint = (sumconduc - slope * sumtemp) / ndata;
printf("\n\n%10f, %10f, %10f, %10f",
sumtemp, sumconduc, sumtempsqrd, sumtemptconduc);
fprintf(out, "\n\n%10f, %10f, %10f, %10f",
sumtemp, sumconduc, sumtempsqrd, sumtemptconduc);

最佳答案

for循环中,你使用未初始化的sumconduc:

sumconduc = conduc + sumconduc;

您应该将其初始化为0sumtempsumtempsqrdsumtempconduc 也是如此。

通过 gcc -Wall -Wextra 启用警告进行编译会诊断出此类愚蠢的错误。

关于c - 运行程序返回 -1.#QNAN0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383165/

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