# Thread: Making a pyramid using recursive functions

1. ## 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. *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. 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.

4. Originally Posted by Salem
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. Pass an identation_amount variable to the recursive function. If the indenation_amount == MAX_NUM_ASTERICS, subtract 1 from the indentation amount.

6. So you need to figure out how many leading spaces you need, based on the number of stars you need.

7. Originally Posted by Salem
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. 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;