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 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    ON, Canada
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Unwanted addition in my C program using "while"

    SOLVED

    Hello all. I am brand new to programming and would like just an explanation as to exactly why I am getting a specific result

    After learning if, else, while, and a couple other things I decided to think of my own little simple program to see how I do without copying from the book. Figured it would help me gain some basic critical thinking for programming when I'm the one creating my own problems and fixing my own problems. Everything went according to the way I wanted it to, however it seems that somehow I am getting one final unwanted addition outside my while loop, which is what I'm trying to figure out.

    Here's the code (My program goal parameters written in the comment on top)

    Code:
    /* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
    #include <stdio.h>
    main()
    {
        int doubles = 0, triples = 0, singles = 0;
    
        while (doubles <= 100){
            printf("doubles tally: %d\n", doubles);
            printf("triples tally: %d\n", triples);
            printf("singles tally: %d\n", singles);
            doubles = doubles + 2;
            triples = triples + 3;
            singles = singles + 1;
        }
        printf("dude, that's a lot of doubles,  triples, and singles\n");
        printf("doubles total: %d\n", doubles);
        printf("triples total: %d\n", triples);
        printf("singles total: %d\n", singles);
    
    return 0;
    }
    When I build and run, everything is just peachy except the doubles total is 102, the triples total is 153, and singles total is 51, when they should all respectively be 100, 150, and 50 (or at least that was the plan anyway)

    If I remove my four final printf statements, the totals become what I want them to...but I don't understand how the printfs cause the values to change.

    The only explanation I can come up with is that the inclusion of the printfs actually for some reason cause the while to be looped one final [unwanted] time???

    Thanks for your help

    EDIT: I'm 27 and work full time; this is not a homework assignment and I am not a student. (I'm teaching myself via some reading materials and online resources)
    Just didn't want any miscommunication since school is starting back up and homework assignments aren't allowed on the forums.
    Last edited by AGENT_P6; 09-06-2012 at 01:52 AM. Reason: see edit

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Its a simple issue with your loop condition: while (doubles <= 100){. When doubles reaches 100, it proceeds to loop for a final time. Simply compare to < 100 and it will give you the final results of 50, 100 and 150.

  • #3
    New Coder
    Join Date
    Nov 2011
    Location
    ON, Canada
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Its a simple issue with your loop condition: while (doubles <= 100){. When doubles reaches 100, it proceeds to loop for a final time. Simply compare to < 100 and it will give you the final results of 50, 100 and 150.
    Thanks for the reply Fou-Lu.
    Although I understand what you're saying, Im still unsure because if the four printf statements are taken away, i get 50,100,150.

    So what Im saying is when doubles reaches 100 and those four printf statements at the bottom are taken out of the program, the loop does NOT execute that one last time.

    Alternatively, with those printf statements in there, when doubles reaches 100, the loop DOES execute one last time

    Im trying to figure out how in the heck a printf causes this
    sudo chmod / 000ps!

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    if the four printf statements are taken away, i get 50,100,150.
    The increment statements come after the first three printf statements.

    Change your code to
    Code:
     while (doubles < 100){
     doubles = doubles + 2;
            triples = triples + 3;
            singles = singles + 1;       
     printf("doubles tally: %d\n", doubles);
            printf("triples tally: %d\n", triples);
            printf("singles tally: %d\n", singles);
            
        }
    and see what happens
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    AGENT_P6 (09-05-2012)

  • #5
    New Coder
    Join Date
    Nov 2011
    Location
    ON, Canada
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hey again and thanks for the replies.

    I understand that I should be using "is less than 100" rather than "is equal to or less than 100" in order to get the desired results

    What I'm trying to figure out is why this
    Code:
    /* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
    #include <stdio.h>
    main()
    {
        int doubles = 0, triples = 0, singles = 0;
    
        while (doubles <= 100){
            printf("doubles tally: %d\n", doubles);
            printf("triples tally: %d\n", triples);
            printf("singles tally: %d\n", singles);
            doubles = doubles + 2;
            triples = triples + 3;
            singles = singles + 1;
        }
        printf("dude, that's a lot of doubles,  triples, and singles\n");
        printf("doubles total: %d\n", doubles);
        printf("triples total: %d\n", triples);
        printf("singles total: %d\n", singles);
    
    return 0;
    }
    will give me the totals of 102, 153, and 51 WHEN THIS:

    Code:
    /* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
    #include <stdio.h>
    main()
    {
        int doubles = 0, triples = 0, singles = 0;
    
        while (doubles <= 100){
            printf("doubles tally: %d\n", doubles);
            printf("triples tally: %d\n", triples);
            printf("singles tally: %d\n", singles);
            doubles = doubles + 2;
            triples = triples + 3;
            singles = singles + 1;
    
    }
    return 0;
    }
    will give me 100, 150, and 50. Why does the inclusion of the last 4 printf statements cause the loop to execute 51 times, when the EXCLUSION of the last 4 printf statements causes the loop to only execute 50 times

    Sorry for the confusion, as this stuff is new to me.

    I fully understand that I should be using "less than" rather than "equal to or less than". The purpose of my inquiry is only to understand WHY my the first set of code in this post makes for totals of 102, 153, and 51, yet the second set of code in this post results in 100, 150, and 50 when they are BOTH using the "equal to or less than" operator

    Sorry to be a pain...I'm just really inquisitive and want to know exactly why things work the way that they do
    sudo chmod / 000ps!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The first one shows 1 to many versus just removing them since the values are incremented in the loop after the printfs. When condition is <=100, then it increments them once more leaving them with the values 51, 102 and 153.
    Removing the = sign will leave the variables with the values 50, 100, and 150 after the loop, so you can still add the last four printf's if desired.

  • Users who have thanked Fou-Lu for this post:

    AGENT_P6 (09-05-2012)

  • #7
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,267
    Thanks
    10
    Thanked 277 Times in 276 Posts
    Code:
    while (doubles <= 100){
            printf("doubles tally: %d\n", doubles);
            printf("triples tally: %d\n", triples);
            printf("singles tally: %d\n", singles);
            doubles = doubles + 2;
            triples = triples + 3;
            singles = singles + 1;
    }
    This code will print out (on its last iteration through the loop) 100, 150, 50, but immediately after you print those values out, you then execute:
    Code:
            doubles = doubles + 2;
            triples = triples + 3;
            singles = singles + 1;
    Which sets doubles to 102, triples to 153 and singles to 51. Correct?

    Then the loop exits, and in one example you print the final values of these variables (102, 153, 51). In the other example, you don't print them out so you don't notice that you incremented them.

    Dave

  • Users who have thanked tracknut for this post:

    AGENT_P6 (09-05-2012)

  • #8
    New Coder
    Join Date
    Nov 2011
    Location
    ON, Canada
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tracknut View Post
    Code:
    while (doubles <= 100){
            printf("doubles tally: %d\n", doubles);
            printf("triples tally: %d\n", triples);
            printf("singles tally: %d\n", singles);
            doubles = doubles + 2;
            triples = triples + 3;
            singles = singles + 1;
    }
    This code will print out (on its last iteration through the loop) 100, 150, 50, but immediately after you print those values out, you then execute:
    Code:
            doubles = doubles + 2;
            triples = triples + 3;
            singles = singles + 1;
    Which sets doubles to 102, triples to 153 and singles to 51. Correct?

    Then the loop exits, and in one example you print the final values of these variables (102, 153, 51). In the other example, you don't print them out so you don't notice that you incremented them.

    Dave
    This explains it

    It makes sense now. The program always counted to the wrong numbers, but only one set of code had the final printfs to make me aware of that 51st loop.

    Told u i was new to this *facepalm*

    Im going to click the thanx button for all of u tho, as all of your posts helped me learn and understand more.

    sudo chmod / 000ps!


  •  

    Posting Permissions

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