#include <stdio.h>
int main(void)
{
FILE *in;
float day[365],sup[365],mich[365],huron[365],erie[365],ont[365],stclr[365];
int i,j;
float year;
float sup_sum,mich_sum,huron_sum,erie_sum,ont_sum,stclr_sum;
float sup_avg,mich_avg,huron_avg,erie_avg,ont_avg,stclr_avg;
int counter = 0;
in = fopen("C:\\Users\\TEMP.DESKTOP-O4G383R.000\\Desktop\\CPS Term Project\\term_proj_data.txt","r");
while ( !feof(in) )
{
for( i = 0; i < 2920; i++)
{//outer for loop
for (j = 0; j < 8; j++)
{//inner for loop
if (j == 0)
fscanf(in, "%f", &year);
if (j == 1)
fscanf(in, "%f", &day[i]);
if (j == 2)
fscanf(in, "%f", &sup[i]);
if (j == 3)
fscanf(in, "%f", &mich[i]);
if (j == 4)
fscanf(in, "%f", &huron[i]);
if (j == 5)
fscanf(in, "%f", &erie[i]);
if (j == 6)
fscanf(in, "%f", &ont[i]);
if (j == 7)
fscanf(in, "%f", &stclr[i]);
}//inner for loop
}//outer for loop
}
printf("%.2f", stclr[5]);
/*--------------Calculating averages----------------------*/
//lake Superior
for (i = 0; i < 365; i++)
{
sup_sum += sup[i];
counter++;
}
sup_avg = sup_sum / counter;
printf("\n\n Lake Superior temp avg:%f", sup_avg);
counter = 0;
//lake Michigan
for (i = 0; i < 365; i++)
{
mich_sum += mich[i];
counter++;
}
mich_avg = mich_sum / counter;
printf("\n\n Lake Michigan temp avg:%f", mich_avg);
counter = 0;
//lake Huron
for (i = 0; i < 365; i++)
{
huron_sum += huron[i];
counter++;
}
huron_avg = huron_sum / counter;
printf("\n\n Lake Huron temp avg:%f", huron_avg);
counter = 0;
//lake Erie
for (i = 0; i < 365; i++)
{
erie_sum += erie[i];
counter++;
}
erie_avg = erie_sum / counter;
printf("\n\n Lake Erie temp avg:%f", erie_avg);
counter = 0;
//lake Ontario
for (i = 0; i < 365; i++)
{
ont_sum += ont[i];
counter++;
}
ont_avg = ont_sum / counter;
printf("\n\n Lake Ontario temp avg:%f", ont_avg);
counter = 0;
//lake St.Clair
for (i = 0; i < 365; i++)
{
stclr_sum += stclr[i];
counter++;
}
stclr_avg = stclr_sum / counter;
printf("\n\n Lake St.Clair temp avg:%f", stclr_avg);
counter = 0;
return 0;
}
我的程序的整个想法是从txt文件中读取值并将它们存储到数组中。然后,我必须计算每个湖泊365天的平均温度。除了最后的平均温度值不正确之外,一切对我来说都很好。
我不太确定是什么原因造成的。我不是C语言方面的专家。我是一名正在学习该语言的学生。