In this lab, you may use MATLAB to implement the Recursion problems. Do not use
ID: 3796279 • Letter: I
Question
In this lab, you may use MATLAB to implement the Recursion problems. Do not use a for or a while loop to implement a recursive function.
3. Recursion: Sort Game (One of the reasons why we sort) (25 points) Write a recursive function that guesses a number between 0 and x (x being an input number to the function). If you guess too high, it tells you that and continues. If you guess too low, it tells you that and continues. If you guess the number, it tells you how many guesses you took and stops. So your output might look something like this:
Guess a num between 0 and 100: 33 33 is too high.
1 Guess a num between 0 and 100: 12 12 is too high.
Guess a num between 0 and 100: 6 6 is too low
Guess a num between 0 and 100: 8 8 is too low
Guess a num between 0 and 100: 9 9 is too low.
Guess a num between 0 and 100: 11 11 is too high.
Guess a num between 0 and 100: 10 you guessed 10 in 6 guesses!
Note: write two functions. The first takes as an input parameter a number, and generates a random number between 0 and that input number. It then calls the second, recursive function with about 3 input parameters, including the first function’s input parameter (in the above example, that was the number 100) and the random number (in the above example, that was 10). It is in the recursive function that the user is asked to guess a number. Try testing the function using 8,16,32,64 as the upper limit of the random number (or, in other terms, as input into the first function). This is one of the reasons we sort sets (lists) of numbers so often - it is incredibly more efficient to find a number if we’re searching through a sorted set of numbers than it is to find a number while searching through a randomly ordered set of numbers.
4. Recursion: Cipher (25 points) Write the comments with input, output, and test cases for a recursive function that creates a ”gibberish” language. So if a letter in a word is a vowel, it is replaced with ”ithag”, then the vowel, whereas if the letter is a consonant, it is just included normally. •
For instance, the word ”dog” would be ”dithagog” • ”math” would be ”mithagath”
Explanation / Answer
Matlab Question for Problem 3)
function GuessTheNumber()
Up_Lmt = input('Enter the upper limit of the random number: ');% Enter the limit
Number = floor(Up_Lmt*rand(1));% Generate random number
recursive(Up_Lmt,Number,0); % Call the funcrion
end
function recursive(Up_Lmt,Number,Iter) % Recursive function
Iter = Iter +1; % increse the number of Recursions
Guess = input(['Guess a num between 0 and ',num2str(Up_Lmt),': ']);% Input the guess
if Guess == Number % if guess is correst
fprintf('you guessed %d in %d guesses! ',Guess,Iter);
elseif Guess < Number % if guess is less
fprintf('%d is too low. ',Guess);
recursive(Up_Lmt,Number,Iter);
elseif Guess > Number % If guess is more
fprintf('%d is too high. ',Guess);
recursive(Up_Lmt,Number,Iter);
end
end
OUTPUT
>> GuessTheNumber
Enter the upper limit of the random number: 8
Guess a num between 0 and 8: 5
5 is too high.
Guess a num between 0 and 8: 2
2 is too high.
Guess a num between 0 and 8: 1
1 is too high.
Guess a num between 0 and 8: 0
you guessed 0 in 4 guesses!
>> GuessTheNumber
Enter the upper limit of the random number: 16
Guess a num between 0 and 16: 8
8 is too high.
Guess a num between 0 and 16: 4
4 is too low.
Guess a num between 0 and 16: 6
6 is too low.
Guess a num between 0 and 16: 5
5 is too low.
Guess a num between 0 and 16: 7
you guessed 7 in 5 guesses!
>> GuessTheNumber
Enter the upper limit of the random number: 32
Guess a num between 0 and 32: 16
16 is too high.
Guess a num between 0 and 32: 8
8 is too low.
Guess a num between 0 and 32: 12
you guessed 12 in 3 guesses!
>>
Matlab function for the problem 4)
function out = gibberish(in)
out = ''; % variable
if isempty(in) == 0 % Checking vowel or not
if(in(1) == 'a'||...
in(1) == 'e'||...
in(1) == 'i'||...
in(1) == 'o'||...
in(1) == 'u')
% creating the new word with ithag
out = [out,'ithag',in(1),gibberish(in(2:end))];
else
% not doing anything to the word
out = [out,in(1),gibberish(in(2:end))];
end
end
end
Testing the function
>> out = gibberish('dog')
out =
dithagog
>> out = gibberish('math')
out =
mithagath
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.