Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Best MEthod to Solve Problem

  1. Jan 20, 2005 #1
    Hello all

    Given the code:

    Code (Text):
     
    int a = 0;
    int b = 0;

    for(a = 0; a < 10 && b < 20; a = a+2) {
            a = a + b;
            b = b + a + 1;
    }

     
    Find a and b. SO this is what I did:

    0 = 0 + 2 (value of a)
    2 = 2 + 0 + 1 (value of b)

    Now a = 2 and b = 3, and we work from those values until we get to the constraints. However I am not sure whether this is correct?

    Any help and tips is appreciated!

    Thanks
     
  2. jcsd
  3. Jan 20, 2005 #2
    I see a mistake... upon your first execution of the for loop,, you forgot to execute
    b = b + a +1
    => b = 0 + 0 + 1
    so b = 1 after the first iteration
     
  4. Jan 20, 2005 #3
    why would it be 0 + 0 + 1? wouldn't the values of a and b change?

    Thanks
     
    Last edited: Jan 20, 2005
  5. Jan 20, 2005 #4
    post your final result... I'll check it with mine
     
  6. Jan 20, 2005 #5
    a = 12
    b = 16
     
  7. Jan 20, 2005 #6
    I got the same thing
     
  8. Jan 20, 2005 #7

    DaveC426913

    User Avatar
    Gold Member

    That is not what I got.

    Code (Text):

    .                      a  b
    . initial:             0  0
    . after 1st iteration: 0  1
    . after 2nd iteration: 5  5
     
    (don't forget that the loop incerments a by 2)

    (I ran the code in JavaScript, and lo! I got the same answers as by hand. pat pat pat)
     
  9. Jan 20, 2005 #8
    would it be a = 10 b = 16?

    0 = 0 + 0 = 0
    0 = 0 + 0 + 1 = 1

    2 = 2 + 1 = 3
    1+ 3 + 1 = 5;

    5 = 5 + 5 = 10
    5 = 5 + 10 + 1 = 16?
     
  10. Jan 20, 2005 #9
    Keep going.. it doesnt end after the second iteration...
    post your code.. cause something is wrong
     
  11. Jan 20, 2005 #10
    No... that would be the values after the third iteration... but it then increments a by 2 before checking the conditional statements
    you had the correct answer
     
  12. Jan 20, 2005 #11
    Why am I getting a = 10, b = 16 instead of a = 12 b = 16?

    Thanks
     
  13. Jan 20, 2005 #12
    yes... and they do... everytime you come across an assignment (=) statement the variable to the left changes.

    when you first come to the for loop both a and b are 0.
    the for loop first initializes a to be 0 (it only does this once)... then checks the conditional statements (a<10 && b<20 ).
    As long as these statements return a value of true. it executes the body of the for loop.
    Since 0<10 and 0<20 it goes inside the body of the for loop and executes the first line of code.
    The first line of the for loop is basically assigning to a the value of 0 + 0... so a remains zero.
    The second line then assigns to b the value of b + the value of a + 1...
    a and b are both still 0 so this looks like
    b = 0 + 0 + 1.
    Once the body of the for loop has been executed, it goes to the incremental stage and increments a by 2.
    then goes back to check the conditional statements skipping the initialization. This process is repeated until one or both of the conditions returns a value of false, in which the program exits the for loop.
    Does this make sense?
     
  14. Jan 20, 2005 #13
    The last statement you execute should be a = a +2;
    do you see why?
     
  15. Jan 20, 2005 #14
    well if you go back up, 10 < 10 is false. so it should not increment a by 2. H.. I am not sure why it increments by 2.
     
  16. Jan 20, 2005 #15
    i think i got it now. it increments immediately after its down iteration.

    thanks
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook