{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chgBase - What would it be in terms of f'3,4 If my input...

Info icon This 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 %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?
Image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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; %the number will equal 0 when there is an empty string else number = str2num(str(1)); %converts the first element of the string to a number so it can me %operated on mathematically num = chgBase(str(2:end),numin) + number.*numin.^(length(str)-1); %clones the original function from the second position to the end, with %the same base, and adds the number from the first position in the %string multiplied by the base number raised to the length-1 power end end...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern