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

  • Thread starter courtrigrad
  • Start date
  • Tags
    Method
In summary, two variables, a and b, are initialized to 0 and a for loop is used to increment and assign new values to them until the conditions a < 10 and b < 20 are no longer true. The final values of a and b are 12 and 16 respectively. The value of a is incremented by 2 after each iteration of the for loop, and the value of b is assigned the sum of b, a, and 1 for each iteration.
  • #1
courtrigrad
1,236
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
  • #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
 
  • #3
why would it be 0 + 0 + 1? wouldn't the values of a and b change?

Thanks
 
Last edited:
  • #4
post your final result... I'll check it with mine
 
  • #5
a = 12
b = 16
 
  • #6
I got the same thing
 
  • #7
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)
 
  • #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?
 
  • #9
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
 

What is the best method to solve a problem?

The best method to solve a problem depends on the specific problem being faced. However, some common problem-solving methods include brainstorming, trial and error, and using a systematic approach.

What is the first step in problem-solving?

The first step in problem-solving is to clearly define the problem. This involves identifying the root cause of the problem and understanding its impact.

How do I know if my problem-solving method is effective?

To determine if your problem-solving method is effective, you can evaluate the results. If the problem is solved and the solution is sustainable, then your method can be considered effective.

What should I do if my problem is too complex to solve?

If the problem is too complex to solve on your own, it is important to seek help from others. This could include collaborating with colleagues, consulting with experts, or conducting research to gather more information.

Can problem-solving skills be learned?

Yes, problem-solving skills can be learned and developed over time. By practicing different problem-solving methods and approaches, individuals can improve their problem-solving abilities and become more effective at finding solutions to various problems.

Similar threads

Replies
2
Views
382
  • Introductory Physics Homework Help
Replies
5
Views
224
Replies
20
Views
811
  • Introductory Physics Homework Help
Replies
1
Views
149
  • Introductory Physics Homework Help
Replies
2
Views
405
  • Introductory Physics Homework Help
Replies
13
Views
815
  • Introductory Physics Homework Help
Replies
5
Views
296
  • Introductory Physics Homework Help
Replies
21
Views
607
  • Introductory Physics Homework Help
Replies
3
Views
184
  • Introductory Physics Homework Help
Replies
34
Views
597
Back
Top