Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.
  1. Argument x is a nonnegative integer.
  2. Argument y is a positive integer.
  3. Outputs q and r satisfy: x = q*y + rwith 0 <= 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:
  1. You must use repeated subtraction.
  2. You must use either a loop or recursion.
  3. You must not use any other functions or operations, including rem, mod, floor or /.
MATHLAB PROGRAM Your Task Your task is to implement a function with the following signature.
  1. Argument x is a nonnegative integer.
  2. Argument y is a positive integer.
  3. Outputs q and r satisfy: x = q*y + rwith 0 <= 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:
  1. You must use repeated subtraction.
  2. You must use either a loop or recursion.
  3. You must not use any other functions or operations, including rem, mod, floor or /.
Your Task Your task is to implement a function with the following signature.
  1. Argument x is a nonnegative integer.
  2. Argument y is a positive integer.
  3. Outputs q and r satisfy: x = q*y + rwith 0 <= 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:
  1. You must use repeated subtraction.
  2. You must use either a loop or recursion.
  3. You must not use any other functions or operations, including rem, mod, floor or /.
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:

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