Multidimensional arrays as Matrices

This is a discussion on Multidimensional arrays as Matrices within the C++ Programming forums, part of the General Programming Boards category; As I understand a multidimensional array looks like this: Code: [][][][][] [][][][][] [][][][][] [][][][][] [][][][][] I can either have chars, ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Question Multidimensional arrays as Matrices

    As I understand a multidimensional array looks like this:
    Code:
    [][][][][]
    [][][][][]
    [][][][][]
    [][][][][]
    [][][][][]
    I can either have chars, integers or whatever in the array. However, I want to be able to store coordinates in this array. Something like: (2,4)

    Any ideas? Or anything else I could use for this?

  2. #2
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Learn about structures...
    Code:
       struct  coord
       { 
            int x,y;
        }
     
    ....
    
    coord Map[12][12][12];

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    thanks, i didn't really want to go down that line...i thought i could somehow directly add in the coordinates in a matrix like manner, since i don't read in the x and y value at the same time.

    what do you mean by this: coord Map[12][12][12];

    i could've done this by vector of a vector or some other combination but it has the same problem as before...

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    unfortunately the only way is to assign two types to a single location, would be by a structure, all you have to do is what Iamien sayd:

    Code:
    struct coord
    {
       int x, y;
    };
    
    coord Map[12][12];  //no use for 3, that would be a 3d array eh...lol
    
    //or you could do this in one statement:
    struct coord
    {
       int x, y;
    } Map[12][12];

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ya i dont really get this line:
    coord Map[12][12];

    what's the 12 for? is that the C++ assoc. container map? as in could you illustrate how you would store some coords, like (1,1), (1,2), and (1,3)

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    O the 12 would just be the size, 12 width and 12 length. So you could initialize it like so...

    to initialize each coordinate point, you could go through a loop and make them all relative to their indexing numbers.

    Code:
    for (int row = 0; row < 12; row++)
       for (int col = 0; col < 12; col++)
       {   
          Map[row][col].x = row;
          Map[row][col].y = col;
       }
    lets say you have a map called testMap and you had one coordinate on it, (0, 0) heres how you would do it:

    Code:
    coord testMap;
    testMap.x = 0;
    testMap.y = 0;
    when you normally initialize arrays you do it like so:
    Code:
    int array[2][2] = {{2, 3}, {3, 5}};
    but since its a structure you have to assign values to both x and y.
    Last edited by JoshR; 05-30-2005 at 12:09 AM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    or you mean that MAP is a 2D array of type struct...ya i guess that's what it is...12 is the worry though...don't really know how many elements ill have...is it possible to use vectors instead...

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    ya you could use anything, vectors, list, etc... then push back the value or add it to a list.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    oh ok...thanks a lot man...

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    hey one more thing...maybe you know this since you look fairly good at container classes...how can i store 3 values in a map...we can store pairs...i want to store this struct, an integer key and a character...i dont think multimap is the answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Adding multidimensional arrays
    By newbie2C++ in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2001, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21