Reputation: 3
The task at hand is to calculate the yearly average temperature for each of the lakes, and the yearly average for all six lakes put together.
Text needs to be read from the file below, when I saved it onto my desktop, I made sure to remove all the lines excluding the one's with actual data so I only have numbers on my text file, which begins
Daily Lake Average Surface Water Temperature
From
Great Lakes Surface Environmental Analysis maps
--------------------------------------------------------
Surf. Water Temp. (degrees C)
Year Day Sup. Mich. Huron Erie Ont. St.Clr
--------------------------------------------------------
2017 001 3.88 4.36 4.00 3.06 5.03 2.06
2017 002 3.46 4.36 3.98 3.05 5.01 2.12
2017 003 3.33 4.36 3.94 3.01 5.00 2.14
...etc
I have attached the code that I have tried, which worked at school but not on my personal computer.
#include <stdio.h>
#include <math.h>
int main()
{
double line[365][8], sup, mich, huron, erie, ont, stclr, avesup, avemich, avehuron, aveerie, aveont, avestclr, ave;
int row, column;
FILE *fp;
fp = fopen("data.txt", "r");
for(row=1;row<=365;row++)
{
for(column=1;column<=8;column++)
{
fscanf(fp, "%lf", &line[row][column]);
}
}
for(row=1;row<=365;row++)
{
printf("%.0lf\t%.0lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n",
line[row][1], line[row][2], line[row][3], line[row][4], line[row][5],
line[row][6], line[row][7], line[row][8]);
}
for(row=1;row<=365;row++)
{
sup = sup + line[row][3];
}
avesup = sup/365;
printf("%.2lf\n", avesup);
for(row=1;row<=365;row++)
{
mich = mich + line[row][4];
}
avemich = mich/365;
printf("%.2lf\n", avemich);
for(row=1;row<=365;row++)
{
huron = huron + line[row][5];
}
avehuron = huron/365;
printf("%.2lf\n", avehuron);
for(row=1;row<=365;row++)
{
erie = erie + line[row][6];
}
aveerie = mich/365;
printf("%.2lf\n", aveerie);
for(row=1;row<=365;row++)
{
ont = ont + line[row][7];
}
aveont = ont/365;
printf("%.2lf\n", aveont);
for(row=1;row<=365;row++)
{
stclr = stclr + line[row][8];
}
avestclr = stclr/365;
printf("%.2lf\n", avestclr);
ave = avesup + avemich + avehuron + aveerie + aveont + avestclr;
printf("%.2lf", ave);
fclose(fp);
return 0;
}
It worked at school and averaged fine but now it is not working.
Upvotes: 0
Views: 1020
Reputation: 32596
in all the
for (row=1;row<=365;row++)
the range of index is invalid, must be
for (row=0;row<365;row++)
and of course same problem in
for(column=1;column<=8;column++)
whose must be
for(column=0;column<8;column++)
Also
printf("%.0lf\t%.0lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", line[row][1], line[row][2], line[row][3], line[row][4], line[row][5], line[row][6], line[row][7], line[row][8]);
must be
printf("%.0lf\t%.0lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n",
line[row][0], line[row][1], line[row][2], line[row][3], line[row][4],
line[row][5], line[row][6], line[row][7]);
The behavior when you go out of an array is undefined, this is why it (seems) to work on some platforms and not on others
In an array the indexes start at 0, not at 1
Upvotes: 2