# digits - % size, floor, round, mod, rem, and abs. %...

This preview shows page 1. Sign up to view the full content.

% 10/21/2009 1:40 PM % Function Name: digits % Inputs: 1. double - an integer % Outputs: 1. double array - a vector of digits % % Function Description: % Write a function that takes in an integer and returns a vector % containing all of the digits in the order they appear in the input. % Your function should work for all integers. Simply ignore the negative % sign if negative. % % You must use recursion to receive credit. You may find it helpful to % start conceptually with the recursive call and to work your way % backwards to the terminating condition. Remember to use a wrapper % function to handle undesirable inputs to your recursive function if % necessary. Do NOT use any other built-in functions besides length,
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: % size, floor, round, mod, rem, and abs. % function vec = digits(num) num = abs(num); num = floor(num); vec = split(num); end function vec = split(num) temp = num2str(num); if isempty(temp) vec = ; elseif length(temp)==1 vec = num; elseif temp(2)=='0' % takes care of case of multiple zeros vec = [str2num(temp(1)) getzeros(temp(2:end)) split(str2num(temp(2:end)))]; else vec = [str2num(temp(1)) split(str2num(temp(2:end)))]; end end e function vec = getzeros(strnum) if isempty(strnum) || strnum(1)~='0' vec = ; elseif length(strnum)==1 &amp;&amp; strnum(end)=='0' vec = ; else vec = [0 getzeros(strnum(2:end))]; end end...
View Full Document

Ask a homework question - tutors are online