Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Arithmetic error |
| Date | 2011-09-15 22:34 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <j4tr35$h3j$1@dont-email.me> (permalink) |
| References | <j4toe7$1ls$1@speranza.aioe.org> |
"Andy Foot" <foot@dev.null.invalid> wrote in message
news:j4toe7$1ls$1@speranza.aioe.org...
> Hello
>
> I have recently started a distance-learning C course.
>
> I am writing a program to input some numbers and calculate they're mean
> (average). However there is a bug: if the numbers are 1,2,3 then the
> calculation is correct (mean=2) however if the numbers are 1,2 then the
> answer is wrong (mean=1, should be 1.5).
>
> What's going on!
Try this version. The main change is moving the (float):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char buf[BUFSIZ];
int a=0,b=0;
puts("Enter one number per line. Blank line to end:");
while(strcmp(gets(buf),"")!=0){
a++; b+=atoi((char*)buf);
}
if (a) printf("Mean=%f\n",((float)b/a));
}
BTW you should use a newer textbook. (And where is BUFSIZ declared? Seems to
compile though..)
--
bartc
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Arithmetic error Andy Foot <foot@dev.null.invalid> - 2011-09-15 20:50 +0000
Re: Arithmetic error Ian Collins <ian-news@hotmail.com> - 2011-09-16 08:56 +1200
Re: Arithmetic error Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-09-15 14:07 -0700
Re: Arithmetic error "BartC" <bc@freeuk.com> - 2011-09-15 22:34 +0100
Re: Arithmetic error Ike Naar <ike@sverige.freeshell.org> - 2011-09-15 22:07 +0000
Re: Arithmetic error Ike Naar <ike@sverige.freeshell.org> - 2011-09-15 22:04 +0000
Re: Arithmetic error Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-15 22:52 +0100
Re: Arithmetic error Fred <fred.l.kleinschmidt@boeing.com> - 2011-09-16 11:37 -0700
csiph-web