Thread: for anyone that can do recursion

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    for anyone that can do recursion

    could someone explain the changes of the variable N for me, and explain why. you help would be appreciated....

    Code:
    #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) {
    	hanoi(3,'a','b','c');
    	getchar();
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    void main(void) should be int main(void) and 0 should be returned upon a successful return from main().

    Now with that said, what is the actual question? The phrase "changes of the variable N" sort of seems to say that you want to know how n changes. n changes because the function itself is passing a modified value of n, namely n-1, to itself.

    It's not really that complicated if you can understand that a function can simple call itself, and then it returns to itself. Also, you need to remember that one call to a function is separate to another call of a function (except as related to the parameters being passed).

    If you could clarify your question, you might receive a better answer, since I don't think this answer is that good.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    when the print statment is executed for the entire program the the 7 output result for the variable n is 1 , 2, 1, 3, 1, 2, 1........what i would like to know is how those values are arrived at...

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Uh.... Trace the program.

    First time you put 3, before it prints, it calls the function again. That's passed 2. Before it prints, it calls the function again. That's passed 1. Before it prints, it calls the function again. That's passed 0. It sees 0 has been passed, and returns.... etc. etc..

    Trace it out manually on paper.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps throw in another output statement:
    Code:
    void hanoi(int n,char from,char aux,char to) {
    	printf("hanoi(%d,'%c','%c','%c')\n", n, from, aux, to);
    	if (n==0){
    		return;}
    	hanoi(n-1,from,to,aux);
    	printf("Move disk %d from %c to %c\n",n,from,to);
    	hanoi(n-1,aux,from,to);
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    i am starting to understand but how do i know what number to return to after it pass 0. since it doesnt return 1 all the time

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What do you mean? The function returns void, which means it doesn't return anything....

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    i am starting to get a bit confuse myself...........i understand that the print statement is going to be executed 7 times........explain how the values of N is derived at each time it prints...

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I already explained that. n-1 is how it's done, because it's always being called.

    Something like this:

    Code:
    Call 1, n = 3
    	Call 2, n = 2
    		Call 3, n = 1
    			Call 4, n = 0, return
    		print
    			Call 5, n = 0, return
    	print
    		Call 6, n = 1
    			Call 7, n = 0, return
    		print
    			Call 8, n = 0, return
    print
    	Call 9, n = 2
    		Call 10, n = 1
    			Call 11, n = 0, return
    		print
    			Call 12, n = 0, return
    	print
    		Call 13, n = 1
    			Call 14, n = 0, return
    		print
    			Call 15, n = 0, return

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM