0% found this document useful (0 votes)
103 views5 pages

WDWDDW

The algorithm converts decimal numbers to binary by repeatedly taking the modulo 2 of the number and appending the result to a string, then dividing the number by 2 until it reaches 0. It traces the steps of converting 6, 13, and 163 to binary. For each number, it initializes the string and number, then loops while the number is not 0 by taking the modulo 2 of the number and prepending it to the string before dividing the number by 2. Once the number reaches 0, it has the full binary conversion in the string.

Uploaded by

Stell aC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views5 pages

WDWDDW

The algorithm converts decimal numbers to binary by repeatedly taking the modulo 2 of the number and appending the result to a string, then dividing the number by 2 until it reaches 0. It traces the steps of converting 6, 13, and 163 to binary. For each number, it initializes the string and number, then loops while the number is not 0 by taking the modulo 2 of the number and prepending it to the string before dividing the number by 2. Once the number reaches 0, it has the full binary conversion in the string.

Uploaded by

Stell aC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Stella Connaughton

10.5.15

Tracing & Understanding Algorithms


1. What does the following algorithm do?
Convert a decimal number into binary.
2. TRACING ALGORITHIM
num=6
1. while x=/= 0 repeat the following
TRUE (num=6) (result=)
2.

appendLeft num mod 2 to result

(num=6)(result=0)
3. set num to num div 2
(num=3) (result=0)
4. end while loop
(num=3) (result=0)

(loop #2) num=3


1. while x=/= 0 repeat the following
TRUE (num=3)(result=0)
2.

appendLeft num mod 2 to result

(num=3)(result=10)
3. set num to num div 2
(num=1) (result=10)
4. end while loop
(num=1) (result=10)

(loop # 3) num=1
1. while x=/= 0 repeat the following
TRUE (num=1)(result=10)
2.

appendLeft num mod 2 to result

(num=1) (result=110)
3. set num to num div 2
(num=0) (result=110)
4. end while loop
(num=0, so no more loops)(result=110)

Stella Connaughton
10.5.15
110=3
Num=13
1. while x=/= 0 repeat the following
TRUE (num=13) (result=)
2.

appendLeft num mod 2 to result

(num=13) (result=1)
3. set num to num div 2
(num=6) (result=1)
4. end while loop
(num=6) (result=1)

(loop #2) num=6


1. while x=/= 0 repeat the following
TRUE (num=6) (result=1)
2.

appendLeft num mod 2 to result

(num=6) (result=01)
3. set num to num div 2
(num=3) (result=01)
4. end while loop
(num=3) (result=01)
(loop #3) num=3
1. while x=/= 0 repeat the following
TRUE
2.

(num=3) (result=101)

appendLeft num mod 2 to result

(num=3) (result=101)
3. set num to num div 2
(num=1) (result=101)
4. end while loop
(num=1) (result=101)
(loop # 4) num=1
1. while x=/= 0 repeat the following
TRUE (num=1) (result=101)
2.

appendLeft num mod 2 to result

Stella Connaughton
10.5.15
(num=1) (result=1101)
3. set num to num div 2
(num=0) (result=1101)
4. end while loop
(num=0, so no more loops) (result=1101)
1101= 13
Num=163
1. while x=/= 0 repeat the following
TRUE (num=163) (result=)
2.

appendLeft num mod 2 to result

(num=163) (result=1)
3. set num to num div 2
(num=81) (result=1)
4. end while loop
(num=81) (result=1)
(loop #2) num=81
1. while x=/= 0 repeat the following
TRUE (num=81) (result=1)
2.

appendLeft num mod 2 to result

(num=81) (result=11)
3. set num to num div 2
(num=40) (result=11)
4. end while loop
(num=40) (result=11)
(loop #3) num=40
1. while x=/= 0 repeat the following
TRUE (num=40) (result=11)
2.

appendLeft num mod 2 to result

(num=40) (result=011)
3. set num to num div 2
(num=20) (result=011)
4. end while loop
(num=20) (result=011)

Stella Connaughton
10.5.15
(loop #4) num=20
1. while x=/= 0 repeat the following
TRUE (num=20) (result=011)
2.

appendLeft num mod 2 to result

(num=20) (result=0011)
3. set num to num div 2
(num=10) (result=0011)
4. end while loop
(num=10) (result=0011)
(loop #5) num=10
1. while x=/= 0 repeat the following
TRUE (num=10) (result=0011)
2.

appendLeft num mod 2 to result

(num=10) (result=00011)
3. set num to num div 2
(num=5) (result=00011)
4. end while loop
(num=5) (result=00011)
(loop #6) num=5
1. while x=/= 0 repeat the following
TRUE (num=5) (result=00011)
2.

appendLeft num mod 2 to result

(num=5) (result=100011)
3. set num to num div 2
(num=2) (result=100011)
4. end while loop
(num=2) (result=100011)
(loop #7) num=2
1. while x=/= 0 repeat the following
TRUE (num=2) (result=100011)
2.

appendLeft num mod 2 to result

(num=2) (result=0100011)
3. set num to num div 2

Stella Connaughton
10.5.15
(num=1) (result=0100011)
4. end while loop
(num=1) (result=0100011)
(loop #8) num=1
1. while x=/= 0 repeat the following
TRUE (num=1) (result=0100011)
2.

appendLeft num mod 2 to result

(num=1) (result=0100011)
3. set num to num div 2
(num=0) (result=10100011)
4. end while loop
(num=0, so no more loops) (result=10100011)
10100011=163

You might also like