Invitation to Computer Science 8th Edition

Published by Cengage Learning
ISBN 10: 1337561916
ISBN 13: 978-1-33756-191-4

Chapter 1 - Challenge Work - Page 41: 1

Answer

Given: Two positive m-digit numbers $a_{m} a_{m-1} \ldots a_{1}$ and $b_{m} b_{m-1} \ldots b_{1}$ where $a_{m} a_{m-1} \ldots a_{1} \geq b_{m} b_{m-1} \ldots b_{1}$ Wanted: $c_{m} c_{m-1} \ldots c_{1}$ which is equal to $a_{m} a_{m-1} \ldots a_{1}-b_{m} b_{m-1} \ldots b_{1}$ Step 1 Set the value of borrow to 0 Step 2 Set the value of $i$ to 1 Step 3 While the $i \leq(m)$ perform Steps 4 through 7 Step 4 Set value of $a_{i}$ to $a_{i}-$borrow Step 5 If $a_{i} \geq b_{i}$ then $c_{i}=a_{i}-b_{i}$ and set borrow $=0$ Step 6 Otherwise, $c_{i}=10-\left(b_{i}-a_{i}\right)$ and set borrow $=1$ Step 7 Add 1 to $i$ Step 8 Print the answer $c_{m} c_{m-1} \ldots c_{1}$ Step 9 Stop

Work Step by Step

Given: Two positive m-digit numbers $a_{m} a_{m-1} \ldots a_{1}$ and $b_{m} b_{m-1} \ldots b_{1}$ where $a_{m} a_{m-1} \ldots a_{1} \geq b_{m} b_{m-1} \ldots b_{1}$ Wanted: $c_{m} c_{m-1} \ldots c_{1}$ which is equal to $a_{m} a_{m-1} \ldots a_{1}-b_{m} b_{m-1} \ldots b_{1}$ Step 1 Set the value of borrow to 0 Step 2 Set the value of $i$ to 1 Step 3 While the $i \leq(m)$ perform Steps 4 through 7 Step 4 Set value of $a_{i}$ to $a_{i}-$borrow Step 5 If $a_{i} \geq b_{i}$ then $c_{i}=a_{i}-b_{i}$ and set borrow $=0$ Step 6 Otherwise, $c_{i}=10-\left(b_{i}-a_{i}\right)$ and set borrow $=1$ Step 7 Add 1 to $i$ Step 8 Print the answer $c_{m} c_{m-1} \ldots c_{1}$ Step 9 Stop
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.