Thread: just checking i have this 2d array right

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    just checking i have this 2d array right

    as the title says have i got this finally into my thick skull!

    Code:
                               // element [0][0] here
                               // |
                              //  v
    int illegal_squares[8][8] = {{1,0,1,0,1,0,1,0},
                                 {0,1,0,1,0,1,0,1},
                                 {1,0,1,0,1,0,1,0},
                                 {0,1,0,1,0,1,0,1},
                                 {1,0,1,0,1,0,1,0},
                                 {0,1,0,1,0,1,0,1},
                                 {1,0,1,0,1,0,1,0},
                                 {0,1,0,1,0,1,0,1}};
                                             // ^
                                             // |
                                            //  element[7][7] here
    is this correct or have i messed up again
    many thanks
    coop
    Last edited by cooper1200; 04-30-2019 at 05:26 AM.

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Yes, that's correct. It might be less confusing to use a non-square matrix, so you can see which dimension is which:
    Code:
    int arr[4][2] {
        {0, 0}, // [0][0], [0][1]
        {0, 0}, // [1][0], [1][1]
        {0, 0}, // [2][0], [2][1]
        {0, 0}  // [3][0], [3][1]
    };

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    thanks that's a good idea i keep thinking i have mine wrong no matter how many rude comments i leave myself
    coop

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Just remember to index it with illegal_squares[y][x], not illegal_square[x][y].

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Quote Originally Posted by gaxio
    Just remember to index it with illegal_squares[y][x], not illegal_square[x][y].
    That just depends on what exactly are x and y meant to be used for. illegal_square[x][y] can be perfectly fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 8 Bit (Binary) array checking in c
    By thannara123 in forum C Programming
    Replies: 8
    Last Post: 08-04-2015, 10:29 AM
  2. Checking char of array
    By mrapoc in forum C Programming
    Replies: 4
    Last Post: 04-25-2012, 08:22 AM
  3. checking an array for a value
    By nowber in forum C Programming
    Replies: 2
    Last Post: 11-14-2009, 09:36 AM
  4. Checking 2d array...
    By ninety3gd in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2009, 11:15 PM
  5. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM

Tags for this Thread