Homework help (again)... So simple, its hard!
Hey all again... Ready for this one??
I need to read from an external file called data3.dat, which I inputted....
Code:
0 3
0 6
1 10
1 8
1 11
0 9
1 30
1 7
1 8
If there is a 0 (column 1), ignore the corresponding number next to it (colunm 2). If there is a 1 in column 1, find the average of all the numbers in column 2 that correspond to 1's in column one. So basically, take the average of all the numbers that have a 1 in front of them. Then find the max. I am okay up to right here... here is my code;
Code:
/* Homework 14 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int flag, x, count=0, j=1, flag1=0, max;
float sum=0.0, ave;
FILE *data;
data = fopen("data3.dat", "r");
while(fscanf (exam, "%i %i", &flag, &x )==2)
{
if (flag==1)
{
sum = sum + x;
++count;
if (flag == 0)
{
max=x;
flag1 = 1;
}
if (x>max)
{
max = x;
}
}
}
ave = sum/count;
printf("Average = %5.2f \n", ave);
printf("Max = %2i \n", max);
return 0;
}
Now I have to print out all the locations of the max value in column 2. If there is more than 5 max values, print "More than five maxima. \n"
Help me please, I am not sure where to start!!