Understanding c Code Tower of Hanoi using Recursion

0 votes

I'm studying about recursion in C++, but the following C++ code used to solve the Tower of Hanoi issue has me baffled.

void Hanoi(int m, string start, string middle, string end){
    cout << "m is equal to: " << m << endl;
    if(m == 1){
        cout << "Move Disc " << " from " << start << "  to " << end << endl;
    }
    else{
        Hanoi(m-1,start,end,middle);
        cout << "Move disc " << m << " from " << start << " to " << end << endl;
        Hanoi(m-1,middle,start,end);
    }
}
int main(){
    int discs = 3;
    Hanoi(discs, "start","middle","end");

}

the output of the code is as follows:

m is equal to: 3
m is equal to: 2
m is equal to: 1
Move Disc  from start  to end
Move disc 2 from start to middle
m is equal to: 1
Move Disc  from end  to middle
Move disc 3 from start to end
m is equal to: 2
m is equal to: 1
Move Disc  from middle  to start
Move disc 2 from middle to end
m is equal to: 1
Move Disc  from start  to end

My general issue is that I don't understand how the recursion works. 

Why does m increment to 1 before executing the "if" statement? 

How does m return to 2?

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
578 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
1 answer

Suggestions of excellent examples of real C/C++ code

I'd like to particularly bring up memcached.  ...READ MORE

answered Dec 17, 2022 in C++ by narikkadan
• 63,600 points
630 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
896 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
688 views
0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
583 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
11,229 views
0 votes
1 answer

str_lib_facilities.h file from Stroustrup's Programming: Principles and Practice Using C++ generating errors

Switching the compiler to compile in c++11 ...READ MORE

answered Jun 13, 2022 in C++ by Damon
• 4,960 points
1,017 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
686 views
0 votes
1 answer

How can I build a recursive function in python?

I'm wondering whether you meant "recursive". Here ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,020 points
1,142 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,384 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,448 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP