• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested loops and them looping through a grid?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all! new to java programming and I need some serious help


So I have a grid and height and width are human inputs.



So here when the human enters say 10, width and height will be the same? I think? don't know if this is true.
Then if I understand nested loops correctly what happens is that int is initialized to 0, ok , then the condition is check 0<10 (true), ok then it goes to the next line. So int is initialized to 0; then checks 0<10 (true). Now the part which really confuses me is what is happening with the part: grid.setColor(i, k, Color.red); what exactly is even happening here? Like grid.SetColor, is that like classname.method? So the first loop checks out, and the second loop checks out then what does the grid.setColor(i,k, Color.red) do?

I understand that the outer loop is basically what tells the inner loop how many times to run. So this cycling happens 10 times if the user types in 10, from the initial 0. So in my mind each time grid changes with accordance with the loops and what number they are at gets filled in with their appriate letters in the parameters of the class.method()

something like:

grid.setColor(0, 0, Color.red);
grid.setColor(1, 1, Color.red);
grid.setColor(2, 2, Color.red);
grid.setColor(3, 3, Color.red);
grid.setColor(4, 4, Color.red);
grid.setColor(5, 5, Color.red);
grid.setColor(6, 6, Color.red);
grid.setColor(7, 7, Color.red);
grid.setColor(8, 8, Color.red);
and so on...

Now sure if I have the order correct or iteration steps. what exactly is happening when i and k arguments doing when they get passed into the method parameters? and then what does Color.red do?

Thank you!
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The inner loop runs 'width' times for every time the outer loop runs, which is 'height' times. So if the user enters 3 for height, and 5 for width, this is what will happen:

Color is presumably a class or enum containing constants for certain colors.

grid is a likely a data structure that can store colors in rows and columns.

None of this can be said with certainty unless we know more about the code.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . Now sure if I have the order correct or iteration steps.. . .
No you haven't.

What you actually have is more like this:-
grid.setColor(0, 0, Color.red);
grid.setColor(0, 1, Color.red);
grid.setColor(0, 2, Color.red);
grid.setColor(0, 3, Color.red);
grid.setColor(0, 4, Color.red);
grid.setColor(0, 5, Color.red);
grid.setColor(0, 6, Color.red); …
grid.setColor(0, width - 2, Color.red);
grid.setColor(0, width - 1, Color.red);

grid.setColor(1, 0, Color.red);
grid.setColor(1, 1, Color.red);
grid.setColor(1, 2, Color.red);
grid.setColor(1, 3, Color.red);
grid.setColor(1, 4, Color.red);
grid.setColor(1, 5, Color.red);
grid.setColor(1, 6, Color.red); …
grid.setColor(1, width - 2, Color.red);
grid.setColor(1, width - 1, Color.red);

grid.setColor(2, 0, Color.red);
grid.setColor(2, 1, Color.red);
grid.setColor(2, 2, Color.red);
grid.setColor(2, 3, Color.red);
grid.setColor(2, 4, Color.red);
grid.setColor(2, 5, Color.red);
grid.setColor(2, 6, Color.red); …
grid.setColor(2, width - 2, Color.red);
grid.setColor(2, width - 1, Color.red);



grid.setColor(height - 2, 0, Color.red);
grid.setColor(height - 2, 1, Color.red);
grid.setColor(height - 2, 2, Color.red);
grid.setColor(height - 2, 3, Color.red);
grid.setColor(height - 2, 4, Color.red);
grid.setColor(height - 2, 5, Color.red);
grid.setColor(height - 2, 6, Color.red); …
grid.setColor(height - 2, width - 2, Color.red);
grid.setColor(height - 2, width - 1, Color.red);

grid.setColor(height - 1, 0, Color.red);
grid.setColor(height - 1, 1, Color.red);
grid.setColor(height - 1, 2, Color.red);
grid.setColor(height - 1, 3, Color.red);
grid.setColor(height - 1, 4, Color.red);
grid.setColor(height - 1, 5, Color.red);
grid.setColor(height - 1, 6, Color.red); …
grid.setColor(height - 1, width - 2, Color.red);
grid.setColor(height - 1, width - 1, Color.red);

What is wrong with the Graphics#fillRect() method?
No, grid.setColor(...) is objectReferenceName.methodName(...)
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Justin Robbins wrote:. . . Now sure if I have the order correct or iteration steps.. . .
No you haven't.

What you actually have is more like this:-

. . .

What is wrong with the Graphics#fillRect() method?
No, grid.setColor(...) is objectReferenceName.methodName(...)


I don't know how to use the fillRect() method :/

I am really confused why the 'i' and 'k' go in that order. Could you please explain the reason they are changing like that? I don't see the pattern there.

Thank you for your help!
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:
Stephan wrote:None of this can be said with certainty unless we know more about the code.

Usually the arguments to graphics methods are X followed by Y. E.g.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's starting to make a lot more sense now. I made a diagonal pattern but it came out differently than expected and I was hoping someone here could shed light on it. Here is the extension of the earlier code:




So if I understand correctly, it checks the condition of the outer loop then runs the inner loop until it's proven false. And within the inner loop the col is defined and stays the same no matter what. I'll choose '4' for user input, making height and width the same.
So it checks the outer loop: 0<4 (true)
Then the inner loop condition gets checked: 0<4 (true)
Then checks if-statement: 0<=0 (true)
Then it sets accordingly
grid.setColor(0,0, Color.yellow);

then it cycles through that inner loop because of its brackets until the loop is proven false:


So it paints the whole top row yellow?

then the process repeats starting with the outer loop

Checks the outer loop 1<4 (true)
Checks the inner loop 0<4 (true)
Checks the if-statement 1<=0 (false)
So then this gets set to red and the process goes:

grid.setColor(1,0, Color.red);

Then runs the whole inner loop 1<4 (true)
Checks the if-statement 1<=1 (true)
Sets it:
grid.setColor(1,1, Color.yellow);
then the next iteration
2<4(true)
1<=2 (true)
grid.setColor(1,2, Color.yellow);

grid.setColor(1,3, Color.yellow);
grid.setColor(1,4, Color.yellow);

So I'd imagine that the whole first row at the top is yellow. Then the second row starts with one red then the rest yellow. The third row two red the rest yellow and so on. But when it runs instead it's one yellow at the top, then the rest red. Then two yellow in the second row, the rest red and so on in that pattern. What am I misunderstanding here?

Thanks!


 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't quote the whole of a preceding post; that simply makes the thread get longer and longer to no benefit. I have removed most of the quote because you can read it anyway.
Justin Robbins wrote:It's starting to make a lot more sense now. . . .
So if I understand correctly, it checks the condition of the outer loop then runs the inner loop until it's proven false.
It is very satisfying when things start to make sense, isn't it? Yes, the outer loop starts and (assuming its condition is true) it tries to start the inner loop until it terminates.
And within the inner loop the col is defined and stays the same no matter what. I'll choose '4' for user input, making height and width the same.
So it checks the outer loop: 0<4 (true)
Then the inner loop condition gets checked: 0<4 (true)
Then checks if-statement: 0<=0 (true)
. . .
You are using col in the outer row, so that goes from left to right. So when you are in the top row (row=0) you only get the col <= row true for the leftmost column, no 0. In the 2nd row, the first two columns will pass that test, and when you get to the bottom row in a square pattern, it will be true for everything except the rightmost column. So you would expect the lower left part to be yellow and the upper right to be red.

That assumes that your setColor method is supposed to take row before column when you have column before row everywhere else.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't get it....



Wher 'i' represents what's going to be passed into col and 'k' represents what's going to be passed into row of the setColor() method. But what makes row and col represent those things in the method? Like how is row automatically what makes the grid go from left to right, and col what automatically makes the grid go up to down, or do I have that backwards? Is it done through the method, is that what defined them? I always thought that rows were what starts at the leftmost side of the block and then moves along horizontally filling in each block as it goes. And then column are like vertical posts starting at the top moving downwards. So if we have a col set to '0' and a row set to '0' that means that row and col are at the exact same spot? on the upper leftmost point? should I think of them as x,y coordinate? like if I have 0,0 that's like x,y? in other words the col,row? but by default setColor() is row first then col? so confused about this. And if I have 0,1 that's like saying start and the very top leftmost point and move over one the right? so the first variable in the setColor() method defines what block it will start on, on that first upward stack of blocks and then the second ar of setColor() will define how far over we will go from left to right?

What exactly is 'row' and 'col' and what defines their behavior for 'row' moving from left and right and col moving from top to borrom in the grid class?

Thank you
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
move rightcol++
move leftcol--
move downrow++
move uprow--


So a row is like the y axis and a column is like the x axis. In many spreadsheets the columns are labeled "A", "B", "C", etc. The rows are number 1, 2, 3, etc. So A1 = coordinates (0, 0), A2 = (1, 0), B1 = (0, 1), etc.

Edit: s/b A2 = (0, 1), B1 = (1, 0).
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right to suggest that it depends on how the setColor method is defined. And how the output is displayed. If you represent an array of pixels like that, if row=0 means top and row=n means bottom, if col=0 means left and col=m means right, then you can work out which pixel will be which colour. If n=9, you get 10 rows, so let's have row ≤ col = red otherwise yellow.
RRRRRRRRRR
YRRRRRRRRR
YYRRRRRRRR
YYYRRRRRRR
YYYYRRRRRR
YYYYYRRRRR
YYYYYYRRRR
YYYYYYYRRR
YYYYYYYYRR
YYYYYYYYYR
You can of course change that; you can change the ≤ to <, > or ≥. You can also have row + col ≤ n, or one of the other operators, which will mean the demarcation will run this direction / rather than \
Try those combinations on a grid similar to what I just wrote and you can see the differences.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . . the demarcation will run this direction / rather than \   . . .
And when you try out the different combinations you will find out I was mistaken there.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never thought rows and cols would be so difficult to understand. Java is new to me...

So the way I've been thinking of it is like this, so height and width are set to 4, then Java outputs a grid like this:

[0,0] [0,1] [0,2] [0,3] [0,4]
[1,0] [1,1] [1,2] [1,3] [1,4]
[2,0] [2,1] [2,2] [2,3] [2,4]
[3,0] [3,1] [3,2] [3,3] [3,4]
[4,0] [4,1] [4,2] [4,3] [4,4]

So working from the top left we have [0,0] the first '0' is the row number? and the second '0' is the column number?
then below that we have [1,0] so '1' is the row number and '0' is the column number? Is that right?


Why does col++ move things to the right?
Why does col-- move things to the left?
Why does row++ move things down?
Why does row-- move things up?

I thought column vertical? and rows were horizontal?

Thanks!
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:Never thought rows and cols would be so difficult to understand. Java is new to me...
Nothing to do with Java®; the problem is independent of any language. Most languages are very easy to understand. It is programming that is difficult.


So the way I've been thinking of it is like this, so height and width are set to 4, then Java outputs a grid like this:

[0,0] [0,1] [0,2] [0,3] [0,4]
[1,0] [1,1] [1,2] [1,3] [1,4]
[2,0] [2,1] [2,2] [2,3] [2,4]
[3,0] [3,1] [3,2] [3,3] [3,4]
[4,0] [4,1] [4,2] [4,3] [4,4]

So working from the top left we have [0,0] the first '0' is the row number? and the second '0' is the column number?. . .
No. Assuming you use the usual (xy) notation, your grid will read.
But when you go up to index 4, you have a 5×5 grid. Note on a computer screen (0, 0) is usually the top left corner and columns count more to the right and rows count more downwards. That is different from when we learnt Cartesian coordinates at school. There is a section about coordinates in the Java™ Tutorials.

As for col++. Beware. You are at risk of confusion already, and the i++ operator can cause confusion to those who haven't struggled with it a lot. In your present project, always use row++; col++; row--; and col--; as statements on their own. Then it will be obvious where the incrementing occurs and there will be no problems caused by your missing the increment (as can happen if you are unlucky and use them inside other expresions).
If you increase the column number you move to the right, if you increase the row number you move downwards, and vice versa.
Columns might be vertical, but the number of the column is how far you have to go to the right to find that column. Rows might be horizontal but the number of that row is how far you have to go downwards to find that row. Just as at school we learnt x is how far you go to the right and y is how far up  down you go. Remember that y goes IN the opposite direction here.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok ok, think I'm getting it.

[col, row]

col = first pt
row = second pt

col = how many over
row = how many down

so example [3,2]
3 is the column
2 is the row

therefore:

3 over 2 down
or better
3 column over 2 row down




[0,?]
[0,?]
[0,?]
[0,?]
Here all those zeros represent the column #'s

and

[?,1] [?,1] [?,1] [?,1]

Here all those ones represent the row #'s

so all over those pixels each one is really saying in order "this many over this many down, this many over this many down, this many over this many down..." as it loops through the nested for loop.

Is this how I should think of it executing through the code [a,b] where 'a' is the col (how many over) and 'b' is the row (how many down)?

Thanks!





 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:so example [3,2]
3 is the column
2 is the row
No. First is what is in between {...}, second what is in between {{...}} or first rows, second columns. It depends how you look at it.

or
So, A[3][2] = ?
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Justin Robbins wrote:so example [3,2]
3 is the column
2 is the row
No. First is what is in between {...}, second what is in between {{...}} or first rows, second columns. It depends how you look at it.

or
So, A[3][2] = ?


Aren't those arrays? I haven't covered them yet. I am not understanding what you mean by what's in between {...} Is there another way you could explain that I am very new to Java.

Thanks


EDIT: I am thinking that {} means the loops so how row and col get changed. But isn't it still [col,row] so {some stuff happens in here} then col and row get changed and then [col,row] col shifts it to the right a certain number and row shifts in down a certain amount dependant on what happens in {}?
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, I totally misread topic. Today discovered multiple questions about array of arrays, so that just stuck in my head for a while. Really sorry. Please ignore my previous post.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . . "this many over this many down, this many over this many down, this many over this many down..." . . .
yesyesyesyesyesyesyes
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wonderful! finally seem to be making a little progress. So [col,row] mean this many over and this many down.
So does that mean column is the same thing as width, since width spans right or left. And is row the same as height, since it spans up and down?

So should I think of when the nested for loop is cycling through as starting from 0,0?

Something like this comes to mind with the width and height function defined:

....__w_i_d_t_h___\
h| [0,0].............. /
e|
i|
g|
h|
t|
\|/


Does everything start from that initial top left 0,0 point when making these iterations go through?

Making the diamond shape I am thinking I will have to take the width and divide it by 2 so that gives me the middle pt, so the col will be the midpt and the row needs to be '0'. So the middle box gets colored. Then the next iteration will have to add on three to width so width+3 to color in three boxes and each row needs to be set accordingly. Not even sure how to approach this at all....now that I think I understand what row and column are it's the nested loops and the general process of creating a diamond grid shape that's confusing.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . So does that mean column is the same thing as width, since width spans right or left. And is row the same as height, since it spans up and down? . . .
Width means how many columns you can fit in and height means how many rows you can fill in. One a GUI you would have to consider each column as 1px wide. You usually call the column number x or xPos and the row number y or yPos.
You start your iterations where you started your loop; if you have
for (x = 0;...) ... for (y = 0;...) ...
then you will start at the top left at (0, 0).

Drawing diamonds can be thought of as dividing the area into four quarters and drawing a triangle in each, so yes, you would halve the height and width. You can think of midpoint + i and midpoint − i and colour both those points the same colour. With a bit of such jiggery‑pokery you can colour four points in one iteration of the loop, so you only need one loop. But that needs time to implement.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so width is the number of columns we can fill in and height is the number rows we can fill in.
So then width is like the x-axis and col are like the individual spots on the x-axis, and height is like the y-axis and row is like the individual spots on the y-axis..

So that's why we would make a for loop like:



The default name we give it is col, but it could be anything, right? but width will be re-assigned an actual value once the user types in a value into the console.
Once that happens width gets a value and then begins cycling through until proven false, thus it will give each value that it takes to get to width -1 because it's < (less than)
So if width is '7' then it will cycle through like: 0, 1, 2, 3, 4, 5, 6 and stop.

And a for loop like:

Similarly, since this code defined height and width to be the same if height is 7 then...
this for loop will cycle through: 0, 1, 2, 3, 4, 5, 6 and stop.

How exactly is having all numbers starting from 0 to desired number going to be useful? So we know that both height and width must go through 7 iterations 0, 1, 2, 3, 4, 5, 6 until their for-loops are proven false. How will that help make the diamond pattern?




 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:Ok, so width is the number of columns we can fill in and height is the number rows we can fill in.
So then width is like the x-axis and col are like the individual spots on the x-axis, and height is like the y-axis and row is like the individual spots on the y-axis...

It seems to me like you're overthinking this.

Suppose I gave you two packs of cards, one with red backs, and one with black backs, and I told you to "paint" a red diamond in a black square?

What would YOU do? How would you arrange the cards and why? And what other piece(s) of information would I need to give you to make sure you do it correctly?

My suggestion: Try it for yourself - and make sure you have some paper handy for notes when you do. Sometimes actually doing something can help you work out things that you can't when you're simply looking at code.

HIH

Winston
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
It seems to me like you're overthinking this.

Suppose I gave you two packs of cards, one with red backs, and one with black backs, and I told you to "paint" a red diamond in a black square?

What would YOU do? How would you arrange the cards and why? And what other piece(s) of information would I need to give you to make sure you do it correctly?

My suggestion: Try it for yourself - and make sure you have some paper handy for notes when you do. Sometimes actually doing something can help you work out things that you can't when you're simply looking at code.

HIH

Winston


Possibly. I know what I want to come out:

....*
...***
..*****
.*******
*********
.*******
..*****
...***
....*

something like that
Then I'd have the pattern:
4, 1
3, 3
2, 5
1, 7
0, 9
1, 7
2, 5
3, 3
4, 1
But how do I generalize this in a code form so where any odd number like 7, 19 or something will create a diamond grid pattern. It's hard wrapping my head around how to do this.
4 white space, then 1 colored box
3 white space, then 3 colored boxs
2 white space, then 5 colored boxs
and so on...I don't know how to implment this pattern.
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did this challenge on CodeWars and didn't use arrays at all. That is to say, try not to say, "How could I use arrays to..." (fill in the blank) and instead say, "How would I do this without programming at all?" Often the answer will come from a completely different area.
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I'd have the pattern:
4, 1
3, 3
2, 5
1, 7
0, 9
1, 7
2, 5
3, 3
4, 1

For the top half

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I did this challenge on CodeWars and didn't use arrays at all. That is to say, try not to say, "How could I use arrays to..." (fill in the blank) and instead say, "How would I do this without programming at all?" Often the answer will come from a completely different area.


Could you explain a little more. I am new to Java don't really understand arrays. And no clue on how to create this diamond shape with the grid and colors.
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:
Knute Snortum wrote:I did this challenge on CodeWars and didn't use arrays at all. That is to say, try not to say, "How could I use arrays to..." (fill in the blank) and instead say, "How would I do this without programming at all?" Often the answer will come from a completely different area.


Could you explain a little more. I am new to Java don't really understand arrays. And no clue on how to create this diamond shape with the grid and colors.

Temporarily, I suggest that you set aside the grid / arrays / colors and create a very simple program to print out a diamond shape using spaces and asterisks. It will be a lot easier to debug that first. Then take what you've learned and apply it to the grid / arrays / colors.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!