Thread: help with recursion function

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    help with recursion function

    I am trying to create a shape in the form of:

    ****
    ***
    **
    *
    **
    ***
    ****

    I got this shape:

    ****
    ***
    **
    *
    using a recursion function but I have been beating myself in the head trying to figure out the rest of it:

    Any clues?
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    int design(int);
    int main(int argc, char *argv[])
    {
        design(4);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    int design(int x)
    {
    int i;
    int j = 3;
    
       for(i =0; i < x; i++)      
    	  cout << "*";         
    	  
          cout << endl;             
    
      	if(x == 0)
    		return 1;
    	else
            design(x - 1);
    program output so far is:

    ****
    ***
    **
    *

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    If x equals zero, set a variable like goingup to TRUE, call design(x+1) and test in the function for goingup. If it is TRUE increment x and call design(x+1).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You don't need recursion. You can do what you've already done with a simple double loop. Then write another double loop after the first one to output the second half of the pattern.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    yes I do realize that but I am trying to learn how to do it using a single recursive function.

    But hey thanks for the reply.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    well I got some what of an idea but my inner if statement does not increment back up.
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    int design(int);
    int main(int argc, char *argv[])
    {
        design(4);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    int design(int x)
    {
    int i;
    int j = 3;
    
       for(i =0; i < x; i++)  //run until x = 0   
       {
    	  cout << "*";         
       }  
          cout << endl;             
    
      	if(x == 0)
    	{
    		                for(i = 0; i <= x; i++) // run until x = 4
    						{
    							cout << "*";			
    						}
    						cout << endl;
    
    					if(x == 4)
    					{
    					  return 1;
    					}
    				    else
    					{
    			    	  design(x + 1);  // here my x does not add to one
    					}
    	}
    	else
    	{
            design(x - 1);
    	}

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Another thing could do, without the whole concept of this 'direction' stuff (which is really more iterative than disguised as recursive), is something like this.

    Code:
    void design(int x)
    {
            // print stars
            // call design(x-1)
            // print stars
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For this particular problem, whether you use recursion or not, the most natural way to do it is to have two different pieces of code to handle the two halves of the pattern. If you use recursion, it's easier to have 2 recursive functions, one for each half. And you already know how to do that.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> For this particular problem, whether you use recursion or not, the most natural way to do it is to have two different pieces of code to handle the two halves of the pattern. If you use recursion, it's easier to have 2 recursive functions, one for each half. And you already know how to do that.

    False: see, my post. Only um -- bipartisan [?] divided into two parts [?] in the sense that it does the same printing twice.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Yes, you're right. Assuming this is a classroom problem, that's probably what the instructor was thinking of, in fact.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    Thumbs up

    Well guys I finally got it:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    void designDown(int);
    int designUp(int);
    int main(int argc, char *argv[])
    {
        designDown(4);
    	
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void designDown(int x)
    {
    	int i; 
    	for(i = 0; i < x; i++)
    		cout << "*";
    	    cout << endl;
    	
    	
           if(x == 1)
    		   designUp(x);
    	   
    	   else
    		designDown(x-1);
    
    }
    int designUp(int x)
    {
      int i ;
        
         for(i = 0; i < x; i++)
    		 cout << "*";
    	     cout << endl;
    
    		 if(x == 4)
    			 return 1;
    		 else
    			designUp(x + 1);
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Look at Tonto's code snippet - you can do this much more compactly with a single (recursive) function. And contrary to what I said earlier, it really is the most natural way to solve this particular problem.

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    yes you are right! and it is much easier to understand as well.

    thanks guys!!!

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The whole exercise is probably about understanding recursion, in particular what happens if you do things before and after calling the function recursively. So, using two functions is not a good idea. You draw the first half of the image as you go deeper in the recursion. You draw the second half as you've reached the "bottom" and are backing out of the recursion.

    Here's an example
    Code:
    void design(int n)
    {
        draw_stars(n); //function to draw a line of n stars
        if (n > 1) 
            design(n-1);
        draw_stars(n);
    }
    Now, this draws the line with the single star twice. See if you can make it draw it only once (hint: n==1 will be a special case).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM