School

Toronto Metropolitan University *

*We aren’t endorsed by this school

Course

404

Subject

Computer Science

Date

Apr 3, 2024

Type

Pages

2

Report

Uploaded by AmbassadorPrairieDog4161

1. How will the first recursive call to towers() be invoked? Answer this question in the form: towers(x, y, z) where you give the actual values to the three parameters. 2. How many recursive calls to towers() will be made before this first recursive call actually returns to the initial invocation? 3. Once towers(5, 2, 3) has invoked its first recursive call to towers() and this invocation has returned, what will be printed to stdout? (i.e. What actual move will be made by towers(5, 2, 3) ?) 4. How will the second recursive call to towers() be invoked? Answer this question in the form: towers(x, y, z) where you give the actual values to the three parameters. 1. In the main program, towers() is a function. Therefore the recursive call to towers() will be invoked by placing values in the brackets. When printing out the program, the dotted lines represent the recursive calls. The recursive call is such that "n = n-1", "from" remains the same, and "spare = 6-from-dest". Since towers(5,2,3) is the initial invocation, the first recursive call will be invoked with the following: towers(4,2,1) 2. 5 recursive calls to towers() will be made before the first recursive call actually returns to the initial invocation. Once the recursion goes to towers(0,2,1), the recursion goes back into the previous calls. Therefore, 5 recursion calls will be made! 3. Once towers(5, 2, 3) has invoked its first recursive call to towers() and this invocation has returned, the output will be the following: 2 3 This represents the disc moving from Tower 2 to Tower 3. 4. In the main program, towers() is a function. Therefore the recursive call to towers() will be invoked by placing values in the brackets. When printing out the program, the dotted lines represent the recursive calls. The recursive call is such that "n = n-1", "from" remains the same, and "spare = 6-from-dest". Since towers(4,2,1) is the first recursive call, then the second recursive call will be invoked with the following: towers(3,2,3) Question: Suppose that towers(8, 1, 2) is invoked. How many lines will be printed to stdout? Note • You should note (or try to convince yourself) that the number of lines printed to stdout is precisely equal to the number of moves required to solve the problem. • You can use the theoretical analysis of the problem to determine the number of moves. • The towers command behaves somewhat differently than what has been described so far. In particular, if it is invoked with an argument, it will move the specified number of disks from Tower 1 to Tower 2. After printing the data in the terminal, there are 255 moves, therefore, there must be 255 lines printed to stdout. Also from the formula, Moves = 2 ^n -1, where n is the number of disk. In this case 'n' is 8. Moves = 2 ^8 -1 = 255 Therefore, 255 lines
Conclusion - I was able to successfully rectify and fix the bugs in the main code to print the algorithm for the recursive function. To correctly answer the questions, it was best to fix the code first so that I could better analyze the output. To ensure that the command lines were collected accurately, I used "argv[i]" syntax to initialize the inputs in their correct position and variables: n, from, and spare. Once, completed, I was able to test the output data and examine it to solve the questions above. References Romik, Dan. “Solving the Tower of Hanoi.” Futurum, 16 June 2021, futurumcareers.com/solving-the-tower-of-hanoi. R, Gourav. “Exit() Function in C.” Scaler Topics, Scaler Topics, 9 Nov. 2022, www.scaler.com/topics/exit-function-in-c/#:~:text=0%20or%20EXIT_SUCCESS%3A%20The %20statements,terminated%20abruptly%20with%20an%20error.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help