Thread: Please help! short while loop tracking problem.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Please help! short while loop tracking problem.

    Hello all,
    I have always had problems with loop tracking problems, I seem to understand how the loop runs and when it ends and the general idea of how it works. However, when I take a programming test and I need to track the output of a short loop, I fail most of the time. Can someone please help me? or give me some strategy tips?

    The problem below is an example, I need to find the output of SUM without a computer, just using logic and a pencil and paper.

    This is what I got for my i, and sum;

    i
    0
    3
    9
    12

    sum:
    0
    3
    6
    9
    12

    So in my attempt: sum= 12 but the correct answer is sum=30.

    Code:
    int main () {
    	
    int sum=0, i=0;
    
    while (i <=12) {
    	   sum = sum +i;  // before this read sum=sum+1 which was a typo:/
    	   i= i+3;
    }
    printf("%d", sum);
    Last edited by matthayzon89; 04-22-2010 at 11:40 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    sum increase by 1 each time through the loop. i increases by 3 each time. Your sum values posted above are thus wrong and you are missing a 6 in your list of i values.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Darn!

    Sorry, there was a typo in the segment of code
    I edited it, the correct answer for sum=30.


    Can someone please help me understand whats going on? Can you basically solve this and think out loud so I can follow your thought pattern and learn from it?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by hk_mp5kpdw View Post
    sum increase by 1 each time through the loop.
    No, it does not.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by grumpy View Post
    No, it does not.
    It did... in the original version of his post. (He edited it)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Not so difficult to follow on paper (values below taken at end of each loop):
    Code:
    i    sum
    3    0
    6    3  (0  +  3)
    9    9  (3  +  6)
    12   18 (9  +  9)
    15   30 (18 + 12)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by hk_mp5kpdw View Post
    Not so difficult to follow on paper (values below taken at end of each loop):
    Code:
    i    sum
    3    0
    6    3  (0  +  3)
    9    9  (3  +  6)
    12   18 (9  +  9)
    15   30 (18 + 12)

    Thank you very much! That clarifies. I finally get it!

    Thanks again man, take care.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Also, two NOOB questions

    a) What is the format to randomize a number? for example a number between 15-100.

    would it be rand(15)%100?

    b)How come the answer to this is 15?

    13+20/(5+3%4)

    I get 4.125 for my answer:

    1)3%4=3
    2)5+3=8
    3)13+20=33
    4)33/8=4.125
    Last edited by matthayzon89; 04-22-2010 at 12:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. Replies: 8
    Last Post: 12-09-2008, 12:07 PM
  4. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 11:19 AM
  5. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 06:14 AM