Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Average

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Malaysia
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Average

    Dear sir,

    I want to find the average of the four values as below. Why my average was displayed in 15.676126 instead of the correct average is 15.676125.

    #include <stdio.h>
    #include <conio.h>

    void main()
    {
    int v1 = 4;
    int v2 = 42;
    float v3 = 16.7;
    float v4 = 0.0045;
    float average;

    clrscr();
    printf("The four number are:\n");
    printf("%i %i %g %g\n\n", v1, v2, v3, v4);
    printf("The average is:\n");

    average = (v1 + v2 + v3 + v4)/4;
    printf("%f", average);
    getch();
    }


    Thanks,

    Xiang

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Probably a floating-point round-off error. Use double for higher precision


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •