# For Loops making me loopy

Matthew Pearson
Greenhorn
Posts: 6
Im working my way through some book exercises (the art and science of java, eric s.roberts) and I am at my wits end on this one (its Q11 C4 if you have the book)

Basically you have to use for loops to display a pyramid of bricks like this

the bricks have names constants BRICK_WIDTH, BRICK_HEIGHT and BRICKS_IN_BASE

I have tried nested for loops but am going mad, I could only get a square grid of bricks or just the side so I went back to basics and did it like this (the program uses custom acm package, but that doesnt matter in principle here)

as you can see this approach (if it was logically continued (there is only 3 layers of bricks here)) does produce the correct output but the code has to be written with the BRICKS_IN_BASE number of for loops, so changing the constant means changing the code. I KNOW its supposed to be nested loops but for the life on me cant work it out. Can anyone assist me for sanities sake?

Edit by mw: Added Code Tags to preserve indentation.
[ July 18, 2007: Message edited by: marc weber ]

Jake Miller
Ranch Hand
Posts: 43
Way beyond anything I have learned yet (newbie like whoa here), but just an FYI it may help to put the code tags around your code: [ code] [/ code] without the spaces

marc weber
Sheriff
Posts: 11343
You might think of an outer loop iterating through rows (e.g., from top to bottom) and an inner loop iterating through columns (e.g., from left to right).

To get a feel for the logic, try writing a nested loop that just outputs the following:

[ July 18, 2007: Message edited by: marc weber ]

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791

What would it take to use exactly the same code in all three of those loops? The first one starts i at bricks-0, the second at bricks-1, the third at bricks-2, so we'd need a new variable that has value 0 the first time, 1 the second, 2 the third.

Then we have x at i*width, i*width+.5, i*width+1. We need another variable that goes up from zero by halves. Adding float 0.5 repeatedly is likely to get accumulating error, so lets go up by 1 and divide by 2. And now that's the same as j isn't it?

Do you see where this is going? Fix up the y position and we can put this inside a loop that increments j ...

If it gets too deeply nested, make a new method.

Does that make sense? I haven' tried this, but I'm suspicious of the x position calculation. See how it looks and adjust if necessary.

Finally, just for myself, I'd rename "j" to "row" and "i" to "brickInRow" or something more meaningful. Simple i and j for ints is so common that many people will choke on that suggestion, so take it or leave it.
[ July 18, 2007: Message edited by: Stan James ]

Matthew Pearson
Greenhorn
Posts: 6
ok, thanks for the replies, I shall get on it today and see what I can come up with.

Matthew Pearson
Greenhorn
Posts: 6
OK, I have some progress, i have

which has this result

row 1
column 1
row 2
column 1column 2
row 3
column 1column 2column 3
row 4
column 1column 2column 3column 4
row 5
column 1column 2column 3column 4column 5
row 6
column 1column 2column 3column 4column 5column 6

but i am unsure how to make it symmetrical down the middle to get the pyramid, any suggestions?

marc weber
Sheriff
Posts: 11343
Originally posted by Matthew Pearson:
...i am unsure how to make it symmetrical down the middle to get the pyramid, any suggestions?

The question is how much space to insert to the left of the first brick ("visible" column), which depends on how many bricks are in the row, which depends on which row you're in. In other words, you can calculate how much space to begin each row with based on "SIZE" and "row."

Matthew Pearson
Greenhorn
Posts: 6
I have just done, it didnt get your last post, im glad i didnt cause i worked it out myself, the pyramid structure works for all sizes of width, height nad number of bricks in base. Thank you very much for your time and patience, without which I would have given up!!
I may now continue the program so you dont have to recompile to change constants, have a GUI.
once again thank you

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Hey, glad you got it going. This:

was the key, I'm sure ... getting the two nested loops right. Now you're in good shape to make other shapes ... triangles, diamonds, zigzag lines.

What I was after very clumsily up above was to turn your three loops into the one nested loop. How did you solve for the half-width thing going across?
[ July 20, 2007: Message edited by: Stan James ]

Matthew Pearson
Greenhorn
Posts: 6
I made a variable x_offset which moves the draw position for each row of bricks, the variable gets less for each row. And the positions of each subsequent brick is calculated by the heigh and width of the brick mutliplied by the row and column respectively. Initially I had arbitary values but hen realized I could use some maths. Here it is

the drawRect is (x position, y postion, width of rect, height of rect)
I see by looking at this I could change the x_offset to SIZE*width/2 in the orignal intialization. May start the GUI now...

Matthew Pearson
Greenhorn
Posts: 6
on second thoughts, the offset cant be changed, cause its decremented in each loop!!