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

MATLAB function M-files HIGH POINTS Write a MATLAB function M-file named isdiv9

ID: 3553966 • Letter: M

Question

MATLAB function M-files HIGH POINTS

Write a MATLAB function M-file named isdiv9 to receive a row array of numbers as input, determine which are divisible by 9, then create and return a row array with logical values as output: 1 if the number is divisible by 9, and 0 if it is not. However! This is to be done without using division! Instead, implement an algorithm for the divisible-by-9 rule: if the sum of all the digits in a number is divisible by 9, then the number itself is also divisible by 9. Restrictions on input: The input must be a scalar or in a row array All numbers in the input array must less than 1 10 10 All numbers in the input array must be integers. The M-file should give an error message, and halt the program, should any of these occur You may not use division, nor any MATLAB built-in function that can be used to determine divisibility (such as ren). Your grade will be zero. Hint: Built-in functions num2str and str2num will be helpful. They are mentioned in Chapter 7 of the textbook & in MATLAB help Examples: Write a MATLAB function M-file named Div9Finder that has one input argument and one output argument. The restrictions on input are the same as for isdiv9, but do not duplicate the error code in this function. The output contains all of the numbers from the input array that are divisible by 9. This function must call (use) isdiv9. Examples:

Explanation / Answer

CODE


Part 1)

%%

function [X] = isdiv9(Input)


if size(Input,1) > 1

error('Input must be a scalar or in a row array')

elseif(max(Input)>1*10^10)

error('All numbers in the input array must be less than 1 x 10^10');

elseif any(~mod(Input,1)==0)

error('All numbers in the input array must be integers')

end


for i=1:numel(Input)

a = abs(Input(i));

while a > 9

sum(i)=0;

a = num2str(a);

for j = 1:numel(a)

sum(i) = sum(i) + str2num(a(j));

end

a = sum(i);

end

if a==0 || a==9

X(i) = 1;

else X(i) = 0;

end

end

%%


Part 2)

%%

function [Y] = Div9Finder(Input)

Out = isdiv9(Input);

Y = Input(find(Out));

%%