944,191 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1246
  • C RSS
Sep 26th, 2007
0

Help!!!!!!!!!!!!!

Expand Post »
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);
}
Reputation Points: 9
Solved Threads: 0
Newbie Poster
kemboy is offline Offline
13 posts
since Apr 2007
Sep 26th, 2007
0

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 26th, 2007
0

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....
Reputation Points: 9
Solved Threads: 0
Newbie Poster
kemboy is offline Offline
13 posts
since Apr 2007
Sep 26th, 2007
0

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....
Reputation Points: 9
Solved Threads: 0
Newbie Poster
kemboy is offline Offline
13 posts
since Apr 2007
Sep 27th, 2007
0

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.

  1. #include<stdio.h>
  2.  
  3. void hanoi(int n,char from,char aux,char to)
  4. {
  5. if (n==0)
  6. {
  7. return;
  8. }
  9. hanoi(n-1,from,to,aux);
  10. printf("\nMove disk %d from %c to %c",n,from,to);
  11. hanoi(n-1,aux,from,to);
  12. }
  13.  
  14. int main(void) {
  15. hanoi(3,'a','b','c');
  16. getchar();
  17. return 0;
  18. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 27th, 2007
0

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?
Reputation Points: 9
Solved Threads: 0
Newbie Poster
kemboy is offline Offline
13 posts
since Apr 2007
Sep 30th, 2007
0

Re: Help!!!!!!!!!!!!!

can anyone tell why and how the recursion return to a previous value of the variable N???
Reputation Points: 9
Solved Threads: 0
Newbie Poster
kemboy is offline Offline
13 posts
since Apr 2007
Oct 1st, 2007
0

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.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006
Oct 1st, 2007
1

Re: Help!!!!!!!!!!!!!

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: newline character
Next Thread in C Forum Timeline: segmentation fault in reading file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC