Thread: recursive function

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    recursive function

    I am writing a recursive function that seems to work, but I have to also include a while loop to ask the user for the depth of the recursion. This is resulting in an endless loop.

    Here is the code:

    Code:
     void recurse(int count, int depth);	//	function prototype
    
     int main()
     {
    
    	 int depth;
    	 cout << "Please enter a number for recursion depth, or -1 to exit: " << endl;
    	 cin >> depth;
    	 while (depth != depth *2)
    	 {
    		 for (int counter = depth -(depth * 2); counter <= depth; counter++)
    	 	 {
    				recurse(counter, depth);
    		 }
    
    	cout << "Please enter a number for recursion depth, or -1 to exit: " << endl;
    }
    	return 0;
    
    	 }
    
    
    void recurse(int count, int depth)
    {
    	if (count < 0)
    		cout << "This was written by call number " << (count * -1) << "." << endl;
    	else if (count > 0)
    		cout << "This was ALSO written by call number " << count << "." << endl;
    	else if (count == depth)
    		return;
    }
    Any ideas on how to break from this while loop?
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    Code:
    while (depth != depth *2)
    I haven't looked at the rest, but that is the source of your infinite loop.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you're just interested in a recursive call x number of times, then a while loop is not needed. Just pass an argument that gets decremented by 1 in each recursion.
    Code:
    void Recurse(int Count)
    {
       if(Count > 0) Recurse(Count - 1);
    }
    
    void StartRecursion()
    {
       cin >> NrOfTimes;
       Recurse(NrOfTimes);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    My program needs to print out something similar to this:

    Get input from user for incursion depth of x = 5, so:

    This was written by call number x.
    This was written by call number x-1.
    This was written by call number x-2.
    This was written by call number x-3.
    This was written by call number x-.4
    This was ALSO written by call number x-4.
    This was ALSO written by call number x-3.
    This was ALSO written by call number x-2.
    This was ALSO written by call number x-1.

    My question is how to construct my while loop to prompt the user for another incursion depth, or a sentinel to exit.
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I don't know what a sentinel is...
    You could use 0 as an exit condition though:
    Code:
    int InputDepth;
    
    while(true)
    {
       cin >> InputDepth;
       if(InputDepth == 0) break;
       Recurse(InputDepth);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    perfect! thanks Magos
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM