How many times to divide by zero? Write a function called Divide which divides t
ID: 3881422 • Letter: H
Question
How many times to divide by zero? Write a function called Divide which divides two numbers, and keeps count of how many times the function was called asking to divide by zero. The function has two outputs: z, the division of the two numbers, and times, the number of times the function was called with a divide-by-zero request. In the case of a divide-by-zero request, the resultant should be set to not a number (NaN). Restriction: The function must make use of a persistent variable. Ex: >> clear all; x=1; y=1; [z,times] = Divide ( x, y) times = [l x#1 ; y=0 ; [z,tines] Divide ( x, y) >> = NaN times = [z,tines] Divide(x,y) >> x-2 ; howmany y-0; = z- NaN times =Explanation / Answer
% Matlab function which divides two numbers and keeps the count of how many times divide-by-zero was called using a persistent variable
function [z,times] = Divide(x,y)
persistent howmany;
% initialize the persistent variable
if isempty(howmany)
howmany = 0;
end
if y==0
howmany= howmany+1;
z = NaN;
else
z =x/y;
end
if howmany == 0
times = [];
else
times = howmany;
end
end
% end of function
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.