• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Asterick Triangles - Stuck after The first tier

 
vahid mirjamal
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not what I got when I tried your code: I got

Enter the size of the triangle
4

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

 
Campbell Ritchie
Sheriff
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monu Tripathi wrote: . . . Sir Campbell Ritchie . . .
Where does the "Sir" come from?
 
Murali Ponnusamy
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Triangle {
[edit - solution deleted]
}
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

We are glad you've joined us here, but please be a little more careful in the kind of help you give someone.

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for noticing the answer and deleting it, Fred.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Monu Tripathi wrote: . . . Sir Campbell Ritchie . . .
Where does the "Sir" come from?

I think.....French!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
French?
 
vahid mirjamal
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't get how to do the second part of the loop. Any more hints?

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic