Thread: Making a pyramid using recursive functions

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

    Making a pyramid using recursive functions

    I am trying to make this:
    ( I cant really draw it correctly here it wont let me but it should be 2 pyramids one on top of the other.)
    Code:
     
               *
            *   *
          *   *   *
       *   *    *    *
          *   *   *
            *   *
               *
    The Largest row that is the middle row's length will be determined by the user. I have been trying and trying but its proving to be really hard for me. Here is the code I have so far it is not complete and my results are not what I want.
    The pyramids have to be made using recursive functions.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int printTopStars(int num);
    int printBottomStars(int num);
    
    int main()
    {
    	int num;
    
    	cout << " Enter the number of stars in the largest row of the pyramid: " << endl;
    	cin >> num;
            printTopStars(num);
    	return 0;
    
    }
    
    int printTopStars(int num)
    {
    
        int counter = 0;
    
        if(num == 0)
    		return 0;
    	else
    		for(counter;counter < num; counter++)
    		{
    			cout << "*";
    		}
    		cout << endl;
    	
      return printTopStars(num -1);
    
    }
    
    int printBottomStars(int num)
    {
       
      return 0;
    }
    The outcome of my program as of now is this:

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

    Any advice would be greatly appreciated.
    -Mike

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    *Update!*

    I have now gotten my code to print:

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

    I just need it to look like my target shape( see first post)

    here is my updated code:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int printTopStars(int num);
    int printBottomStars(int num);
    
    int main()
    {
    	int num;
    	int num2;
    	
    
    	cout << " Enter the number of stars in the largest row of the pyramid: " << endl;
    	cin >> num;
    	num2 = num;
    	printTopStars(num);
    	printBottomStars(num2);
    	return 0;
    }
    int printTopStars(int num)
    {
    
    	if(num == 0)
    		return 0;
    	else
        for (int row = 1; row <= num; row++)
    	{
            for (int col = 0; col < row; col++)
    		{
                cout << "*";
            }
            cout << endl;  // end the line.
    	}
    
       return printTopStars(0);
    }
    int printBottomStars(int num)
    {
    	int counter = 0;
        if(num == 0)
    		return 0;
    	else
    		for(counter;counter < num; counter++)
    		{
    			cout << "*";
    		}
    		cout << endl;
    	
      return printBottomStars(num - 1);
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Have you managed to just print the top half recursively?

    If you ignore the middle row as a special case, then printing the same row twice at the appropriate recursive depth does what you want.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Quote Originally Posted by Salem View Post
    Have you managed to just print the top half recursively?

    If you ignore the middle row as a special case, then printing the same row twice at the appropriate recursive depth does what you want.
    I have managed to print both the top and the bottom. (see my updated post)

    it displays this now.

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

    I need it to not be hugging the wall but to look more like a diamond ( 2 pyramids one upright the other upside down which then creates a diamond.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Pass an identation_amount variable to the recursive function. If the indenation_amount == MAX_NUM_ASTERICS, subtract 1 from the indentation amount.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you need to figure out how many leading spaces you need, based on the number of stars you need.
    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.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Quote Originally Posted by Salem View Post
    So you need to figure out how many leading spaces you need, based on the number of stars you need.
    The number of the longest row is given by the user. Im just not sure how to adjust it so it shows up the way I want it to.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Draw the shape you want on some graph paper, and figure out the mathematical relationship between spaces and stars.

    I dunno, something like
    numSpaces = width - numStars * 2;
    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. Replies: 15
    Last Post: 12-13-2008, 09:32 AM
  2. Making functions execute at certain times
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2007, 11:55 AM
  3. recursive list functions
    By sam.woolner in forum C Programming
    Replies: 13
    Last Post: 02-27-2005, 07:00 AM
  4. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM