-2

I am testing some codes related to recursion function. I saw the recursion method for towers of hanoi problem but I am very confused, two recursive functions are using in if condition. The understanding of the program till now is first it set n to zero and after this I dont know how it increases the n and how it knows which recursive function it should call first. I am weak in understanding of recursive function,please help me in understanding the working of recursive function of given program.

// Recursive Towers of Hanoi

#include <iostream>

using namespace std;

void towersOfHanoi(int n, int x, int y, int z)
{// Move the top n disks from tower x to tower y.
 // Use tower z for intermediate storage.
    if (n > 0) // Base Case
    {
        towersOfHanoi(n - 1, x, z, y);
        cout << "Move top disk from tower " << x << " to top of tower " << y << endl;
        towersOfHanoi(n - 1, z, y, x);

    }

} // Recursive Procedure

void main(void)
{
    cout << "Moves for a three disk problem are" << endl;
    towersOfHanoi(3, 1, 2, 3);
    system("pause");
}

2 Answers2

0

i just read your question. Maybe your should know this. A few rules to be followed for Tower of Hanoi are − Only one disk can be moved among the towers at any given time. Only the "top" disk can be removed. No large disk can sit over a small disk. So we need to move the top n-1 disks from x to z, then we can move the n disk from x to y. finally move the top n-1disks from z to y.

yupe
  • 117
  • 1
  • 10
0

Your understanding is pretty wrong.

It first set n to zero. Actually it first sets n to 3

towersOfHanoi(3, 1, 2, 3); - this sets n to 3 (first argument)

I don't know how it increases n. Actually it decreases n,

towersOfHanoi(n - 1, z, y, x); - this sets n to n - 1 (first argument again).

how it knows which recursive function it should call first?. It calls the first one first, same as any other code.

    towersOfHanoi(n - 1, x, z, y); // THIS ONE FIRST
    cout << "Move top disk from tower " << x << " to top of tower " << y << endl;
    towersOfHanoi(n - 1, z, y, x); // THIS ONE SECOND

Recursive functions work exactly the same as any other functions. There is no magic.

john
  • 85,011
  • 4
  • 57
  • 81
  • when n wiill sent in void towersOfHanoi(int n, int x, int y, int z) it will b 3 I know, but after calling three times this function towersOfHanoi(n - 1, x, z, y); n will be 0 right? so after this how n increases, This wasmy question, I know how it is working but I dont know how n is increment – M. Tayyab Ali Jul 22 '19 at 00:38
  • @Tayyab Yes, it's initially 3, and yes after calling 3 times it will be zero. **BUT** when you return from the function that value of `n` goes back to what it was before. Remember that there isn't one `n` variable. Every call to `towersOfHanoi` has its **own copy** of the `n` variable each with a different value. Again this is not something that's special about recursive functions. Any function call at all gets it's own copy of all it's parameters. – john Jul 22 '19 at 05:42