MATHLAB PROGRAM Your Task Your task is to implement a functi
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.
Here is the usual implementation for this function in Matlab: one using floating point numbers and floor:
However, this is how your function must be implemented:
- 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/.
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.
Here is the usual implementation for this function in Matlab: one using floating point numbers and floor:
However, this is how your function must be implemented:
- 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/.
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.
Here is the usual implementation for this function in Matlab: one using floating point numbers and floor:
However, this is how your function must be implemented:
- 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/.
Your task is to implement a function with the following signature.
Here is the usual implementation for this function in Matlab: one using floating point numbers and floor:
However, this is how your function must be implemented:
Solution
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

