# Division by repeated subtraction with remainder

3 visualizzazioni (ultimi 30 giorni)
Eric Brown il 24 Apr 2022
Risposto: Dyuman Joshi il 24 Apr 2022
The code I'm trying to build needs to compute the division of two non-zero numbers and return the quotient and remainder. When I input x = 4 and y = 3, I was getting z = [ 1 1]. While I was trying to get the remainder to be a fraction, I don't know what I did to it but, now it wont even return an output.
function z = DbyS(x,y)
q = 0;
if x ~= 0
if x < 0
x =0-x;
r = x;
while r > y
r = x - y;
q = q + 1;
end
z = [q r];
else
r = x;
while r > y
r = x - y;
q = q + 1;
end
z = [q r];
end
else
z=0;
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Image Analyst il 24 Apr 2022
The code DOES return an output. It returns [0, 3]. See
z = DbyS(3, 4)
z = 1×2
0 3
function z = DbyS(x,y)
q = 0;
if x ~= 0
if x < 0
x =0-x;
r = x;
while r > y
r = x - y;
q = q + 1;
end
z = [q r];
else
r = x;
while r > y
r = x - y;
q = q + 1;
end
z = [q r];
end
else
z=0;
end
end
I think you just need to go over the logic.

Accedi per commentare.

### Risposta accettata

Voss il 24 Apr 2022
DbyS(4,3)
ans = 1×2
1 1
Consider that the following lines of code:
r = x;
while r > y
r = x - y;
q = q + 1;
end
z = [q r];
appear two times, once for x < 0 and once for x > 0 (with the one for x < 0 being preceded by x =0-x;) That means those lines are executed in either case (x < 0 or x > 0), so you can separate them from the if x < 0 ... else ... end logic:
function z = DbyS(x,y)
q = 0;
if x ~= 0
if x < 0
x =0-x;
end
r = x;
while r > y
r = x - y;
q = q + 1;
end
z = [q r];
else
z=0;
end
end
Now consider what happens if x > 2*y. Say, for instance, x = 10 and y = 3:
r = x; % intialize r = 10
while r > y % 10 > 3 is true -> enter while loop
r = x - y; % r = 10-3 = 7
q = q + 1; % increment q
at that point the while condition will be checked again, this time with r = 7. It finds that the condition r > y (i.e., 7 > 3) is true, so the loop iterates again. The first line inside the loop is to update r:
r = x - y; % r = 10-3 = 7
but the update is based on x, which hasn't changed since the first time through, so you just get the same value of r (10-3=7) again. Then the while condition is checked again, nothing's changed, the loop iterates again, etc., etc., ad infinitum. The while loop's just going to run forever in this case.
Hopefully by considering that case, it's clear that the updated value of r needs to be based on the current value of r, not the initial value of r, which is x. That is, that line should be:
r = r - y; % r = 10-3 = 7
(just as q is incremented by 1 on the next line, r is decremented by y on this line). So that block should be this:
r = x;
while r > y
r = r - y;
q = q + 1;
end
z = [q r];
If you make that change the code should return a result in all cases where y > 0.
In case y < 0 (if your code has to support this case), you'll have to do something else because subtracting y from r will only increase the value of r. I'll leave that to you to think about, and also consider what should happen when y = 0 (something divided by zero).
And what about when x = 0 (zero divided by something)? As it is now the code would return a scalar z = 0 in that case. Maybe that's what you want, or maybe it should also return a quotient and remainder in that case (i.e., a 1x2 vector).
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Dyuman Joshi il 24 Apr 2022
Your while loop is stuck in an infinite loop since it defines r as the same value every time. I have modified it
DbyS(17,4)
ans = 1×2
4 1
DbyS(-33,5)
ans = 1×2
6 3
function z = DbyS(x,y)
q = 0;
if x ~= 0
if x < 0
x =0-x;
r=x;
while r > y
r = r - y;
q = q + 1;
end
z = [q r];
else
r=x;
while r > y
r = r - y;
q = q + 1;
end
z = [q r];
end
else
z=0;
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by