Question

2. Trace the following algorithm and determine the return value: Name : Algorithm AB Input :...

2. Trace the following algorithm and determine the return value: Name : Algorithm AB Input : None Output: An integer 1 a = 0 2 b = 20 3 while a < b : 4 a = a + 1 5 b = b - 2 6 end while 7 return a - b

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1 a=0

2 b=20

3 while a<b

4      a=a+1

5    b=b-2

6 end while

7 return a-b

Trace:

Initially

a=0

b=20

1st iteration

while(a<b) (0<20 which is true )

a=a+1 =0+1=1

b=b-2=20-2=18

2nd iteration

while(a<b) (1<18 which is true )

a=a+1 =1+1=2

b=b-2=18-2=16

3rd iteration

while(a<b) (2<16 which is true )

a=a+1 =2+1=3

b=b-2=16-2=14

4th iteration

while(a<b) (3<14 which is true )

a=a+1 =3+1=4

b=b-2=14-2=12

5th iteration

while(a<b) (4<12 which is true )

a=a+1 =4+1=5

b=b-2=12-2=10

6th iteration

while(a<b) (5<10 which is true )

a=a+1 =5+1=6

b=b-2=10-2=8

7th iteration

while(a<b) (6<8 which is true )

a=a+1 =6+1=7

b=b-2=8-2=6

8th iteration

while(a<b) (7<6 which is false loop fails)

end while loop

return a-b (7-6=1)

there fore output =1

Add a comment
Know the answer?
Add Answer to:
2. Trace the following algorithm and determine the return value: Name : Algorithm AB Input :...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT