Thread: Fun with arrays..

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote 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.


    Adak

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    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. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    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. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you could set object[row][col] beforehand. (But that doesn't work too well if you're only setting object[row][col] temporarily.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote 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.

    Adak

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The OP could be looking for a way to put the cursor at a certain point on the screen. You can do that, but it's not portable. You'd be best off with SKeane's code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Arrays of Strings are fun
    By kippwinger in forum C++ Programming
    Replies: 9
    Last Post: 07-02-2003, 04:12 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. Pointer arrays, oo, fun! >.< URG.
    By Nakeerb in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2002, 09:20 PM