435,491 Members | 3,132 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

averaging numbers in text file

 P: n/a I'm trying to add numbers from a text file and find the average. The numbers in the file are: 25 50 75 100 I expect the result to be 62.50 but I get 85.00. I've twiddled with the condition expression of the for statement but apparently I have no idea what I'm doing. Post-increment or pre-increment seems to make no difference. Any help appreciated. /* avg.c : find the average of n integers in a text file */ #include int main() { int num, i; float sum; for(i = 0; i != '\n'; i++) { scanf("%d", &num); sum += num; } printf("The average is %.2f\n", sum/i); return 0; } Thanks, Bill Nov 13 '05 #1
9 Replies

 P: n/a Bill Reed wrote: On Tue, 22 Jul 2003 20:46:00 -0700, Bertrand Mollinier Toublet wrote: Assuming an architecture with ASCII character set, we have '\n' == 10.Thus, for i going from 0 to 3, you scan the four numbers in your file,so that sum equals 250. For the next 6 attempts, the scanf executionfails to interpret anything, and, I suppose, num is unchanged, so thatyou actually add 6 * 100 = 600 to your sum.At the end of the for loop, we thus have sum == 850. So that youraverage is 850 / 10 = 85. Although, please note that this is a probable explanation, but nothing you can rely on. Remember that when you have undefined behaviour in your program (sun being uninitialized, which I missed, is an example), *anything* can happen, and it generally doesn't bring much to try and explain what happened. The typical example of "Don't explain" is anything along the lines of i = i++ + ++i; You can spend hours saying "the good result should be this or that because this or that", but it is all moot: the expression invokes undefined behaviour, is illegal, and is allowed to yield any result it wants including letting DEMONs (those missile-mounted nuclear warheads) fly out of the local NOSE (the operating center to launch the warheads). -- Bertrand Mollinier Toublet "No sea vivo, Buendia" -- El presidente del tribunal, in Cien anos de soledad, de Gabriel Garcia Marquez Nov 13 '05 #3

 P: n/a On Wed, 23 Jul 2003 03:13:26 GMT, Bill Reed wrote:I'm trying to add numbers from a text file and find the average. Thenumbers in the file are:25 50 75 100I expect the result to be 62.50 but I get 85.00. with scanf would be fine use switch + goto #include int main() {int num=0, i, c; float sum=0;/* here num=0, sum=0 */ printf("Inserisci i numeri: "); fflush(stdout); for(i=0;;) {switch(scanf("%d[^\n]", &num)) {case 1: sum += num; ++i; if((c=getchar())==EOF || c=='\n') goto label; else ungetc(c, stdin); break; case 0:/*if scanf meets one letter (a,b,c..) or EOF end*/ case EOF: goto label; } } label: printf("The average is %.2f\n", (float) (sum/i) ); return 0; } Nov 13 '05 #4

 P: n/a #include int main(){int num=0, i, c; float sum=0;/* here num=0, sum=0 */ printf("Inserisci i numeri: "); fflush(stdout); for(i=0;;) {switch(scanf("%d[^\n]", &num)) {case 1: sum += num; ++i; if((c=getchar())==EOF || c=='\n') goto label; else ungetc(c, stdin); break; case 0:/*if scanf meets one letter (a,b,c..) or EOF end*/ case EOF: goto label; } }label: i==0?++i: 0; printf("The average is %.2f\n", (float) (sum/i) ); return 0;} Nov 13 '05 #5

 P: n/a On Wed, 23 Jul 2003 09:11:49 -0700, "Mike Wahler" wrote: So far I have this but I need to do more reading. /* avg.c : find the average of n integers in a text file */ #include int main() { int num, i; double sum = 0; FILE *fptr1; char file1[] = "avg.txt"; fptr1 = fopen(file1, "r");YOu need to check that the file was opened successfully.If 'fopen()' returned NULL, it was not, and you shouldnot try to read from the file. By 'opened successfully' do you mean that the file exits? When I use the command: avg < avg.txt if avg.txt does not exist the system lets me know. Or are there other problems I might encounter? for(i = 0; !feof(fptr1); i++)You're using feof() incorrectly. It doesn't indicateend of file until *after* an attempt to read *past*end of file. { scanf("%d", &num); sum += num; } printf("The average is %.2f\n", sum/i); return 0; } It compiles with no errors or warnings but doesn't do anything.Actually, it does. It waits for *you* to do something.'scanf()' takes input from 'standard input', which istypically a 'console' on systems that have one. If youwant to read from a file, use 'fscanf()' or 'fgets()'or 'fgetc()' etc.BTW which book(s) are you reading? Perhaps you needbetter one(s). I looked up feof() in "Teach Yourself C in 24 Hours". I purchased K&R 2 yesterday. The former books description of the function seems comparable to K&R 2 but his program examples don't appear to lend themselves to my application. Probably I just need to read and think about it some more. Bill Nov 13 '05 #6

 P: n/a On Thu, 24 Jul 2003 05:56:25 GMT, gi******@giuseppe.wwwew (Giuseppe) wrote: On Wed, 23 Jul 2003 03:13:26 GMT, Bill Reed wrote:I'm trying to add numbers from a text file and find the average. Thenumbers in the file are:25 50 75 100I expect the result to be 62.50 but I get 85.00.with scanf would be fine use switch + goto#include int main(){int num=0, i, c; float sum=0;/* here num=0, sum=0 */ printf("Inserisci i numeri: "); fflush(stdout); for(i=0;;) {switch(scanf("%d[^\n]", &num)) {case 1: sum += num; ++i; if((c=getchar())==EOF || c=='\n') goto label; else ungetc(c, stdin); break; case 0:/*if scanf meets one letter (a,b,c..) or EOF end*/ case EOF: goto label; } }label: printf("The average is %.2f\n", (float) (sum/i) ); return 0;} This is interesting. If I remove the printf() and fflush() line the result is exactly what I was looking for. As mentioned in another post I should probably declare sum as a double so it doesn't need to be converted later. This is very different from anything I was considering as a solution. Thanks. Bill Nov 13 '05 #7

 P: n/a Bill Reed wrote: if avg.txt does not exist the system lets me know. Or are there other problems I might encounter? Bill... On my system fopen() can fail even when the file exists if I don't have the requisite file permission(s). Probably I just need to read and think about it some more. (A winning strategy! 8^) -- Morris Dovey West Des Moines, Iowa USA C links at http://www.iedu.com/c Nov 13 '05 #8

 P: n/a On Thu, 24 Jul 2003 11:10:57 -0500, Bill Reed wrote: On Thu, 24 Jul 2003 05:56:25 GMT, gi******@giuseppe.wwwew (Giuseppe)wrote:On Wed, 23 Jul 2003 03:13:26 GMT, Bill Reed wrote:I'm trying to add numbers from a text file and find the average. Thenumbers in the file are:25 50 75 100I expect the result to be 62.50 but I get 85.00.with scanf would be fine use switch + goto#include int main(){int num=0, i, c; float sum=0;/* here num=0, sum=0 */ printf("Inserisci i numeri: "); fflush(stdout); for(i=0;;) {switch(scanf("%d[^\n]", &num)) {case 1: sum += num; ++i; if((c=getchar())==EOF || c=='\n') goto label; else ungetc(c, stdin); break; case 0:/*if scanf meets one letter (a,b,c..) or EOF end*/ case EOF: goto label; } }label: printf("The average is %.2f\n", (float) (sum/i) ); return 0;}This is interesting. If I remove the printf() and fflush() line theresult is exactly what I was looking for. As mentioned in another postI should probably declare sum as a double so it doesn't need to beconverted later. This is very different from anything I wasconsidering as a solution. Thanks.Bill please you note that if there is not input => case 0 => i=0 => sum/i ERROR Nov 13 '05 #9

 P: n/a On Thu, 24 Jul gi******@giuseppe.wwwew (Giuseppe) wrote:#include int main(){int num=0, i, c; float sum=0;/* here num=0, sum=0 */ printf("Inserisci i numeri: "); fflush(stdout); for(i=0;;) {switch(scanf("%d[^\n]", &num)) {case 1: sum += num; ++i; if((c=getchar())==EOF || c=='\n') goto label; else ungetc(c, stdin); break; case 0:/*if scanf meets one letter (a,b,c..) or EOF end*/ case EOF: goto label; } }label: i==0?++i: 0; printf("The average is %.2f\n", (float) (sum/i) ); printf("The average is %.2f\n", (double) (sum/i) ); ^^^^^^^^ Why for scanf %f is float and for printf %f is double? return 0;} Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion. 