Write a MATLAB function DivBy11List that takes an array of p
Write a MATLAB function DivBy11List that takes an array of positive integers as input. The output argument is an array that contains the numbers from the input array that are divisible by 11. Use function isDiv11 to determine divisibility.
Examples:
>> DivBy11List([44 99 101 68 2519])
ans = 44 99 2519
>> DivBy11List([92 1035 85])
ans = [ ]
Code Requirements:
• The function M file must use the function isDiv11. No credit will be given if divisibility is determined any other way
• The function M-file must contain isDiv11 as a subfunction
• No vectorization is allowed, nor use of any functions, such as sum, any, or find, that work on an entire array.
Input Restrictions & special cases:
• No extra code is required. Leave the error statements as is in isDiv11
• The function should return an empty array
Solution
Matlab Code
function [ ans ] = Divby11List( A )
ans = [];
k=1;
function [out] = isDiv11(x)
if(mod(x,11)==0)
out=1;
else
out=0;
end
end
for i=1:length(A)
if isDiv11(A(i))==1
ans(k)=A(i);
k=k+1;
end
end
end
Sample Output
>> A = [44 99 101 68 2519];
>> Divby11List(A)
ans =
44 99 2519
