Hey, guys im stuck on this problem. I'm supposed to make a base pyramid in the format:

--------*
------***
----*****

Basically, it takes user input and subtracts two on the next line. (e.g. 5, 3, 1)

Right now I can only make it go down one by one, I know it's an extremely basic logic problem but im stressing out on this one thing. Do I add an extra for loop or it it some current value to change?

Here is my code:

#include <iostream>
using namespace std;

void main()
{
int base;
cout << "Please enter the base of the pyramid";
cin >> base;

for(int i=0; i<= base-1;i++)
{ 
	for(int k=0; k<=base-i;k++)
		cout <<" ";

		for(int j=1; j<=i+1;j++)
{ 
	cout <<"* ";
}
cout <<"\n";
}
}

I would really appreciate a hint or step in the right direction. Thanks!

Recommended Answers

All 3 Replies

If the last row has zero spaces to the left of the *s then somehow you have to get K equal to zero.

row number zero has 1 *
row number one has 3 *s
row number two has 5 *s
row number x has (2 times the row number) plus (one) *s in it.

If the last row has zero spaces to the left of the *s then somehow you have to get K equal to zero.

row number zero has 1 *
row number one has 3 *s
row number two has 5 *s
row number x has (2 times the row number) plus (one) *s in it.

Thanks for the quick reply. I'm not sure I understand, why does k have to be equal to 0?

I think I understand the second part. Basically make it something like X row + 1, to give another star to each row?

It only has to be zero if there are no spaces before the asterixes in the bottom row and pyramid starts with 1 asterix on top row and base number of asterixes in the bottom row.

int base = number of asterixes in the bottom row;//must be odd #
int numOfRows = (base + 1)/2;
for(int i=0; i <  numOfRows; i++)
{ 
  for(int k=0; k < numOfRows - 1 - i; k++)
      cout << ' ';
  for(int m = 0; m < (i * 2) + 1; ++m)
      cout << '*';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.