Question

Discussion - Recursion Create a Hanoi Tower program (You can use the worked example) Explain in...

Discussion - Recursion

Create a Hanoi Tower program (You can use the worked example) Explain in plain English how this program works and how it demonstrates the use of recursion Include a link to a repl.it file of your working program.

(c++)

Homework Answers

Answer #1

Tower of Hanoi is kind of mathematical puzzle where we can have 3 rods and N disks.Main objective of this puzzle is to move the entire stack to another rod, following 3 simple rules which are as follows:
1) At a time, we can move only one block.
2) Each move will consist of taking the topmost disk from one of the stacks, and placing it on top of another stack i.e. a disk can be moved only if it is the topmost disk on a stack.
3) larger sized disk cannnot be placed on the smaller sized disk.

take help from the below image, you will get the concept.

#include <bits/stdc++.h> 
using namespace std; 

void hanoiTower(int n, char from_stick, char to_stick, char aux_stick)  
{  
    if (n == 1)  
    {  
        cout << "Moving disk 1 from stick " << from_stick <<" to stick " << to_stick <<endl;  
        return;  
    }  
    hanoiTower(n - 1, from_stick, aux_stick, to_stick);  
    cout << "Moving disk " << n << " from stick " << from_stick << " to stick " << to_stick << endl;  
    hanoiTower(n - 1, aux_stick, to_stick, from_stick);  
}  
  
// Driver code 
int main()  
{  
   cout << "Enter total number of disks ";
   int k;
   cin >> k;
   
   hanoiTower(k, 'A', 'C', 'B'); //here we are asssuming A, B, C as three available three stick
  return 0;  
}  

our main aim here is to transfer all the disks from stick A to stick C.

Run this program and try to ananlize each line of the output, you will get the better picture

for any further doubt, comment below. i would be more then happy to help you out.

Know the answer?
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
Similar Questions
I have a recursive Tower of Hanoi program but don't know how to include the count...
I have a recursive Tower of Hanoi program but don't know how to include the count variable. Could you tell me how? Main.java import java.io.*; import java.util.List; public class Main { //int count = 0; /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole B without ever putting a larger disk on top of a smaller disk.*/...
How can you use recursion in lisp (clisp) to process infix. Example (Thisisinfix '(1 + 2...
How can you use recursion in lisp (clisp) to process infix. Example (Thisisinfix '(1 + 2 * ( 1 + 2)) = 7 (Thisisinfix '((1 + 2) * ( 1 + 2))) = 9 I need the general idea of how this works, not the full functional code. This also needs to be done not using any kind of variables such as let, or any sets, only recursion, however you can define other functions using defun if that helps. I...
In this exercise, you will create a program that displays the gross pay for one or...
In this exercise, you will create a program that displays the gross pay for one or more employees. If necessary, create a new project named Introductory16 Project, and save it in the Cpp8\Chap10 folder. The program should allow the user to enter the number of hours the employee worked and his or her hourly pay rate. Use a negative sentinel value to stop the program. Employees are paid at their regular pay rate for hours worked from 1 through 37....
create a program that prompts the user for a file name and then provides the user...
create a program that prompts the user for a file name and then provides the user with the number of word in the file. The program should then allow the user to repeatedly specify a number and show that word in the file. (Note: Do Not use an array.) please write in bsic comments so i can more easily understand A test plan for the program. (How would you know the program is actually working properly?) A shell program with...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
write a c++ program that can take an input file with string as for example "i...
write a c++ program that can take an input file with string as for example "i saw 5 teddy bears" and in a created output file create a new string to just change a digit to a word. To " I saw five teddy bears" . output should be located in an output file thank you
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Hello my name is Nartlada, i would like to know how can i create the histograms...
Hello my name is Nartlada, i would like to know how can i create the histograms from the number of the excel file. The question is asking "create histograms illustrating the distributions of the two temperature variables (T1 and T2). correctly the label the x and y axes and give your plot title. We are required to use R program that include the set working directory. I tried to drag the excel file to here or even drag just the...
Can you explain how proof by induction works and give an example?
Can you explain how proof by induction works and give an example?