Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...with companys cutting back on training, lack of true support by makers of software, the forums are a great tool in your cyber-toolbox...."

Geography

Where in the world do Tek-Tips members come from?

Recursion Applications

Recursive Function Definition for Tower of Hanoi.
Posted: 4 May 01

The Explanation
I thank Aphrodita for this neat explanation it is copied AS IS :

The first recursive call would move all but the lowest disk on an auxilary one, and then move the biggest of them to the final destination. As you do it, you again have two towers to move around your disks, because if you put any disk on top of the biggest, it does not break the rule.
The second recursive call, makes the first one to solve the problem for n - 1 disks, and at the end of it, it will move the pre-biggest disk on top of the biggest in the 3rd tower. Totally, it will happen n time, making the problem smaller each time, until n becomes 1, which is to mave the last disk on top of all in the 3rd tower.



The code >>
#include <stdio.h>

void move(int,int,int,int);
int main(void)
{
move(4,1,3,2);
}


void move(int n,int A,int C,int B)
{
if (n==1)
    {
    printf("Move from %d to %d.\n",A,C);
       }
else
    {
    move(n-1,A,B,C);
    move(1,A,C,B);
    move(n-1,B,C,A);
    }
}

Regards,
SwapSawe.

Back to C FAQ Index
Back to C Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close