MATHLAB PROGRAM Your Task Your task is to implement a function with the followin
ID: 3923469 • Letter: M
Question
MATHLAB PROGRAM Your Task Your task is to implement a function with the following signature.- Argument
xis a nonnegative integer. - Argument
yis a positive integer. - Outputs
qandrsatisfy:x = q*y + rwith0 <= r < y.
- You must use repeated subtraction.
- You must use either a loop or recursion.
- You must not use any other functions or operations, including
rem,mod,flooror/.
- Argument
xis a nonnegative integer. - Argument
yis a positive integer. - Outputs
qandrsatisfy:x = q*y + rwith0 <= r < y.
- You must use repeated subtraction.
- You must use either a loop or recursion.
- You must not use any other functions or operations, including
rem,mod,flooror/.
- Argument
xis a nonnegative integer. - Argument
yis a positive integer. - Outputs
qandrsatisfy:x = q*y + rwith0 <= r < y.
- You must use repeated subtraction.
- You must use either a loop or recursion.
- You must not use any other functions or operations, including
rem,mod,flooror/.
Explanation / Answer
function [q, r] = divmod(x, y)
if x<y
error('Dividend should be greater than divisor')
end
if y==0
error('Division by zero is not allowed')
end
count=0;%Initialize count
a=0;%Initialize a
while(x>=y&&x>=0)
for i=y
x=x-i;%Repeated subtraction
count=count+1;
end
end
q=count;%Assign quotient
r=x;%Assign remainder
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.