• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pyramid

 
Antonio Nobrega
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you manage to print one pyramid ? Show us that code.
 
Antonio Nobrega
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i made:

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic