multidimensional array question

This is a discussion on multidimensional array question within the C++ Programming forums, part of the General Programming Boards category; How do you make a multidimensional array of chars on the free store and how do you modifiy the values ...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    multidimensional array question

    How do you make a multidimensional array of chars on the free store and how do you modifiy the values of each of the chars in the array?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lookup new and delete.

    Post what you're tried if you get stuck.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    When I try and create a multidimensional array of chars on the free store I do it like this:
    Code:
    char * board = new char[5][6];
    This is excactly the way my book does it, only it's a one dimensional array in the examples. With the above I get the error 'cannot convert "char (*)[6]" to "char *". I have no idea what that means.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How about:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main(void)
    {
      const int ROW = 5;
      const int COL = 6;
      int **array = new int*[ROW];
      for (int i = 0; i < ROW; i++)
        array[i] = new int[COL];
        
      for (int i = 0; i < ROW; i++)
        for (int j = 0; j < COL; j++)
          array[i][j] = i*10+j;
          
      for (int i = 0; i < ROW; i++)
        for (int j = 0; j < COL; j++)
          std::cout <<"array["<<i<<"]["<<j<<"] = " 
                    <<std::setw(2)<<std::setfill('0')
                    <<array[i][j] <<std::endl;
      
      for (int i = 0; i < ROW; i++)
        delete [] array[i];
      
      delete [] array;
      return(0);
    }
    
    /*
    Output
    
    array[0][0] = 00
    array[0][1] = 01
    array[0][2] = 02
    array[0][3] = 03
    array[0][4] = 04
    array[0][5] = 05
    array[1][0] = 10
    array[1][1] = 11
    array[1][2] = 12
    array[1][3] = 13
    array[1][4] = 14
    array[1][5] = 15
    array[2][0] = 20
    array[2][1] = 21
    array[2][2] = 22
    array[2][3] = 23
    array[2][4] = 24
    array[2][5] = 25
    array[3][0] = 30
    array[3][1] = 31
    array[3][2] = 32
    array[3][3] = 33
    array[3][4] = 34
    array[3][5] = 35
    array[4][0] = 40
    array[4][1] = 41
    array[4][2] = 42
    array[4][3] = 43
    array[4][4] = 44
    array[4][5] = 45
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    thanks for your help.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    array[0][0] = 00
    array[0][1] = 01
    array[0][2] = 02
    array[0][3] = 03
    array[0][4] = 04
    array[0][5] = 05
    array[1][0] = 10
    array[1][1] = 11
    array[1][2] = 12
    array[1][3] = 13
    array[1][4] = 14
    array[1][5] = 15
    array[2][0] = 20
    array[2][1] = 21
    array[2][2] = 22
    array[2][3] = 23
    array[2][4] = 24
    array[2][5] = 25
    array[3][0] = 30
    array[3][1] = 31
    array[3][2] = 32
    array[3][3] = 33
    array[3][4] = 34
    array[3][5] = 35
    array[4][0] = 40
    array[4][1] = 41
    array[4][2] = 42
    array[4][3] = 43
    array[4][4] = 44
    array[4][5] = 45
    Yeesh...
    Code:
    for(int a=0;a<6;a++)
    {
      for(int b=0;b<6;b++)
      {
        array[a][b] = a*10+b;
      }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Magos: Your point being??!! That's the same code I used
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Ouch! I didn't see the quotes, that it was the output. Thought you set the values using those 36 lines of code. Should've known you knew better .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I almost posted something about that... then i noticed the comments...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: multidimensional array question

    Originally posted by Mr_Jack
    How do you make a multidimensional array of chars on the free store and how do you modifiy the values of each of the chars in the array?
    The others have told you how to accomplish that -- I will tell a bit about how *not* to accomplish this :P

    Dynamic arrays tend to be a "problem area" of code, and dynamic arrays OF dynamic arrays are even worse. It's hard to make them exception-safe, it's impossible to use them as return values with 100% safety, etc.

    Instead, why not use an STL vector of vectors, or vector of strings?

    E.g.:

    Code:
    // Some constants
    const int nRows = 5;
    const int nCols = 11;
    
    //Typedefs to save our fingers and improve readability
    typedef std::vector<char> cArrayRow;
    typedef std::vector<cArrayRow> cArray;
    
    // Make an array initially filled with 'A':
    
    cArray myArray(nRows,cArrayRow(nCols,'A'));
    That is it. One line to allocate all the memory, NO lines needed to deallocate (it happens automatically when myArray leaves scope). And it's exception safe. AND you can return an array from a function with 100% certainty that you will not ever leak memory.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Another variation on the same theme:

    Code:
    #include <iostream>
    using namespace std;
    
    void populateArray(int* m, int numOfRows, int numOfColumns);
    void displayArray(int* m, int numOfRows, int numOfColumns);
    
    int main()
    {
        unsigned NUM_OF_ROWS = 0;
        unsigned NUM_OF_COLUMNS = 0;
        
        cout << "Enter the number of rows: " << flush;
        cin >> NUM_OF_ROWS;
        cout << "Enter the number of columns: " << flush;
        cin >> NUM_OF_COLUMNS;
    
        int* matrix = new int[NUM_OF_ROWS * NUM_OF_COLUMNS];
        populateArray(matrix, NUM_OF_ROWS, NUM_OF_COLUMNS);
        displayArray(matrix, NUM_OF_ROWS, NUM_OF_COLUMNS);
        delete [] matrix;
    
        return 0;
    }
    
    void populateArray(int* m, int numOfRows, int numOfColumns)
    {
        for(int row=0, value=0; row < numOfRows; row++){
            for(int column=0; column < numOfColumns; column++){
                m[(row*numOfColumns)+column] = value++;
            }
        }
    }
    
    void displayArray(int* m, int numOfRows, int numOfColumns)
    {
        for(int row=0; row < numOfRows; row++){
            for(int column=0; column < numOfColumns; column++){
                cout << m[(row*numOfColumns)+column] << " ";
            }
        }
        endl(cout);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 06:48 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  4. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 03:58 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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