Thread: Fault average

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    47

    Exclamation Fault average

    Hello friends,

    I am trying to make a simple average function but the resukts are faulty

    say i enter 45 ,51 ,23 for 3 numbers but average shows 39.00 instead of 39.66

    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    
    
    int main(){
    
    
     average();
    
    
    return 0;
    
    
    }
    
    
    void average(){
        int a,b,c;
        int total;
        float avg = 0;
        printf("\nEnter 3 numbers: ");
        scanf("\n %d %d %d",&a,&b,&c);
    
    
        total = a+b+c;
        printf("Sum of 3 numbers is %d",total);
        avg =  total/3;
        printf("\nAverage of 3 numbers is %.2f",avg);
    return avg;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should do floating point division, say by involving a type cast, rather than integer division.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Declare "total" as a float instead of an int

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Click_here View Post
    Declare "total" as a float instead of an int
    Or:
    Code:
    avg = total / 3.0;
    And let type promotion do its part.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by flp1969 View Post
    Or:
    Code:
    avg = total / 3.0;
    And let type promotion do its part.
    Integer promotion still takes place, just earlier.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Click_here View Post
    Integer promotion still takes place, just earlier.
    A small correction:
    Code:
    avg = total / 3.0f;
    avg is float, total is int and 3.0f is float... 'total' will be promoted to float to satisfy the expression.
    There is no need to "retipify" 'total' to float here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Average of odd and even
    By Abcdefji in forum C Programming
    Replies: 4
    Last Post: 07-25-2016, 01:21 AM
  2. Replies: 9
    Last Post: 06-25-2015, 04:10 PM
  3. Replies: 27
    Last Post: 02-14-2015, 11:17 AM
  4. help with average
    By tmoney$ in forum C Programming
    Replies: 3
    Last Post: 05-09-2003, 05:46 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread