Thread: Multidimensional Arrays?

  1. #1
    Registered User CreatedByShadow's Avatar
    Join Date
    Jan 2006
    Posts
    24

    Multidimensional Arrays?

    What are multidimensional arrays used for? Until recently, I haven't found much use for them until I read something about designing a grid and it says use a multidimensional array with the sizes of the grid to your liking (i.e. if you want the grid to be 20x40, than it'll look like this: int grid[20][40]).

    I dont really understand them and how to use them.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your screen is a 2D array of pixels / characters.
    A map for a game could be a 2D array.
    Matrices are often stored in a 2D array.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    an array of an array of pointers is also another possible application.. like extracting data from a multiline edit box.. have the first dimension of the array stand for each line of text.. and have the second dimension stand for the line of text itself..

    Code:
    cout << text[2][0] ;       // would print out the second line of text...   until it reaches the '\0'  null terminator
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem
    Your screen is a 2D array of pixels / characters.
    A map for a game could be a 2D array.
    Matrices are often stored in a 2D array.
    Unless you used specialised storage techniques, a matrix is usually a 2 (or more) dimensional array

    A map of the planets in a solar system would probably be three dimensional, as would a map of solar systems in a galaxy.

    If your program is to support display on multiple screens, it may use an array of screens. If each screen is a 2D array of pixels/characters than an array of screens could also be represented as a 3D array of pixels/characters.

  5. #5
    Registered User CreatedByShadow's Avatar
    Join Date
    Jan 2006
    Posts
    24
    Based on your replies, this is what I got.

    Multidimensional arrays can be used for text editing.
    Code:
    int text[4][20] = "16";
    Wouldnt that statment assign 16 to position 19 at line 4?

    And if I wanted to created a 2D map grid the size of 40x40, i would make it like below:
    Code:
    int map_grid[40][40];
    I think i grasp the basics of these. But isn't there a tutorial that explains these a bit more in-depth than the ones here?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by CreatedByShadow
    Code:
    int text[4][20] = "16";
    Wouldnt that statment assign 16 to position 19 at line 4?
    No. That is a syntax error.

    Do you have anything in particular in mind? It's much easier than dreaming up something that you care nothing about.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User CreatedByShadow's Avatar
    Join Date
    Jan 2006
    Posts
    24
    I want to know how to work with them. As of now, im completely lost at this...

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
       int value[4][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} };
       for ( int i = 0; i < 4; ++i )
       {
          for ( int j = 0; j < 3; ++j )
          {
             std::cout << std::setw(2) << value[i][j] << ' ';
          }
          std::cout << '\n';
       }
       return 0;
    } 
    
    /* my output
     1  2  3 
     4  5  6 
     7  8  9 
    10 11 12 
    */
    But it is always beneficial to have a problem to solve before you go looking for solutions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  3. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  4. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM
  5. Adding multidimensional arrays
    By newbie2C++ in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2001, 04:05 PM