DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Help!!!!!!!!!!!!! (http://www.daniweb.com/forums/thread90864.html)

kemboy Sep 26th, 2007 10:30 pm
Help!!!!!!!!!!!!!
 
hey can anyone explain this code to me, i am trying to understand it for days now but i still cant figue it out its the code of "the towers of hanoi"........it goes like this...


#include<stdio.h>

void hanoi(int n,char from,char aux,char to) {
if (n==0){
return;}
hanoi(n-1,from,to,aux);
printf("\nMove disk %d from %c to %c",n,from,to);
hanoi(n-1,aux,from,to);
}

void main(void) {
int exit;
hanoi(3,'a','b','c');
scanf("\n%d",&exit);
}

Ancient Dragon Sep 26th, 2007 11:19 pm
Re: Help!!!!!!!!!!!!!
 
It uses recursion -- if you don't know what recursion is then you won't understand how the program works. Other than that, it is a poorly written program.

kemboy Sep 26th, 2007 11:29 pm
Re: Help!!!!!!!!!!!!!
 
i understand recursion, i know that recursion is a function that calls itself. is it possible to explain each line of the code until the program exit. i would appreciate it..... its been days since i have been trying to figure it out but....i am still going no where with it....

kemboy Sep 26th, 2007 11:36 pm
Re: Help!!!!!!!!!!!!!
 
when the program run the output of "N" is 1,2,1,3, 1,2,1 after the statement is print 7 times. i need to know how is that possible....

Ancient Dragon Sep 27th, 2007 12:17 am
Re: Help!!!!!!!!!!!!!
 
>>i need to know how is that possible....
Get out a pencil & paper then step through the program youself and you will see how it works. The first time hanoi is called from main() the value of N is 3. Since N is not 0 the test on line 5 will fail and line 9 is executed. It subtracts 1 form N and calls hanoi again with the value of 4. This repeats itself until 0 is reached at which time line 7, the return, is executed. Then line 10 is executed with N = 1. Now you can trace through that program with pencil & paper writing down the values of N at each step of the program.

#include<stdio.h>

void hanoi(int n,char from,char aux,char to)
{
    if (n==0)
    {
        return;
    }
    hanoi(n-1,from,to,aux);
    printf("\nMove disk %d from %c to %c",n,from,to);
    hanoi(n-1,aux,from,to);
}

int main(void) {
  hanoi(3,'a','b','c');
  getchar();
  return 0;
}

kemboy Sep 27th, 2007 1:22 am
Re: Help!!!!!!!!!!!!!
 
after the 1 is printed in the print statement and the function is again called below the print statement the program is now going to ignore the top structure call right?
And why is the value now 2. is it safe to say it increment?

kemboy Sep 30th, 2007 2:33 pm
Re: Help!!!!!!!!!!!!!
 
can anyone tell why and how the recursion return to a previous value of the variable N???

WaltP Oct 1st, 2007 3:14 am
Re: Help!!!!!!!!!!!!!
 
As a simplistic explanation, each time you call the recursive function, a new copy of the function is created, including new variables. The values of the variables in the previous copy are not destroyed, so when the new function returns, the values are as they were before the call was made.

Salem Oct 1st, 2007 3:46 am
Re: Help!!!!!!!!!!!!!
 
Recursion explained


All times are GMT -4. The time now is 6:23 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC