chgBase - %way (assuming my baseChange function is...

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

View Full Document Right Arrow Icon
function num = chgBase(str,numin) %Given a number and the base in which it is in, write a function %chgBase that will convert the number to base 10. You must use %recursion for this problem. %Background Information: %Numbers you see everyday are in base 10. That is, if I have the number %435, I know it has the value 4*10^2 + 3*10^1 + 5*10^0. Another way of %thinking about it is that, when you count from 9 to 10, you must use %another digit to represent your number. Or, when I multiply a number by %10, I get a 0 as the rightmost digit (54 * 10 = 540). %By replacing 10 with N we can generalize the above for any base N. Thus %The number 1010 in binary (base 2) has the value %1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 10. %Hint: %The above definition tends to lend itself to an iterative solution. %You can, however, rewrite it in a recursive manner by thinking this
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %way (assuming my baseChange function is f(num,base)) %- If my input was f('3',4), what would my output be? % - If my input was f('31',4), what would my output be? What would % it be in terms of f('3',4)? % - If my input was f('312',4), what would my output be? What would % it be in terms of f('31',4)? % (etc.) %Constraints: %In order to receive credit for this problem, you MUST use recursion. %Also, you may not use dec2bin, bin2dec, base2dec, dec2base or any other %function which changes the base of a number for this problem. Problems %using these functions are subject to an automatic zero. if isempty(str) %terminating condition num = 0; % else number = str2num(str(1)); num = chgBase(str(2:end),numin) + number.*numin.^(length(str)-1); end end...
View Full Document

This note was uploaded on 10/21/2010 for the course CS 1371 taught by Professor Stallworth during the Spring '08 term at Georgia Tech.

Ask a homework question - tutors are online