# Thread: Fun with arrays..

1. ## Fun with arrays..

I'm using a 2d array to make a kind of like a co-ordinate geometry effect. Where my array is oject[r][c], so in effect like (x,y) in geometry.

I've been hitting at this for ages but im totally stumped.. say i wanted to print a character at a particular point on the "plane", how would i do this?

I've been trying this:

Code:
```      if ( object[r][c] == EMPTY ) {
printf( "." );
}```
i think i got the wrong idea.. thanks

2. Originally Posted by icedtea
I'm using a 2d array to make a kind of like a co-ordinate geometry effect. Where my array is oject[r][c], so in effect like (x,y) in geometry.

I've been hitting at this for ages but im totally stumped.. say i wanted to print a character at a particular point on the "plane", how would i do this?

I've been trying this:

Code:
```      if ( object[r][c] == EMPTY ) {
printf( "." );
}```
i think i got the wrong idea.. thanks
You've got the right idea, just put your code above (and thanks for using code tags, btw), in the middle of a for loop:
Code:
```for( row = 0; rowi < maxrow; row++)
for( col = 0; col < maxcol; col++) {
if ( object[row][col] == EMPTY ) {
printf( "." );
}
}```

In other words, you're scanning over the plane or grid. Think of a chessboard. If you have a "mailbox" type of a board representation (like my program does), you need to go over every square on the board, just this way, to find out which piece can move, where.

3. ahh ok thanks heaps adak.

But what happens if im in a position where i don't need to use the if statement. (Just curious question) How would i print a charactar at a particular location of object[r][c]?

4. Assuming "object" is a 2D character array (for the sake of example).

Code:
```for( row = 0; row < maxrow; row++)
{
for( col = 0; col < maxcol; col++)
{
if ( object[row][col] == EMPTY )
{
printf( "." );
}
else
{
printf("%c", object[row][col]);
}
}
printf("\n");
}```

5. Or you could set object[row][col] beforehand. (But that doesn't work too well if you're only setting object[row][col] temporarily.)

6. Originally Posted by icedtea
ahh ok thanks heaps adak.

But what happens if im in a position where i don't need to use the if statement. (Just curious question) How would i print a charactar at a particular location of object[r][c]?
You would normally have defines for the program, like say your top row was actually on the 3rd row down on the screen.

So now you print everything in your grid relative to TopRow, which == 3. So the row you'll print to will be TopRow + row, and same with your columns - they'll all be printed up as LeftSide + column, because you might want to locate the board or grid, someplace else besides right on the very leftmost side of the screen.

To locate the right x and y points on the screen, you can use Goto(x,y) in Windows API, an example of which is located just now on the forum, not far below your own post.

You may need several if statements or a switch statement with several choices, to get all the char's you want printed on your grid.

They all go inside your outer scanning for loop, mentioned earlier.