Ask Question
17 September, 21:16

Prepare a algorithm visualization for the Tower of Hanoi when 4 disks are to be moved from spindle #1 to spindle #3.

+5
Answers (1)
  1. 18 September, 00:35
    0
    follwing is the code for Tower of Hanoi for n disks.

    #include

    using namespace std;

    void towofhan (int n, char source, char aux, char dest) / /function for tower of hanoi ...

    {

    if (n<0)

    return;

    if (n==1) / /base case.

    {

    cout<<"Move disk 1 from "<
    return;

    }

    towofhan (n-1, source, dest, aux); //recursive call.

    cout<<"move disk "<
    towofhan (n-1, aux, source, dest); //recursive call.

    }

    int main () {

    int n=4;

    towofhan (n,'1','2','3'); //function call.

    return 0;

    }

    Explanation:

    If there is only 1 disk then we have to move the disk from source to destination.

    Then after that we will apply recursion to solve the problem.

    We have to work on only nth disk else will be done by the recursion.

    First call recursion to move n-1 disks from source to auxiliary.

    Then move nth disk from source to destination spindle.

    Now move n-1 disks that are on the auxiliary spindle to destination spindle.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Prepare a algorithm visualization for the Tower of Hanoi when 4 disks are to be moved from spindle #1 to spindle #3. ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers