Thread: 2D Array spreadsheet question

  1. #1
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49

    2D Array spreadsheet question

    Hey guys im writing a program that uses a speadsheet like map. I want to check when the person goes off the map but i dont know how to compare 2D arrays like I did 1Ds

    I would do this...
    Code:
    if (array [0] == 4)
    {
       Printf("This would compare the first elemet with 4")
    }
    How would you do it with a 2D array.
    Code:
    if ( array[0][0]= "I want to check both X and Y coordinates")
    ??
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well that kinda depends what you're storing in the array

    For example, if the map itself is indexed by XY position, then you might do
    Code:
    if ( x >= 0 && x < X_MAX && y >= 0 && y < Y_MAX ) {
      something = array[y][x];
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    easy

    Code:
    if(array[x][y]=={{x coordinate to compare},{y coordinate to compare}}){
    
    //statements
    
    }//end if

  4. #4
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by pktcperlc++java
    easy

    Code:
    if(array[x][y]=={{x coordinate to compare},{y coordinate to compare}}){
    
    //statements
    
    }//end if
    array[x][y] is only going to resolve to one single value (just like a single dimension array). This comparison to {x,y} isn't going to compare what you think here.

    As Salem pointed out, it depends. If you want to know if they have gone off the map, do you mean "out of the bounds of the array"? If so, then you want to check the values of x and y in array[x][y] like in Salem's example before you try and use the value of array[x][y].

    Just like array[x], array[x][y] will only resolve to a single value. The difference is
    Code:
    single dimension:
    
       0  1  2
      [3][6][9]
    
    two dimension:
    
       0  1  2
    0 [1][2][3]
    1 [4][5][6]
    2 [7][8][9]
    To access the element in the single dimension array that == 3, you would choose array[0] (or to compare it, if (array[0] == 3) ...). The only difference with 2 dimension is to access the element that == 3, you would choose array[0][2] (or to compare it to 3, if (array[0][2] == 3)...).

    With both, you have to check what you will be using to index the array before you actually use it. Accessing array[4][3] would give you an error during runtime for the two dimension array (crash the program, turn the microwave on, etc.) just the same as trying to access array[5] for that single dimension array.

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    (crash the program, turn the microwave on, etc.)
    I lose so many microwaves that way!!!
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pktcperlc++java
    easy

    Code:
    if(array[x][y]=={{x coordinate to compare},{y coordinate to compare}}){
    
    //statements
    
    }//end if
    Um, no. If x is out of bounds, or y is, then the very act of you accessing it to test for equality will very likely segfault your application.

    Furthermore, you don't usually use the comma operator in an if statement. Oh I suppose you can, but very seldom will it ever do what you thinking it's doing for you in said scenario. Furthermore, you don't use braces in if statements. But for the sake of argument, I'll assume you know that and are simply using as a grouping illustration.

    In any case, your example is wrong and would likely result in a segfault.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    Um, no. If x is out of bounds, or y is, then the very act of you accessing it to test for equality will very likely segfault your application.
    I fail to see why. as i understand it, the program accessing this array is using the array as a map.

    This woudl suggest that you already know the bounds of the array

    to see whether or not the person is on a section of the map, my comparison would work fine.

    you jsut have a nested for loop to cycle through each of the array elements

    you could set up a boolean 2d aray to state where the person is

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pktcperlc++java
    I fail to see why. as i understand it, the program accessing this array is using the array as a map.
    Well it helps if you pay attention to the thread. Let's start at the top:

    Quote Originally Posted by cdonlan
    Hey guys im writing a program that uses a speadsheet like map. I want to check when the person goes off the map but i dont know how to compare 2D arrays like I did 1Ds
    So to start, they've got a guy wandering around some place, and they want to do one test to see if he's in bounds or not. Salem posted a method which works just fine. Then there's your "solution":
    Quote Originally Posted by pktcperlc++java
    Code:
    if(array[x][y]=={{x coordinate to compare},{y coordinate to compare}}){
    
    //statements
    
    }//end if
    Now let's assume there is a function called "move", which takes an object to move, and a direction to move it. We need to, in said function, make sure we don't walk off the edge of the world. (Because that sounds similar to what they're trying to do.


    Quote Originally Posted by pktcperlc++java
    This woudl suggest that you already know the bounds of the array

    to see whether or not the person is on a section of the map, my comparison would work fine.
    No, as I've stated, it won't work. They were asking how to check to see if they've run out of bounds. Let's say, for the sake of this illustration, that they have indeed run out of bounds on the X coordinate:
    Code:
    if( array[x][y] ={{x coordinate to compare},{y coordinate to compare}}){
    Now the first thing that if statement does is access the element located at array[x][y]. If x is already out of bounds, as I have stated, you're toast.

    Quote Originally Posted by pktcperlc++java
    you jsut have a nested for loop to cycle through each of the array elements

    you could set up a boolean 2d aray to state where the person is
    Why? When there are so many better ways to do this...
    Code:
    int move( struct Person *me, int direction )
    {
        int success = 0;
        switch( direction )
        {
            case DirNorth: if( me->ycoord - 1 >= YMin ) me->ycoord--; success=1; break;
            case DirEast:  if( me->xcoord + 1 <  XMax ) me->xcoord++; success=1; break;
            case DirSouth: if( me->ycoord + 1 <  YMax ) me->ycoord++; success=1; break;
            case DirWest:  if( me->xcoord - 1 >= XMin ) me->xcoord--; success=1; break;
        }
        return success;
    }
    
    ...
    
    if( move( me, direction ) )
    {
        output( "You mosey off to the %s.\n", directions[direction].word );
    }
    else
    {
        output( "You find your way blocked.\n" );
    }
    And there we will never run out of bounds. Now I've made some assumptions that are sure to boggle your mind, so I'll just ignore your pending reply when it relates to those bits of code.

    And for one more final bit of clarity: You test the value before you try accessing that member of the array. Or rather, you don't, but you should.

    Quzah.
    Last edited by quzah; 01-24-2005 at 12:32 PM. Reason: Missed a bracket in there with all the colorization...
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  2. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM