Hi all! So this homework problem is similar to that one I posted last time (that I completely gave up on! I will just take the hit on my grade). Anyway, my instructions are to find the average of column 2, the maximum, the location of the fist maximum, and the location of the last maximum of an external file. The number of the second column are only read in if the line in which they reside begin with a 1. So even though you would think 66 is the max, it is invalid because the first number is a 0.
I have everything so far, except the last max. It is printing 0 instead of 7.
The external file reads:
0 2
1 8
0 66
0 2
1 6
1 8
1 8
And here is my code:
Code:
int main(void)
{
int flag, x, count=0, j=1, flag1=0, max, locmax, last;
float sum=0.0, ave;
FILE *inf;
inf = fopen("readme.dat", "r");
while(fscanf (inf, "%i %i", &flag, &x )==2)
{
if (flag==1)
{
sum = sum + x;
++count;
j++;
if (flag == 0)
{
max=x;
flag1 = 1;
locmax = j;
}
if (x>max)
{
max = x;
locmax = j;
}
if (x>max)
{
max = x;
last = j; <========= I am sure this is where my problem lies, I'm just not seeing what I am doing wrong
}
}
ave = sum/count;
printf("Average = %7.2f \n", ave);
printf("Max = %2i \n", max);
printf("Loc of First Max = %2i \n", locmax);
printf("Loc of Last Max = %2i \n", last);
return 0;
}
Like I said, the program compiles correctly but it prints out 0 for the location of the last max. Any help would be greatly appreciated.