Thread: Constructors for creating 2d arrays

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Constructors for creating 2d arrays

    Hey guys, been a while since ive posted on here.......had a break to take some exams, ANYWAY lets get straight to my problem

    I'm making a small minesweeper game, and i want the player to be able to select the size of the minefield, but the constructor is turning out to be a bit of a problem. The best i could come up with it below, but ill need to define i and j as ints, but i'd rather not create another two variables, is there a better way? thanks

    Code:
    int m_Height;
    int m_Width;
    Field(char field[int i][int j]);           // constructor
    char m_MineField[][];                      // minefield itself
    Code:
    Field(char field[i][j]): m_MineField[m_Width][m_Height](field[i][j])

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    int m_Height;
    int m_Width;
    char **m_MineField;  
    m_MineField = new char*[m_Height];
    for(int i = 0; i < m+Height; ++i)
       m_MineField = new char[m_Width];
    look up declaring and using multidimensional arrays using dynamic memory for further details.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use a two dimensional vector instead. It is easy to size to whatever size the user specifies.

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Oh didnt know you could use a 2d vector, thanks for that Daved.

    Before anyone calls me stupid, im learning incase you couldnt guess :P

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can also use a templated grid structure/class:

    Code:
    template<int height=5,int width=5>
    struct grid
    {
        int data[height][width];
        grid()
        {
            // Initialize the grid
        }
        int GetWidth() const
        {
            return width;
        }
        int GetHeight() const
        {
            return height;
        }
        // Other member functions...
    };
    
    ...
    
    grid<> minefield1;       // Creates a default 5x5 minefield
    grid<10,20> minefield2;  // Creates a 10x20 minefield
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    whoa i havent got onto templates yet, one step at a time, lol, cheers anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  3. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  4. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  5. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM