• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The pyramid attacks!

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I apologize for posting yet another pyramid problem but I can't seem to see where I've gone wrong. I've attached a jpeg of what the pyramid is supposed to look like (PyramidSolution.jpg) and the key criteria is that it should be centralized and scalable with any number of "base bricks".

To centralize the pyramid, I tried to 'fan' out all the bricks to the left and right of a center point on the canvas using a pair of nested for loops. My best effort is a centered pyramid with the right number of bricks on each row but I can't seem to display the singular brick on top and there is a hole in the bottom row that I can't get rid of. Is there something wrong with my code or is my logic just wrong? I've attached the console printout along with my pyramid (PyramidProblem.jpg) and any help is greatly appreciated.

Many thanks!
Ming

PyramidProblem.jpg
[Thumbnail for PyramidProblem.jpg]
My results
PyramidSolution.jpg
[Thumbnail for PyramidSolution.jpg]
What its supposed to be
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you explain what the problem is, instead of just posting some code and a picture - maybe the problem is obvious to you, but it isn't to other people.

What are we seeing in the attached picture? Is that how the pyramid is supposed to look, or how it actually looks?

Does your program compile? Does it run? Does it produce some output, but not what you expected? In what way is the actual output different from what you expected?
 
Ranch Hand
Posts: 93
IntelliJ IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what's the question?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're just not running your loops right, so your center bricks are never drawn (which you only notice in the bottom row because all the other rows have outlines from surrounding bricks), your bottom row has thirteen bricks instead of twelve (counting the missing one), and there is no row with one brick.

First of all, use integer indexes, i.e., i, j, and k should be integers. Also, try to see similarities between the j-loop and k-loop. Could you collapse that logic into a single loop? Finally play around with your indexes on the j and k loops. Try to figure out why the bottom row has thirteen bricks, well, more accurately twelve bricks in thirteen positions, and the top row has two bricks instead of one. If you can solve that problem, your whole program will work.
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have done two changes to your code, which might help. please check and revert.


/*
* This program prints a pyramid centered in the canvas
*/

import acm.graphics.*;
import acm.program.*;

public class Pyramid extends GraphicsProgram {

private static final double BRICK_WIDTH = 30;
private static final double BRICK_HEIGHT = 12;
private double numOfBricks = 12;
private double counter = 12;


public void run() {
for (double i = 0; i<numOfBricks; i++) { //controls the number of rows (height)
// System.out.print("\n i= "+i+"\t");
for (double j = counter/2; j>0; j--) { //prints bricks to the left of center point
GRect rect = new GRect (getWidth()/2-(BRICK_WIDTH*j), BRICK_HEIGHT+(BRICK_HEIGHT*i), BRICK_WIDTH, BRICK_HEIGHT);
add(rect);
// System.out.print("j= "+j+"\t");
}

for (double k = counter/2; k>0; k--) { //prints bricks to the right of center point
GRect rect2 = new GRect (getWidth()/2+(BRICK_WIDTH*k), BRICK_HEIGHT-(BRICK_HEIGHT*i), BRICK_WIDTH, BRICK_HEIGHT);
add(rect2);
// System.out.print(" k="+ k +"\t");
}
counter-=1;
}
}
}
 
Ming Daines
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Greg and Rohit for responding. Rohit, I've attached your results as jpeg and I'll keep trying.
Pyramid_R.jpg
[Thumbnail for Pyramid_R.jpg]
 
Ming Daines
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally figured it out! The "fan out from the center" logic was too complicated so I centralized the pyramid by pushing it away from the side instead. This really helped to simplify the calculations. I won't be posting the solution but am happy to help if anyone has a similar problem. Thanks again all!
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic