Thread: recursion problem!!

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    Unhappy recursion problem!!

    Although this code here taught me some basics about recursion but something here isnt clear to me.

    #include<conio>
    #include<iostream>

    void doll(int size)
    {
    if(size==0)
    return;
    else{doll(size-1);cout<<(size-1);}
    }
    void main(){
    doll(10);
    getch();}

    doll(10) calls from the main() and "if size=0",then it exits. now if size!=0 , doll(9) is provoked and the transfer is directed again to doll(size).am i right?? and according to me , the cout<<size-1 should print 9,8,7,6,5,4,3,2,1 but i get 1,2,3,4,5,6,7,8,10. And also where is the transfer control directed after doll(size-1) is executed??pls help me..shouldnt it be transferred to " doll(size)" again??? if i am doing wrong in anything ..pls let me know
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    Unregistered
    Guest
    Try this instead

    Code:
    #include<iostream> 
    void doll(int size) 
    {
    	if(size==0)
    		return;
    	else
    	{
    		std::cout<<(size-1);
    		doll(size-1);
    	} 
    } 
    int main()
    {
    	doll(10);
    	getchar();
    	return 0; 
    }

  3. #3
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Oops that was me. I think you can see the difference but if you have anymore questions feel free to ask.

  4. #4
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ok

    first of all i am using borland 5.02 and i guess my compiler needs to be replaced by newer version before using your code properly. But anyway I did what you wrote but the compiler popped up "Qualifier 'identifier' is not a class or namespace name".. i dont what to do..
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  5. #5
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ok

    where is the transfer control directed after doll(size-1) is executed??pls help me..shouldnt it be transferred to " doll(size)" again??? the code ran and i was glad that you helped me out but pls answer the process controls behind it.
    thanx ;-)
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  6. #6
    Unregistered
    Guest
    void doll(int size)
    {
    if(size==0)
    return;
    else{doll(size-1);cout<<(size-1);}
    }
    void main(){
    doll(10);
    getch();}


    Start at doll(10) in main(). That call sends the value of 10 to doll() and assigns 10 to size. size is then compared with 0. If size == 0 then there is no further function calls made. Since size != 0 the else statement is entered where the first statement encountered is doll(size - 1) which is equivalent to 9 or doll(9). The cout statement is not encountered until there is a return from doll(9). The same thing is done for the call to doll(9) where doll(8) is called before the cout statement is called. The same thing continues until doll(0) is called. Then size == 0 and doll(0) will then encounter the keyword return without any cout statement and will terminate returning control to doll(1) where there is a waiting cout << size - 1 which will be 0 and then doll(1) completes and control is returned to doll(2) where there is a cout statement waiting to print size - 1 which is 1 and then doll(1) completes and returns control to doll(3) which has a cout << size -1 waiting and will print out a 2 etc. until doll(9) returns control to doll(10) which has a size - 1 remaining and prints out 9 and then there is no further return to a doll() waiting so control returns to whoever called doll(10) which is main() and the getch() is encountered.

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 problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  3. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 03:52 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. recursion problem
    By dustinc in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 04:29 AM