Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Asterick Triangles - Stuck after The first tier

vahid mirjamal
Greenhorn
Posts: 9
I'm doing a quick problem on my java book, and i just can't figure out the second part of it. Essentially what you do is enter a number and it'll make a triangle based on that using astericks like this.

If i enter 5

.
..
...
....
.....
....
...
..
.

4 would be

.
..
...
....
...
..
.

all in astericks: the code below is what i got so far.

So far I'm getting

*
**
***

just fine for when i leave out the second nested for statement which is oddly wrong. Any hints on how to get the second part of it to make the above

.
..
...
..
.

when I type in 3?

Campbell Ritchie
Sheriff
Posts: 50749
83
That's not what I got when I tried your code: I got

Enter the size of the triangle
4

****
*****
******
*******
********

Campbell Ritchie
Sheriff
Posts: 50749
83
That's not what I got when I tried your code: I got

Enter the size of the triangle
4

****
*****
******
*******
********

Sorry: I pushed "submit" instead of "preview".

Try one loop to print the * ** *** **** and a second loop to print *** ** *.

You can also do it by doubling the number of *s required and use Math.min(stars - i, i) in your for loop, which you ought to consider as an enhancement once you get it working with one loop.
By the way: you write System.out.println() if you only want a new line.

Monu Tripathi
Rancher
Posts: 1369
1
vahid mirjamal wrote:So far I'm getting

*
**
***

just fine for when i leave out the second nested for statement which is oddly wrong. Any hints on how to get the second part of it to make the above

.
..
...
..
.

when I type in 3?

Perhaps, the last for loop(involving the iterator b) should become active only after the loop(with "a" as iterator) has finished its job else, you will end up gettting what Sir Campbell Ritchie mentioned in his post. Besides, the first loop(with a) gets you upper half of the triangle, doesn't it? You might have to increase the number of iterations in the outermost for loop.

Campbell Ritchie
Sheriff
Posts: 50749
83
Monu Tripathi wrote: . . . Sir Campbell Ritchie . . .
Where does the "Sir" come from?

Murali Ponnusamy
Greenhorn
Posts: 2
public class Triangle {
[edit - solution deleted]
}

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
Murali,

We don't just hand out answers here. We strive to teach people not only how to program, but how to solve problems on their own. By all means, give tips and suggestions, make corrections, or offer new ideas. But simply posting the solution to the problem doesn't really help anyone, and can lead a desperate student to consider plagiarism.

Thanks

Campbell Ritchie
Sheriff
Posts: 50749
83
Thank you for noticing the answer and deleting it, Fred.

Monu Tripathi
Rancher
Posts: 1369
1
Campbell Ritchie wrote:
Monu Tripathi wrote: . . . Sir Campbell Ritchie . . .
Where does the "Sir" come from?

I think.....French!

Campbell Ritchie
Sheriff
Posts: 50749
83
French?

vahid mirjamal
Greenhorn
Posts: 9
I still don't get how to do the second part of the loop. Any more hints?

Thanks

Campbell Ritchie
Sheriff
Posts: 50749
83
If you have two loops, try the first loop, backwards. You may end up with this
4

*
**
***
****
****
***
**
*

In which case change the 2nd loop slightly. You find in programming that you learn by writing little bits, then tweaking them so they work better.
Or see if you can understand my suggestion with Math.min from earlier.

vahid mirjamal
Greenhorn
Posts: 9
I've already though of that just can't figure out the logic to get it to invert, it's odd but it's not coming to me.

vahid mirjamal
Greenhorn
Posts: 9
No wait I got it finally, brain starting to work a bit:

Now all i need is to remove 1 from the equation Edit, thanks I finally figured it out. Took me a bit though Thanks for not telling me the answer, although I REALLY wanted to see the answer.

Campbell Ritchie
Sheriff
Posts: 50749
83

Monu Tripathi
Rancher
Posts: 1369
1
I originally had it like this:

But then I noted that both the inner loops(involving a & b) were almost the same. And then I read this,
Campbell Ritchie wrote:You can also do it by doubling the number of *s required and use Math.min(stars - i, i) in your for loop, which you ought to consider as an enhancement once you get it working with one loop.

which was exactly what i thought. So, my code now reads:

CR: "Sir" comes from a french word "Sire" !!

Campbell Ritchie
Sheriff
Posts: 50749
83
"Sir" applied to somebody's name like that in this country is a very high honour which I am not entitled to

Nice to see how you enhanced the loop; that is probably the best way to learn programming.