So while doing this assignment i encountered a problem where i tried to save some set of values(float) in an Array so that i can use them later on producing a graph, but the problem which i face here is that i read the values and i can print them but later which i checked the array the numbers which were stored there were not the same.
Im trying to save in in avg[].
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
float maximum(float array[])
{
float max=0;
for(int i=0;i<100;i++)
{
if (array[i]>max)
max=array[i];
}
return max;
}
float minimum(float array[])
{
float min=0;
for(int i=0;i<100;i++)
{
if (array[i]<min)
min=array[i];
}
return min;
}
char * read(char *filename)
{
FILE * sample;
sample = fopen(filename,"r"); //like file open in notepad ,which file? and what to do?
int count = 0;
static char singleline[100];
int cnt = 0;
int sum = 0;
int oldyear = -1;
float avg[82];
while(!feof(sample)) //read that until end.
{
count++;
fgets(singleline,150,sample);
if (count>21 && singleline[33]!='9')
{
char y[5], t[6];
for (int i = 14; i < 18; i++)
{
y[i - 14] = singleline[i];
}
y[4]='\0';
for (int i= 24;i <29;i++)
{
t[i-24]=singleline[i];
}
t[5]='\0';
int year = atoi(y);
int temp = atoi(t);
//printf("year : %i ,temp: %i\n",year, temp);
if (year == oldyear)
{
cnt++;
sum += temp;
}
else
{
int l=0;
l++;
avg[l] = 0.1 * sum / cnt;
if (cnt != 0)
{
printf("Fuer %i - %i Werte - Durchschnitt %.2f °C\n", oldyear, cnt, avg[l]);
cnt = 1;
sum = temp;
//save[l]=avg;
}
oldyear = year;
}
}
}
float last = 0.1 * sum / cnt;
printf("Fuer %i - %i Werte - Durchschnitt %.2f °C\n", oldyear, cnt-1, last);
fclose(sample);
//for(int i=0;i<)
for(int i=0;i<125;i++)
{
printf("%f\n",avg[i]);
}
printf("\nMax Temp= %f",maximum(avg));
printf("\nMax Temp= %f",minimum(avg));
return singleline;
}
int main()
{
char * file1 = "TG_STAID000476.txt";
read(file1);
//char * file2 = "TG_STAID000179.txt";
//read(file2);
return 0;
}
So yea, the problem was to read the year and the corresponding values and form an Average value for that particular year and then represent it in a graph.
I can do the first part where it takes the Average, but when i tried using it later,it had wrong values stored in it, you can see that where i tried to print all the values of avg[], can anyne please help me figure out how to correct the mistake, i want them to be saved as float.
The assignment datasheet is here. https://www.scribd.com/document/333844245/TG-STAID000179
I tried reading the values and used atoi to save them, and then used them to get the Average, i used Count>21 because the text before them are not required and when it reads a '9' on 34th column,it ignores since its not a valid data(in data sheet)
l
, intended to count the computed year temperature averages, is defined and initialized in the block where a year change is handled, thus always reset to zero. Correct this by moving the definition outside of the line read loop, perhaps even at global scope (see 2.). maximum()
and minimum()
functions operate on array
elements 0 to 99, irrespective of the fact that the passed argument is defined as avg[82]
and some elements at the end are not initialized. Correct this by using the actual number l
of stored elements, either as a global scope variable or an additional function argument. singleline
buffer is too short and thus overflows. while(!feof(sample))
loop cycles one extra time after the last line has been read, because EOF is not recognized before the next fgets()
. Correct this by using the return value of fgets()
to test EOF. avg[l] = 0.1 * sum / cnt
is evaluated even if cnt
is zero. Correct this by moving this expression inside the if (cnt != 0) { … }
block, also the l++
, but behind the printf()
. cnt = 1
is not executed if cnt
is zero. This causes the very first data point to not be counted. Correct this by moving this expression behind the if (cnt != 0) { … }
block. cnt-1
in the final printf("Fuer %i - %i Werte - Durchschnitt %.2f °C\\n", oldyear, cnt-1, last);
, but correct is just cnt
. for(int i=0;i<125;i++)
should also use the actual number l
of stored elements, not 125
. avg[]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.