Question

In this lab, you may use MATLAB to implement the Recursion problems. Do not use a...

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”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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!\n',Guess,Iter);
   elseif Guess < Number % if guess is less
       fprintf('%d is too low.\n',Guess);
       recursive(Up_Lmt,Number,Iter);
   elseif Guess > Number % If guess is more
       fprintf('%d is too high.\n',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

>>

Add a comment
Know the answer?
Add Answer to:
In this lab, you may use MATLAB to implement the Recursion problems. Do not use a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • WRITE IN PYTHON: 2. Guess the Number The Goal: Similar to the first project, this project...

    WRITE IN PYTHON: 2. Guess the Number The Goal: Similar to the first project, this project also uses the random module in Python. The program will first randomly generate a number unknown to the user. The user needs to guess what that number is. (In other words, the user needs to be able to input information.) If the user’s guess is wrong, the program should return some sort of indication as to how wrong (e.g. The number is too high...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • In Matlab Create a single script (.m file) to solve these problems. Unless directed otherwise, use...

    In Matlab Create a single script (.m file) to solve these problems. Unless directed otherwise, use meaningful variable names for each variable; do not use the default variable ans to store your results. For this project, suppress your output with semi-colons (;). Each problem should be in a separate cell, using the cell mode feature of MATLAB. Problem 4 Video games are rather complicated to program, not least of which because of the graphics work that needs to be completed...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

  • USING JAVA, Use the binary search algorithm to create a game where you think of the...

    USING JAVA, Use the binary search algorithm to create a game where you think of the number and the computer guesses it. The computer should be able to guess any number between 1 and 500. When the computer chooses a number, you need to tell it if the guess is too high or too low. Have the program print out the number of guesses that it took to guess the number.

  • How can I do this program complete with input validation so that the computer prompts the...

    How can I do this program complete with input validation so that the computer prompts the user to enter a number 0 through 100 if he/she guesses anything lower than 0, a floating point number, or other characters? And later, prompts the user to enter yes or no if he/she enters anything else when the program prompts the user to play again? Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number...

  • In python how could I update my code to tell me the number of guesses it...

    In python how could I update my code to tell me the number of guesses it took my to get the correct number here is my working code so far import random number = random.randint(1,100) total_guess = 0 while total_guess<=100: guess = int(input("what is your guess?")) print (guess) total_guess = total_guess + 1 if guess<number: print("Too low") if guess>number: print("Too high") if guess==number: print("Correct!") break

  • In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game...

    In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game Hangman. In this game, a word is first randomly chosen. Initially, the letters in the word are displayed represented by "_”.   For example, if the random word is "yellow”, the game initially displays "_ _ _ _ _ _”. Then, each turn, the player guesses a single letter that has yet to be guessed. If the letter is in the secret word, then the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT