Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #10290

Arithmetic error

From Andy Foot <foot@dev.null.invalid>
Newsgroups comp.lang.c
Subject Arithmetic error
Date 2011-09-15 20:50 +0000
Organization Aioe.org NNTP Server
Message-ID <j4toe7$1ls$1@speranza.aioe.org> (permalink)

Show all headers | View raw


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!

Thanks,
Andy

#include <stdio.h>
void main()
{
    char buf[BUFSIZ];
    int a=0,b=0;
    while(strcmp(gets(buf),"")!=0){
        a++; b+=atoi((char*)buf);
    }
    printf("mean=%f",(float)(b/a));
}

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


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