# Making a pyramid using recursive functions



## xMikex (Nov 8, 2005)

Please help i am desperate.

here is my code:

#include <iostream>
#include <iomanip>

using namespace std;

int printTopStars(int num, int row);
int printBottomStars(int num , int row);

int main()
{
int num = 0;

cout << " Enter the number of stars in the largest row of the pyramid: " << endl;
cin >> num;
printTopStars(num, 1);
return 0;
}
int printTopStars(int num, int row)
{

for ( int i = 0; i <= num - row; i++)
{
cout << " ";
}

for (int i = 0; i < row; i++)
{
cout << "*";
}
cout << endl;

if (num == row)
{
return 0 ;
}
printTopStars(num, row + 1);
return 0;
}
int printBottomStars(int num,int row)
{
return 0;
}

I need it to print out to pyramids one right side up and another upside down.

they have to be recursive functions and the user specifies how long the largest row is.

can you please help i have been trying and trying to get this to work ....


----------



## xMikex (Nov 8, 2005)

*updated code*
My code is supposed to display a diamond shape with the longest row of the diamond to be user defined.
I have to keep my code pretty much the same as I am about to show you but maybe you guys can figure out why my bottom half of the diamond isn't showing properly.

#include <iostream>
#include <iomanip>

using namespace std;

int printTopStars(int num, int row);
int printBottomStars(int num , int row);

int main()
{
int num = 0;

cout << " Enter the number of stars in the largest row of the pyramid: " << endl;
cin >> num;
printTopStars(num, 1);
printBottomStars(num, 2);
return 0;
}
int printTopStars(int num, int row)
{

for ( int i = 0; i < num - row; i++)
{
cout << " ";
}

for (int i = 0; i < row; i++)
{
cout << " *";
}
cout << endl;

if (num == row)
{
return 0 ;
}
printTopStars(num, row + 1);
return 0;
}
int printBottomStars(int num,int row)
{
for( int i = 1; i < row; i++)
{
cout << " ";
}
for(int i = 1; i < num - row + 2; i++)
{
cout << " *";
}
cout << endl;

if(num == 0)
{
return 0;
}

printBottomStars(num - 1,row);

return 0;
}


----------



## Chicon (Jul 29, 2004)

Hi xMikex,

In your function *printBottomStars*, the variable *row* is keeping its initial value *2* and therefore the loop
*
for( int i = 1; i < row; i++)
{
cout << " ";
}
*
will only edit one space.


----------

