• 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

Understanding the logic behind making "*" triangles

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I was hoping if someone could explain to me in details behind the logic of creating the triangle. I don't understand how every time the counter is iterated, the asterisks output the same amount as the counter. Wouldn't you have to assign the counter to the output of "*" every time there is a new iteration? For example, having a variable amountAsterisks = counter, and when counter becomes 2, the amountAsterisks also becomes 2 - where then it would output two asterisks.

Could someone please explain how the for loop works without having to explicitly state it?

[NOTE]: The code below does not correctly output the triangle. It would be great if someone could provide the correct code and an explanation of how it works.

Thank you!

 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code will not produce a triangle, but would rather give a 10x10 square.

I assume, you are working to get a triangle which has one * in first row, two *s in second row and so on.

The logic is , you should have inner loop condition set to out loop variable.

So you check inner loop untill it reaches the value of the outer loop .
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch!

First, nobody is going to write the code for you. That's not how this site works. People come here to help others learn, not to hand out solutions. So if you show some effort and explain clearly what you're having trouble with, people will be happy to nudge you in the right direction toward figuring things out for yourself.

As for your actual question, it's not clear what you're asking. We have a couple loops, and on each iteration of the inner loop, we print a '*' on the current line, and on each iteration of the outer loop, we print a newline. I have no idea what you mean by "assign the counter to the output of '*'", or why you think we need another variable, or what you think that variable would represent.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code does totally different thing from what you expect. As others said, nobody is going to implement it for you.
I would suggest you try to make it work for a simpler version of what you want, to produce the output like this:

This is very easy to implement, it shouldn't take you long. Then, if you can't get it from head, try to draw what you want to accomplish, how asterisks are positioned in each line, how many of them are in each line and how many spaces you need to print in each line. That way you'll get an idea how to calculate those values for each iteration, and finally the code should be straightforward once you get that.
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the posts, everyone!

Jeff Verdegan wrote:Hi, and welcome to the Ranch!

First, nobody is going to write the code for you. That's not how this site works. People come here to help others learn, not to hand out solutions. So if you show some effort and explain clearly what you're having trouble with, people will be happy to nudge you in the right direction toward figuring things out for yourself.

As for your actual question, it's not clear what you're asking. We have a couple loops, and on each iteration of the inner loop, we print a '*' on the current line, and on each iteration of the outer loop, we print a newline. I have no idea what you mean by "assign the counter to the output of '*'", or why you think we need another variable, or what you think that variable would represent.



Hi!

Yes, I understand that no one will write the code for me. On my original post, I simply said that it would be great. I should have been more specific as to the statement not being a request, sorry. And I know that the program does not work correctly to produce the triangle. However, I am more concerned with the logic behind nested for loops than the actual program.

I guess the more specific question would be that I don't understand how the nested for loops work in a way that outputs the asterisks in relation to the counter. For example, when counter = 1, one asterisk would be produced. When the counter = 2, two asterisks would be produced. I don't see how the increase of counters tell the program to produce a certain amount of asterisks. I would think that it would only produce one asterisk, regardless of how many times the counter incremented.
What exactly does the outer loop do to the inner loop, and vice-versa?

To be more specific, based on the code:


The outer for loop is initializing that row = 1, and that while row is less than or equal to 10, it will increment. And in the inner loop, the column will do the same. So, every time the row and column increment, the system will output the asterisk. What I can't grasp is how having a nested for loop statement causes the asterisks outputted to be equal to the incremental counter. I would think that it would just produce one asterisk.

P.S. How do I quote multiple posts?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeong Ryu wrote:
I guess the more specific question would be that I don't understand how the nested for loops work in a way that outputs the asterisks in relation to the counter. For example, when counter = 1, one asterisk would be produced. When the counter = 2, two asterisks would be produced. I don't see how the increase of counters tell the program to produce a certain amount of asterisks. I would think that it would only produce one asterisk, regardless of how many times the counter incremented.
What exactly does the outer loop do to the inner loop, and vice-versa?

To be more specific, based on the code:


The outer for loop is initializing that row = 1, and that while row is less than or equal to 10, it will increment. And in the inner loop, the column will do the same. So, every time the row and column increment, the system will output the asterisk. What I can't grasp is how having a nested for loop statement causes the asterisks outputted equals the incremental counter. I would think that it would just produce one asterisk.



Each pass through the inner loop produces one asterisk. The inner loop runs 10 times, so it produces 10 asterisks.

The outer loop runs 10 times, each time running the inner loop (which runs 10 times, producing 10 asterisks) and then printing a newline.

Try changing the numbers from 10 and 10 to something smaller and different for each loop, say, 2 and 3. Then try 3 and 2.

P.S. How do I quote multiple posts?



If you mean nested quotes, just quoting a post that quotes another does that for you. If you mean getting pieces from two separate posts, no nested, into your post, you'd have to open a text editor, hit the quote button for each post, copy what's there, and paste it into your editor.
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Jeong Ryu wrote:
I guess the more specific question would be that I don't understand how the nested for loops work in a way that outputs the asterisks in relation to the counter. For example, when counter = 1, one asterisk would be produced. When the counter = 2, two asterisks would be produced. I don't see how the increase of counters tell the program to produce a certain amount of asterisks. I would think that it would only produce one asterisk, regardless of how many times the counter incremented.
What exactly does the outer loop do to the inner loop, and vice-versa?

To be more specific, based on the code:


The outer for loop is initializing that row = 1, and that while row is less than or equal to 10, it will increment. And in the inner loop, the column will do the same. So, every time the row and column increment, the system will output the asterisk. What I can't grasp is how having a nested for loop statement causes the asterisks outputted equals the incremental counter. I would think that it would just produce one asterisk.



Each pass through the inner loop produces one asterisk. The inner loop runs 10 times, so it produces 10 asterisks.

The outer loop runs 10 times, each time running the inner loop (which runs 10 times, producing 10 asterisks) and then printing a newline.

Try changing the numbers from 10 and 10 to something smaller and different for each loop, say, 2 and 3. Then try 3 and 2.

P.S. How do I quote multiple posts?



If you mean nested quotes, just quoting a post that quotes another does that for you. If you mean getting pieces from two separate posts, no nested, into your post, you'd have to open a text editor, hit the quote button for each post, copy what's there, and paste it into your editor.



Are you saying that the for loops iterate based on the number on the counter? So when counter = 1, it loops once. And when counter = 2, it loops twice, etc. If that is the case, I can understand how the incremented counter affects how many asterisks are produced.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeong Ryu wrote:
Are you saying that the for loops iterate based on the number on the counter? So when counter = 1, it loops once. And when counter = 2, it loops twice, etc. If that is the case, I can understand how the incremented counter affects how many asterisks are produced.



No. The counter is only used here to count the number of times executed so far, and to test in the condition to see if we're done yet. In the inner loop, when column is 0, we print one '*', and when it's 1, we print one '*', and when it's 2, we print one '*', ..., and when it's 9 we print one '*', and when it's 10 we're done so we don't execute the body at all any more.

A for loop iterates as long as its condition is true. Those particular for loops each iterate 10 times.

You can find more details by reading the looping section of a tutorial, such as here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Also, did you try using different numbers like I suggested?
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Jeong Ryu wrote:
Are you saying that the for loops iterate based on the number on the counter? So when counter = 1, it loops once. And when counter = 2, it loops twice, etc. If that is the case, I can understand how the incremented counter affects how many asterisks are produced.



No. The counter is only used here to count the number of times executed so far, and to test in the condition to see if we're done yet. In the inner loop, when column is 0, we print one '*', and when it's 1, we print one '*', and when it's 2, we print one '*', ..., and when it's 9 we print one '*', and when it's 10 we're done so we don't execute the body at all any more.

A for loop iterates as long as its condition is true. Those particular for loops each iterate 10 times.

You can find more details by reading the looping section of a tutorial, such as here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Also, did you try using different numbers like I suggested?



I see. So, when the outer loop iterates once, the inner loop iterates 10 times (assuming that we are doing column <= 10)?
Which would makes sense why my code produced a rectangle.

I've experimented with my code, and I was able to find the correct one to create a triangle:


I think I understand why I needed "column <= row". When outer for loop iterates once, the counter = 1. And in the inner for loop, it will iterate 10 times. However, because it starts with "column = 1" and is "<= row" (which is 1), the loop stops after iterating once - creating a single asterisk. And after the inner loop terminates, the outer for loop iterates again, but now with "counter = 2", which then allows the inner for loop to iterate twice, which produces two asterisks, and so on and so forth.
Is this correct?

And yes, I did try using the other numbers. Did you want me to use these numbers so that I could understand what I've said above?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeong Ryu wrote:
I see. So, when the outer loop iterates once, the inner loop iterates 10 times (assuming that we are doing column <= 10)?



Correct.

I think I understand why I needed "column <= row". When outer for loop iterates once, the counter = 1. And in the inner for loop, it will iterate 10 times. However, because it starts with "column = 1" and is "<= row" (which is 1), the loop stops after iterating once - creating a single asterisk. And after the inner loop terminates, the outer for loop iterates again, but now with "counter = 2", which then allows the inner for loop to iterate twice, which produces two asterisks, and so on and so forth.
Is this correct?



Yup.

And yes, I did try using the other numbers. Did you want me to use these numbers so that I could understand what I've said above?



Yes, or at least to get you started on the path to understanding. I wanted you to see the relationship between what was in your code and what the output was. Smaller numbers are easier to work with for this stuff and easier to trace through by hand, and using different values for the two loops lets you see which one contributes what. I've been doing this stuff for a couple of decades now, and I still do that when I encounter something new or confusing. It's a technique you'll want to hang onto.
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Jeong Ryu wrote:
I see. So, when the outer loop iterates once, the inner loop iterates 10 times (assuming that we are doing column <= 10)?



Correct.

I think I understand why I needed "column <= row". When outer for loop iterates once, the counter = 1. And in the inner for loop, it will iterate 10 times. However, because it starts with "column = 1" and is "<= row" (which is 1), the loop stops after iterating once - creating a single asterisk. And after the inner loop terminates, the outer for loop iterates again, but now with "counter = 2", which then allows the inner for loop to iterate twice, which produces two asterisks, and so on and so forth.
Is this correct?



Yup.

And yes, I did try using the other numbers. Did you want me to use these numbers so that I could understand what I've said above?



Yes, or at least to get you started on the path to understanding. I wanted you to see the relationship between what was in your code and what the output was. Smaller numbers are easier to work with for this stuff and easier to trace through by hand, and using different values for the two loops lets you see which one contributes what. I've been doing this stuff for a couple of decades now, and I still do that when I encounter something new or confusing. It's a technique you'll want to hang onto.



Yes, finally!! The whole 'counter' thing tripped me over, haha.
Thank you so much for your help!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're very welcome. Glad I could help.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I'm also very new to Java, I managed to get a solution for the triangles problem. If anyone is interested.
I dont suppose this is a very neat solution, since I'm very new to Java, but the output at least was correct.
Any comments or tips very much welcome.



output was

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

I'm using eclipse btw.
 
reply
    Bookmark Topic Watch Topic
  • New Topic