Loop Question

This is a discussion on Loop Question within the C Programming forums, part of the General Programming Boards category; i'm trying to study for an up comming test and i'm getting stumped on a few review questions.. "Determine the ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    16

    Loop Question

    i'm trying to study for an up comming test and i'm getting stumped on a few review questions..
    "Determine the values of a loop counter and other values during the running of a program."

    Code:
       int x,y=0, value=24;
    	
       for( x=0, y=x+5;y<30;x++,y+=5)
        {
          value =value/y;
        }
    value of x | value of y | value of value |
    __________________________________________________ ___________
    _____________|_____________________|______________ ___________|
    _____________|_____________________|______________ ___________|
    _____________|_____________________|______________ ___________|

    what i see happening is x and y goes up by5 and every time value is dived by the growing y value un till y is just under under 30? so in 5 or 6 cycles? iam not to sure anymore

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    x starts at zero (x=0)
    y starts at 5 (y=x+5)

    x only goes up 1 per loop. (x++)
    y goes up 5 per loop (y+=5)

    value is divided (integer division) by y each loop.

    The loop stops when y gets to 30 (y<30)

    Todd

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    alright so then.....?

    X ------- y ------- value
    1 ------- 5 ------- 4.8
    2 ------- 10 ------- 2.4
    3 ------- 15 ------- 1.6
    4 ------- 20 ------- 1.2
    5 ------- 25 ------- 0.96
    6 ------- 30 ------- 0.8

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    or do i divide the new "value" by the new "Y"

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fr0zen View Post
    or do i divide the new "value" by the new "Y"
    Do you still have the old value of "value" any more? If so, where? If not, why not?

    The words "integer division" mean something -- you should look them up.

    You also need to double check your loop conditions.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    what?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "value" originally had the value of 24. The second time through the loop, what is the value of "value"? Is it still 24, or is it something else?

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    thats what i was asking, does it save the new value or is value always the same... but iam thinking it changes, and i added some coding so that the program prints the numbers in the loop and i got

    X ------- y ------- value
    0 ------- 5 ------- 4
    1 ------- 10 ------- 0
    2 ------- 15 ------- 0
    3 ------- 20 ------- 0
    4 ------- 25 ------- 0
    \ ------- \ ------- \

    but some how that doesnt make sense? shouldn't it be till y equals 30? and value shouldnt it round up to 5, not 4.. it shouldn't have to include decimal answers since its and integer not a float right?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fr0zen View Post
    thats what i was asking, does it save the new value or is value always the same... but iam thinking it changes,
    You shouldn't have to guess; you have the code. The code says value=value/y. What does that statement do?

    Quote Originally Posted by fr0zen View Post
    and i added some coding so that the program prints the numbers in the loop and i got

    X ------- y ------- value
    0 ------- 5 ------- 4
    1 ------- 10 ------- 0
    2 ------- 15 ------- 0
    3 ------- 20 ------- 0
    4 ------- 25 ------- 0
    \ ------- \ ------- \

    but some how that doesnt make sense? shouldn't it be till y equals 30? and value shouldnt it round up to 5, not 4.. it shouldn't have to include decimal answers since its and integer not a float right?
    You need to look up "integer division" in your textbook, since it's doesn't do what you think it does.

    And look at your loop condition. If y is 30, is the condition true or false?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 12:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21