Division (repetitive substraction)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Division (repetitive substraction)

alldp19
Slide number 13, Chapter:12

// Returns the integer part of x / y,
// where x ≥ 0 and y > 0
// Strategy: repetitive subtraction
divide (x , y) :
div = 0
rem = x
while rem ≤ x
rem = rem – y
div = div + 1
return div

I think the while condition here should be rem>=y instead of rem<=x. Please, correct me if I am wrong.
If we divide 20/6
rem = 20-6=14
rem = 14-6 = 8
rem = 8-6 = 2
if rem<=x I don't understand how it works
if rem>=y the the condition will be false and we get the answer.
Reply | Threaded
Open this post in threaded view
|

Re: Division (repetitive substraction)

WBahn
Administrator
Looks like you are correct. An error in the slides. Are those the slides from the website or from the Coursera course?
Reply | Threaded
Open this post in threaded view
|

Re: Division (repetitive substraction)

alldp19
The slides I checked are from the website but I guess they are same for the Coursera course too.