# Best MEthod to Solve Problem

1. Jan 20, 2005

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. Jan 20, 2005

### MathStudent

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

3. Jan 20, 2005

why would it be 0 + 0 + 1? wouldn't the values of a and b change?

Thanks

Last edited: Jan 20, 2005
4. Jan 20, 2005

### MathStudent

post your final result... I'll check it with mine

5. Jan 20, 2005

a = 12
b = 16

6. Jan 20, 2005

### MathStudent

I got the same thing

7. Jan 20, 2005

### DaveC426913

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)

8. Jan 20, 2005

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?

9. Jan 20, 2005

### MathStudent

Keep going.. it doesnt end after the second iteration...
post your code.. cause something is wrong

10. Jan 20, 2005

### MathStudent

No... that would be the values after the third iteration... but it then increments a by 2 before checking the conditional statements

11. Jan 20, 2005

Why am I getting a = 10, b = 16 instead of a = 12 b = 16?

Thanks

12. Jan 20, 2005

### MathStudent

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?

13. Jan 20, 2005

### MathStudent

The last statement you execute should be a = a +2;
do you see why?

14. Jan 20, 2005

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.

15. Jan 20, 2005