# Working with 2D arrays

Can someone help me find out what I am doing wrong? Why is it not showing my green square?

****Move it as the first if statement that is checked.( I moved to to the beginning of draw….) The other two statements should be else if statements. (not sure what to change)

``````int[][] grid; //creates a 2D array called grid

int sqSize = 50;  // used for location and size of squares on grid
int gridSize = 500; //  width and height size
int n = gridSize/sqSize;  // number of rows and columns
int row=5;
int col=5;

public void setup(){
size(500, 500);  //size the frame
noLoop();  // make it not loop
grid = new int[n][n];  //initializes the grid
}

void draw() {
int ran = (int)(Math.random() * n); // choose a random number for index in the grid
int ran2 = (int)(Math.random() * n); // choose a random number for index in the grid

if((row == ran) && (col == ran) && (ran == ran2))
{
fill(0, 255, 0);
rect(row*sqSize, col*sqSize, sqSize-2, sqSize-2);

}
//loop through the rows and columns
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
stroke(0);
if(row == ran && col == ran )
{
fill(255, 0, 0);
rect(row*sqSize, col*sqSize, sqSize-2, sqSize-2);
}

else {
fill(255,255,255);
rect(row*sqSize, col*sqSize, sqSize-2, sqSize-2);
}
}
}
System.out.println(ran);

for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
stroke(0);
if(row == ran2 && col == ran2 )
{
fill(0, 0, 255);
rect(row*sqSize, col*sqSize, sqSize-2, sqSize-2);
}

}
}
System.out.println(ran2);
}
``````

Delete this

Why do you have this

I think you want to have a nested for loop and inside it the if…else if…else if… clause.

See reference for this clause. It means that only one of the ifs can be true at a time

After you have copied the code and tested it, create a second random number called ran2 that also produces a random number between 0-9. (number of rows/columns). Create an if statement using the else if structure that will fill the square with blue at the new random index location [ran2][ran2]. Add a system.out.println statement so you can see the second random number produced.

Add another if structure that will test to see if ran = col and ran = row and that ran and ran2 are the same number. If they are fill the square with the color green. Test the code by hard coding the random numbers the same. Make sure you change it back to produce a random number. You need to check two conditions: if row and col equal ran and if ran equals ran2. An example of the code is below.

if((row == ran) && (col == ran) && (ran == ran2))
{
fill(0, 255, 0);
rect(rowsqSize, colsqSize, sqSize-2, sqSize-2);

}

For the program to operate correctly, this last if structure that fills the square with green needs to be first.

Move it as the first if statement that is checked. The other two statements should be else if statements.

Run the code and fix any errors.

noLoop(); // make it not loop

this step is in there because it is in the original code from the assignment

You can easily figure it out

Gotta go

Run the code and fix any errors.

`:)`

yeah, i get that…but i need to change the other two statements to else if statements. not sure how to do that

No, move it inside the nested for-loops

Put else before each if…

So ONE nested for-loop (with 2 for-loops)
and INSIDE this an if…else if… else if… clause with the ifs you’ve got.

(Which if-clause must come first was told you.)

still can’t get it to work

`:)`