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 ;
}

>it goes in a infinite loop
Because you're reading integers and not handling the commas:

fscanf(fp,"%d",&ch);

This will always try to read a valid integer. fscanf doesn't find a valid integer, it'll fail and leave the offending character on the stream. ',' is not a valid integer character, so your check for it a few lines down is too little and too late. Try using %c instead of %d, but be aware that there are other problems with your program. I'll give you a chance to work them out on your own though.

Thanks for the previous reply but I have tried to read the file using getc() in that case it takes the individual number as character and read the ascii value means if the file has 1,2,3,4, then it take 1 as 49,2 as 50 n so forth,moreover if i enter a 2or 3 digit number it read then individual character.

And with fread also thought it is for binary file but again same the program goes in a infinite loop.

Please help me out in this situation if possible.

>it goes in a infinite loop
Because you're reading integers and not handling the commas:

fscanf(fp,"%d",&ch);

This will always try to read a valid integer. fscanf doesn't find a valid integer, it'll fail and leave the offending character on the stream. ',' is not a valid integer character, so your check for it a few lines down is too little and too late. Try using %c instead of %d, but be aware that there are other problems with your program. I'll give you a chance to work them out on your own though.

>I have tried to read the file using getc() [...]
Yes, and that's one of the problems I was referring to.

>Please help me out in this situation if possible.
Use fscanf with %d, then getc to read a single character. That reads a valid integer and then trims the next comma.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.