Homework help

Homework help - Recursion You are given the following...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Recursion You are given the following function, with the following function calls. What is the value of each function call. If there would be an error, answer 'error'. FUNCTION function out= divide(x,y) if x==1 | y==1 out= 1; elseif mod(x,2)==0 | mod(y,3)==0 out = [round(x/y) divide(round(x/2), round(y/2))]; else out= [x+y divide(round(x/2), round(y/2))]; end end In the command window: A= divide(8,4) B= divide(4,8) C= divide(12,2) D= divide(1,100) E= divide(0,7) F= divide(24,9) Solution: A = [2 2 1] B = [1 1 1] C = [6 1] D = 1 E = [0 0 0 1] F = [3 2 2 5 1] FUNCTION function out= letter(string, number) if isempty(string) out= number; out= [number-length(string) letter(string(2:end), number)]; out= [number+length(find(string=='a')) letter(string(2:end),number)]; else out= [floor(number/2) letter(string(2:end), number+1)]; end end CALLS A= letter('dAz', 2) B= letter('A a', 5) C= letter('+++', 0) D= letter('AaAa', 3) E= letter('CS', -2) Solution: A = [-1 2 1 2] B = [6 2 5 6] C = [0 0 1 3]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
D = [5 0 4 2 3] E = [-2 -2 -2] Function Name : recurSum Input(1) : a vector Outputs(1) : the sum of that vector Note: Please do not use the sum function in your code as it will destroy the entire point. However you can use it to check your work after you are done writing the code. Description: Given a vector as input recursively sum each element of that vector and return the sum of all the elements of that vector as your output. Test Cases : >> recurSum([ 1 2 3]) ans -> 6 >> recurSum([ 12 231 234]) ans -> 477 Solution: function nSum = recurSum(vec) nSum = 0; if isempty(vec) nSum = nSum; elseif length(vec) == 1 nSum = vec(1); else nSum = nSum + recurSum(vec(2:end)); end Function name- recurSort Inputs (1)- a vector of numbers Outputs (1)- a vector of numbers sorted in descending order Function Description- Given a vector of numbers, use recursion to sort the numbers in descending order. *Do not use the sort function. Use recursion* Test Case out1= recurSort([-2 0 -5 6 2 2]) out1 -> [6 2 2 0 -2 -5] Solution: function sorted= recurSort(vector) if isempty(vector) sorted=[]; else [min1 pos]= min(vector); vector(pos)= []; sorted= [recurSort(vector) min1]; end end Function Name- letterReplace Inputs (1)- a string of letters Outputs (1)- the modified string Function Description- Given a string: 1) replace all the vowels (a,e,i,o,u) with the letters 'LO'.
Background image of page 2
2) replace the constants within b-m to the letter 'L'. 3) replace the constants within n-z to the letter 'S'. ** you may assume the input will be only lower case letters ** you MUST use recursion. Test Case out= letterReplace('tech') out2= letterReplace('abn') out -> 'SLOLL' out2 -> 'LOLS' Solution: function out= letterReplace(string) if isempty(string) out = []; elseif string(1) == 'a'|string(1)=='e'|string(1)=='i'|string(1)=='o'|string(1)=='u' out= ['LO' letterReplace(string(2:end))]; out= ['L' letterReplace(string(2:end))];
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

Homework help - Recursion You are given the following...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online