My question is to calculate the mean of a comma separated dataset stored in a file. The program will be run on a system with no floating point library. Obey the following rules:
1.Maximum 100 values in dataset
2.Each data entry in the dataset is a byte (0 - 255)
I wrote the following code but when i execute it goes in a infinite loop,moreover I have anothe doubt Why might a system not have access to the floating point library?
#include<stdio.h>
#include<ctype.h>
main(int argc,char *argv[])
{
FILE *fp;
int ch=0;
int num=0,counter=0;
float mean=0;
if(argc !=2)
{
printf("\n You hv not entered the file name");
exit(1);
}
if((fp=fopen(argv[1],"r"))== NULL)
{
printf("\n Cannot open the file");
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%d",&ch);
if(ch>255)
{
printf("value out of range");
}
if(ch==',')
{
printf("%d",ch);
}
else
{
printf("%d",ch);
num = num + ch;
counter++;
}
}
if(counter >100)
printf("\n mean cannot be calculated");
else
{
mean = num/counter;
printf("\n Mean is %f",mean);
}
fclose(fp);
return ;
}