I have data files, one of them look like this :
All the files have "NA"s in them. I need to read these files and find average of the numbers on second column ignoring the "NA" ones. I want to write a C code. I have made a test data file that looks like this :
And I wrote this test code :
#include<stdio.h>
int main()
{
FILE *f = fopen("a.txt", "r");
int num1, num2;
int i;
char c;
int count = 0;
double avg = 0.0;
c = fgetc(f);
while(c = fgetc(f) != EOF)
{
if(c != 'N' || c != 'A')
{
fscanf(f, "%d %d", &num1, &num2);
count += 1;
avg = avg + num2;
printf("%d\n", num2);
}
}
fclose(f);
avg = avg/count;
printf("Avg = %g\n", avg);
}
This code does not give the desired output. So what I want is num2 (the second column of the data file) for the above data file to look like this, ignoring the "NA" :
1
9
16
How to write C code to perform this task?