wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Asterick Triangles - Stuck after The first tier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Asterick Triangles - Stuck after The first tier" Watch "Asterick Triangles - Stuck after The first tier" New topic
Author

Asterick Triangles - Stuck after The first tier

vahid mirjamal
Greenhorn

Joined: Feb 24, 2009
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

Joined: Oct 13, 2005
Posts: 39885
    
  28
That's not what I got when I tried your code: I got

Enter the size of the triangle
4

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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Oct 12, 2008
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.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Monu Tripathi wrote: . . . Sir Campbell Ritchie . . .
Where does the "Sir" come from?
Murali Ponnusamy
Greenhorn

Joined: Feb 24, 2009
Posts: 2
public class Triangle {
[edit - solution deleted]
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Thank you for noticing the answer and deleting it, Fred.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Campbell Ritchie wrote:
Monu Tripathi wrote: . . . Sir Campbell Ritchie . . .
Where does the "Sir" come from?

I think.....French!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
French?
vahid mirjamal
Greenhorn

Joined: Feb 24, 2009
Posts: 9
I still don't get how to do the second part of the loop. Any more hints?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Feb 24, 2009
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

Joined: Feb 24, 2009
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

Joined: Oct 13, 2005
Posts: 39885
    
  28
Monu Tripathi
Rancher

Joined: Oct 12, 2008
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

Joined: Oct 13, 2005
Posts: 39885
    
  28
"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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Asterick Triangles - Stuck after The first tier