By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 424,686 Members | 2,589 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

# AVERAGE from file pls help

 P: 9 HI all Am a newbie in C. I am trying to calculate the average of some numbers (floating point) from a file : the file format is as follows 0 3.312000 3.312000 3.312000 1 3.362000 3.362000 3.362000 2 3.370000 3.370000 3.370000 3 3.340000 3.340000 3.340000 the first column is the index number . the rest 3 columns are same with the same values. i tried my code . but it went wrong. Any idea is welcomed. Expand|Select|Wrap|Line Numbers #include  #include  #include  #include    #define N 500   // total number   main ()   {     int indx;     int total =0,count=0;     float box1,box2,box3, average =0;     FILE *fp1;       fp1 = fopen ("bxlength.g87","r");       while (fscanf (fp1,"%d%f%f%f",&indx,&box1,&box2,&box3)!=EOF)       {       total+=box1;     ++count;       }       average =  total/N;           //printf("%d %f %f %f",indx,box1,box2,box3);       printf("%f %f",total,average);           fclose (fp1);     }   Thanks in adavnce Jul 9 '07 #1
Share this Question
2 Replies

 Expert Mod 5K+ P: 9,197 You are adding only box1 to the total. average = total/N; This divides the total by 500 but you don't have 500 numbers. Fix this and let me know. Jul 9 '07 #2

 P: 67 Also, total is an integer, not really the best idea when averaging floating point numbers. And average is never declared. and should also be floating point. Jul 9 '07 #3

### Post your reply

Sign in to post your reply or Sign up for a free account.

### Similar topics

Browse more C / C++ Questions on Bytes