What are the values of a and b after the code executes?

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Method
AI Thread Summary
The code execution results in the final values of a and b being a = 12 and b = 16 after the loop completes. The loop increments a by 2 and updates b based on the current values of a and b during each iteration. Initial calculations were confused due to the order of operations and the effect of the loop's increment. The discussion clarified that the loop's conditions and increments must be carefully followed to avoid errors. Ultimately, the correct final values were confirmed through both manual calculations and code execution.
courtrigrad
Messages
1,236
Reaction score
2
Hello all

Given the code:

Code:
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
 
Physics news on Phys.org
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
 
why would it be 0 + 0 + 1? wouldn't the values of a and b change?

Thanks
 
Last edited:
post your final result... I'll check it with mine
 
a = 12
b = 16
 
I got the same thing
 
That is not what I got.

Code:
.                      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)
 
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?
 
DaveC426913 said:
That is not what I got.

Code:
.                      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)
Keep going.. it doesn't end after the second iteration...
post your code.. cause something is wrong
 
  • #10
courtrigrad said:
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?
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
 
  • #11
Why am I getting a = 10, b = 16 instead of a = 12 b = 16?

Thanks
 
  • #12
courtrigrad said:
why would it be 0 + 0 + 1? wouldn't the values of a and b change?

Thanks
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
courtrigrad said:
Why am I getting a = 10, b = 16 instead of a = 12 b = 16?

Thanks
The last statement you execute should be a = a +2;
do you see why?
 
  • #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.
 
  • #15
i think i got it now. it increments immediately after its down iteration.

thanks
 
Back
Top