Question about recursion

This is a discussion on Question about recursion within the C++ Programming forums, part of the General Programming Boards category; A few posts down I saw a post about recursion, that looked interesting enough to try. ( http://cboard.cprogramming.com/showthread.php?t=106852 ) The ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    66

    Question about recursion

    A few posts down I saw a post about recursion, that looked interesting enough to try. (Recursion) The problem presented was:

    It should print its local variable and recursive call parameter. For each recursive call, display the outputs on a separate line and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful.

    I wasn't sure if I should post code in that thread or not because it was a homework thread and I'm not sure what the rules are in relation to that so I figured I'd be safe rather than sorry and open a new thread. Having stated that I think I am rather confused by where a local variable comes into play here. I think it's going to play a key-role in determining the tab stop...but without adding a second parameter I'm not entirely sure how. (I don't know if it allows you to add a second parameter or not, but that would make it too easy and the problem would lose it's challenge!)

    The code I have right now doesn't show any attempts at coming up with a tab system in place, because I keep deleting every attempt I've made so far. Just to clarify really quickly: this isn't my homework, it IS a homework problem but I am taking no programming classes. I just want to do it because it looks like a fun challenge, but I'm stuck and that's why I'm here. Any and all code snippets/or ideas/ways to solve/hints/etc are welcome!

    Code:
    // Our recursive function
    unsigned long Factorial( unsigned long n )
    {
    	// check to see if we need to stop
    	if ( n <= 0 )
    		return 1;
    	else
    	{	// we're good so keep going
    		std::cout << n << std::endl;
    		return n * Factorial( n-1 );
    	}
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::cout << Factorial(4);	// result should be something like:
    								/*
    									4
    										3
    											2
    												1
    													4*3*2*1 = 24
    								*/
    	
    	// don't close the box automatically
    	int x;
    	std::cin >> x;
    
    	return 0;
    }
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,041
    I don't see a way to do this without a second parameter or some other way to keep state between iterations.

    In order to figure out how far to indent the 1 you need to know how many times the function has already been called. There is no way around that, and that information must be passed somehow. It can be a static variable, a global, an additional parameter, or part of a struct that is passed as the one parameter. But somehow it must be passed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Argh...well that just makes last night seem like such a waste. At least I had fun trying XD
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    I'd probably have a second default parameter to control the levels of indentation (a static would have to be reset somehow if you called the function more than once in the program otherwise the indentation would be messed up in successive function calls). The function could be called (from main for example) without a specified second value which would default to 0, within the function itself recursive calls would be made by specifying this parameter (and incrementing by 1 each step).

    Code:
    unsigned long factorial(unsigned long n, unsigned int steps = 0)
    {
        ...
    
        // print out "steps" number of tab chars or something
        // print out current "n"
    
        ...
    
        // recursive call with second parameter
        return n * factorial(n-1,steps+1);
    
    }
    
    
    int main()
    {
        // When calling function from within main, don't specify the second default parameter.
        std::cout << factorial(4) << std::endl;
        std::cout << factorial(5) << std::endl;
    
        return 0;
    }
    You could use a static member, but I think you'd still need to use a default parameter to clear/reset the static value:
    Code:
    unsigned long factorial(unsigned long n, bool reset = true)
    {
        static unsigned steps;
    
        // Either increment or reset static value based on second func arg
        if( reset ) steps = 0;
        else ++steps;
    
        ...
    
        // print out "steps" number of tab chars or something
        // print out current "n"
    
        ...
    
        // recursive call with second function argument
        return n * factorial(n-1,false);
    
    }
    
    
    int main()
    {
        // When calling function from within main, don't specify the second default parameter.
        std::cout << factorial(4) << std::endl;
        std::cout << factorial(5) << std::endl;
    
        return 0;
    }
    As to what the "local" is that's being mentioned I have no idea.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iteration and recursion question
    By Stonehambey in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2008, 06:16 PM
  2. simple recursion question
    By salvadoravi in forum C Programming
    Replies: 4
    Last Post: 12-30-2007, 06:53 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Recursion vs multiple functions
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 07:52 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21