Concept explainers
The Tower of Hanoi is a puzzle where n disks of different sizes are
stacked in ascending order on one rod and there are two other rods with no
disks on them. The objective is to move all disks from the first rod to the
third, such that:
- only one disk is moved at a time
- a larger disk can never be placed on top of a smaller one
Write a recursive function that outputs the sequence of steps needed to
solve the puzzle with n disks.
Write a test program in C++ that allows the user to input number of disks and
then uses your function to output the steps needed to solve the puzzle.
Hint: If you could move up n−1 of the disks from the first post to the
third post using the second post as a spare, the last disk could be moved from
the first post to the second post. Then by using the same technique you can
move the n−1 disks from the third post to the second post, using the first
disk as a spare. There! You have the puzzle solved. You only have to decide
what the nonrecursive case is, what the recursive case is, and when to output
instructions to move the disks.
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images
- Write the definition of a recursive function int simpleSqrt(int n) The function returns the integer square root of n, meaning the biggest integer whose square is less than or equal to n. You may assume that the function is always called with a nonnegative value for n. Use the following algorithm: If n is 0 then return 0. Otherwise, call the function recursively with n-1 as the argument to get a number t. Check whether or not t+1 squared is strictly greater than n. Based on that test, return the correct result. For example, a call to simpleSqrt(8) would recursively call simpleSqrt(7) and get back 2 as the answer. Then we would square (2+1) = 3 to get 9. Since 9 is bigger than 8, we know that 3 is too big, so return 2 in this case. On the other hand a call to simpleSqrt(9) would recursively call simpleSqrt(8) and get back 2 as the answer. Again we would square (2+1) = 3 to get back 9. So 3 is the correct return value in this case.arrow_forwardCS211 Non-recursive solution for Towers of Hanoi Using the algorithm discussed in class, write an iterative program to solve the Towers of Hanoi problem. The problem: You are given three towers a, b, and c. We start with n rings on tower a and we need to transfer them to tower b subject to the following restrictions: 1. We can only move one ring at a time, and 2. We may never put a larger numbered ring on top of a smaller numbered one. There are always 3 towers. Your program will prompt the user for the number of rings. Here is the algorithm. Definition: A ring is "available" if it is on the top of one of the towers. Definition: The "candidate" is the smallest available ring that has not been moved on the most recent move. The first candidate is ring 1. The Algorithm: 1. Find the candidate. 2. Move the candidate (right or left, depending if the number of rings is odd or even) to the closest tower on which it can be placed. Move "around the circle" if necessary. 3. If not done, go back…arrow_forwardWrite a recursive function that returns both the smallest and the largest element in an int array. Also, write a program to test your function.arrow_forward
- 1. Below, enter code to complete implementation of a recursive function sum allintegers(), which takes an input n and adds all integers preceding it, up to n: add all integers(n):arrow_forwardin C programing Write a recursive function that returns 1 if an array of size n is in sorted order and 0 otherwise. Note: If array a stores 3, 6, 7, 7, 12, then isSorted(a, 5) should return 1 . If array b stores 3, 4, 9, 8, then isSorted(b,4) should return 0.int isSorted(int *array, int n){arrow_forwardCreate a report of employees within the Research department showing First Name, Last Name, sorted alphabetically A-Z.arrow_forward
- Written explaination requiredarrow_forwardWrite a recursive function that displays a string reversely on the console using the following header: def reverseDisplay(value):For example, reverseDisplay("abcd") displays dcba. Write a test programthat prompts the user to enter a string and displays its reversal.arrow_forwardWrite a recursive function to sort an array of integers into ascending order using the following idea: the function must place the smallest element in the first position, then sort the rest of the array by a recursive call. This is a recursive version of the selection sort. (Note: You will probably want to call an auxiliary function that finds the index of the smallest item in the array. Make sure that the sorting function itself is recursive. Any auxiliary function that you use may be either recursive or iterative.) Embed your sort function in a driver program to test it. Turn in the entire program and the output.arrow_forward
- Write a recursive function that displays a string reversely on the console using the following header: void reverseDisplay(const string& s) For example, reverseDisplay("abcd") displays dcba. Write a test program that prompts the user to enter a string and displays its reversal.arrow_forward1. Given, nCr= n! (n-r)!r!" Write a function to compute nCr without recursion.arrow_forwardIndirect recursion is when function A calls function B, which in turn calls function A. is it true or false.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education