Thread: Newbie C Progammer - Why getting logic error in my simple program?

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    5

    Newbie C Progammer - Why getting logic error in my simple program?

    Hello sir, I just have started to learn C programming from a few days and I am a total newbie. I created a simple program of printing the average of three integers (input by user) to one decimal place. My correct program is:

    Code:
    //Write a Program to request three integers...
    //...and print their average to one decimal place.
    
    #include <stdio.h>
    
    main()
    {
        int a,b,c;
        printf("\n  Enter the values of a,b,c = ");
        scanf("%d %d %d",&a,&b,&c);
        double d=a+b+c;
        printf("\n  The Average of a,b,c to one decimal place is = %1.1f \n", d/3);
    }
    Now this program runs fine without any problem giving correct results. But when I write the statement 'double d=a+b+c' before first printf function (like in below program) instead of the place as in above, then I get a logic error (example image at the bottom) giving absurd value result on input values.

    Code:
    #include <stdio.h>
    
    main()
    {
        int a,b,c;
        double d=a+b+c;
        printf("\n  Enter the values of a,b,c = ");
        scanf("%d %d %d",&a,&b,&c);
        printf("\n  The Average of a,b,c to one decimal place is = %1.1f \n", d/3);
    }
    Please anybody help me understanding the problem in the second program as what is wrong in it.

    Newbie C Progammer - Why getting logic error in my simple program?-screenshot-2017-02-24-04-35-07-jpg
    Last edited by omani; 02-23-2017 at 05:08 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by omani View Post
    But when I write the statement 'double d=a+b+c' before first printf function (like in below program) instead of the place as in above, then I get a logic error (example image at the bottom) giving absurd value result on input values.

    Code:
    ...
    
    main()
    {
        int a,b,c;
        double d=a+b+c;
        printf("\n  Enter the values of a,b,c = ");
        scanf("%d %d %d",&a,&b,&c);
        printf("\n  The Average of a,b,c to one decimal place is = %1.1f \n", d/3);
    }
    In this program, you are initializing d with uninitialized values a, b and c. Since you didn't initialize a, b and c, their contents is undefined (it could be anything). They could have zeros or strange values in there, so the result of adding three such values together and then averaging them will be unpredictable.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    Code:
        double d=a+b+c;
        printf("\n  Enter the values of a,b,c = ");
        scanf("%d %d %d",&a,&b,&c);
        printf("\n  The Average of a,b,c to one decimal place is = %1.1f \n", d/3);
    What you're suggesting here is that d should always be the sum of a,b,c.
    Such that, if any of them change, then d is re-computed automatically when necessary.
    C isn't one of these kinds of languages -> Declarative programming - Wikipedia


    C is imperative. Everything is done in exactly the order you write it in your program.
    So if you get the order of statements wrong, your program breaks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    5
    Quote Originally Posted by c99tutorial View Post
    In this program, you are initializing d with uninitialized values a, b and c. Since you didn't initialize a, b and c, their contents is undefined (it could be anything). They could have zeros or strange values in there, so the result of adding three such values together and then averaging them will be unpredictable.
    Thanks a lot sir, your simple explanation really cleared my confusion totally.

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    5
    Quote Originally Posted by Salem View Post
    Code:
        double d=a+b+c;
        printf("\n  Enter the values of a,b,c = ");
        scanf("%d %d %d",&a,&b,&c);
        printf("\n  The Average of a,b,c to one decimal place is = %1.1f \n", d/3);
    What you're suggesting here is that d should always be the sum of a,b,c.
    Such that, if any of them change, then d is re-computed automatically when necessary.
    C isn't one of these kinds of languages -> Declarative programming - Wikipedia


    C is imperative. Everything is done in exactly the order you write it in your program.
    So if you get the order of statements wrong, your program breaks.
    Thanks for replying sir.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Also, likely to have issues with "d/3" you will likely want "d/3.0", instead.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic error in larger program, troubleshoot tips/help
    By Glassjaw in forum C Programming
    Replies: 4
    Last Post: 12-05-2015, 03:21 AM
  2. Replies: 4
    Last Post: 09-16-2015, 07:51 AM
  3. A logic error in my program
    By 20120903 in forum C Programming
    Replies: 5
    Last Post: 09-06-2012, 04:00 AM
  4. C Program logic error?
    By bigmac(rexdale) in forum C Programming
    Replies: 27
    Last Post: 02-15-2008, 11:20 AM
  5. Replies: 5
    Last Post: 01-31-2006, 01:54 AM

Tags for this Thread