Thursday, November 5, 2009

An Algorithm For Dividing Integers (Returning the Quotient and the Remainder) in MATLAB

function quotient = intDivision(dividend, divisor)
if divisor > dividend
quotient = [0, dividend];
return;
end
if divisor == 0
quotient = [0, 0];
return;
end
counter = 0;
tempVal = 0;
while tempVal <= dividend
tempVal = tempVal + divisor;
counter = counter + 1;
end
if tempVal == dividend
quotient = [counter, 0];
return;
end
tempVal = tempVal - divisor;
quotient = [counter - 1, dividend - tempVal];

No comments:

Post a Comment