This week's book giveaway is in the Cloud/Virtualization forum.We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

# Pyramid

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

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...

Saloon Keeper
Posts: 9219
177
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.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.