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...
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?
Discussion: Data and Statistics in Social Science a Create a social science research question that you...
Discussion: Data and Statistics in Social Science a Create a social science research question that you would like to learn more about. Note that everyone should create a unique question. An example might be, “Does alcohol abuse increase the rate of domestic violence?” Write down your question here. b What is the population of interest for your specific question and why? c What type of sampling method (name the method and define it) would you use to collect data to...
How can an instructor use performance-based assessment to verify psychomotor skill learning? Include an example of...
How can an instructor use performance-based assessment to verify psychomotor skill learning? Include an example of how you might complete a performance based assessment for a completely online course. Remember to use the vocabulary from the text in your discussion.