Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

Pyramid

Antonio Nobrega
Greenhorn
Posts: 11
Hey guys, i am kinda tired of this program i have to do, i cant manage to do it right.

Basically i have to print a X number of pyramids, each pyramid with a Y height.

So for example, if the user types, he wants 3 pyramids, first pyramid's height is 3, second pyramid's height is 2, third pyramid's height is 4 here's what i have to print:

I can't manage to come up with a code that prints that. Can someone help me?

Thanks

Sheriff
Posts: 14691
16
Did you manage to print one pyramid ? Show us that code.

Antonio Nobrega
Greenhorn
Posts: 11
This is what i made:

Sheriff
Posts: 14691
16
So you've got one pyramid right There are probably different ways to print different pyramids one next to another. You can reuse some of the logic that you've already done.

Let's say you have three pyramids : 3, 2, 4
1. Find out which is the highest (highest=4)
2. Loop highest times ("row" = highest to 0 excluded)
2.1 Loop through each pyramids ("pyramidno" = 0 to 3 excluded)
2.1.1 Print the stars for the current "row" of the current pyramid (you need to know the max width of the current pyramid). If the current "row" is above the highest row of the current pyramid, print only spaces.

Try to build your program according to this algorithm, bits by bits. Start by making a method which returns the highest number of rows (step 1). etc...

Stephan van Hulst
Bartender
Posts: 5415
52
Also, you can consider building the pyramids from the bottom up, this will be a lot easier. Just start with the bottom row, then work your way up.
Remember that you can add line separators to strings.