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

Reply

Join Date: Apr 2007
Posts: 13
Reputation: kemboy is an unknown quantity at this point 
Solved Threads: 0
kemboy kemboy is offline Offline
Newbie Poster

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

 
0
  #1
Sep 26th, 2007
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);
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

 
0
  #2
Sep 26th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 13
Reputation: kemboy is an unknown quantity at this point 
Solved Threads: 0
kemboy kemboy is offline Offline
Newbie Poster

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

 
0
  #3
Sep 26th, 2007
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....
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 13
Reputation: kemboy is an unknown quantity at this point 
Solved Threads: 0
kemboy kemboy is offline Offline
Newbie Poster

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

 
0
  #4
Sep 26th, 2007
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....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

 
0
  #5
Sep 27th, 2007
>>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. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 13
Reputation: kemboy is an unknown quantity at this point 
Solved Threads: 0
kemboy kemboy is offline Offline
Newbie Poster

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

 
0
  #6
Sep 27th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 13
Reputation: kemboy is an unknown quantity at this point 
Solved Threads: 0
kemboy kemboy is offline Offline
Newbie Poster

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

 
0
  #7
Sep 30th, 2007
can anyone tell why and how the recursion return to a previous value of the variable N???
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #8
Oct 1st, 2007
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
1
  #9
Oct 1st, 2007
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC