CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 7 of 7

Thread: Dynamic Matrix

  1. #1
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Dynamic Matrix

    Hello,
    i've a question: how can I get the access to all the elements of a dynamic matrix?
    I create a dynamic matrix in this way:
    Cout<<"insert matrix dimensions";
    cin>>n;
    int ** matrix= new int * [n];
    for (int i=0;i<n;i++)
    matrix [n]= new int [n];

    //but when I try to initialize the element in this way:
    for (int i=0;i<n;i++)
    for (int J=0;j<n;j++)
    matrix [i]*[j]=0;

    the program gives me an error, how could I insert and modify all the location of the dynamic matrix?

    Thanks a lot for attention

  2. #2
    Lindley is offline Elite Member Power Poster Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+)
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,861

    Re: Dynamic Matrix

    matrix[i][j] = 0;

    Note, you'll need to clean up that memory. Consider defining your matrix as a std::vector<std::vector<int>> instead, or with a boost::multi_array<int, 2>.

  3. #3
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: Dynamic Matrix

    yes, but in my code:

    #include <iostream>
    using namespace std;

    int main()
    {
    int n;
    cout<<"Inserisci le dimensioni della matrice NxN\n";
    cin>>n;
    int **matrix = new int * [n];

    for (int i=0;i<n;i++)
    matrix[n]=new int [n];

    for (int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    ------> matrix [i][j]=0;

    for (int i=0;i<n;i++)
    {
    for(int j=0;j<n;j++)
    cout<<matrix [i][j];
    cout<<endl;
    }
    delete []matrix[n];
    return 0;
    }

    the program returns:"Program received signal: “EXC_BAD_ACCESS”.
    sharedlibrary apply-load-rules all
    (gdb) "

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,507

    Re: Dynamic Matrix

    1. You have:

    Code:
    for (int i=0;i<n;i++)
      matrix[n]=new int [n];  // should be "i" not "n"

    2. your delete code is not correct

    Code:
        for (int i=0; i<n; ++i)
        {
           delete [] matrix[i];
        }
        
        delete [] matrix;
    3. There are more efficient ways to allocate the matrix. A search on codeguru
    should yield a better method.

  5. #5
    Lindley is offline Elite Member Power Poster Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+)
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,861

    Re: Dynamic Matrix

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
            int n;
            cout<<"Inserisci le dimensioni della matrice NxN\n";
            cin>>n;
            int **matrix = new int * [n];
    	
            for (int i=0;i<n;i++)
                    matrix[n]=new int [n];
            
            for (int i=0;i<n;i++)
                    for(int j=0;j<n;j++)
            ------>	matrix [i][j]=0;
            
            for (int i=0;i<n;i++)
            {
                    for(int j=0;j<n;j++)
     	                cout<<matrix [i][j];
                    cout<<endl;
            }
            delete []matrix[n];
            return 0;
    }
    I have highlighted the lines which are wrong.

  6. #6
    Join Date
    Apr 1999
    Posts
    26,740

    Re: Dynamic Matrix

    Quote Originally Posted by Falko-tux View Post
    yes, but in my code: ...
    Please take a look at this alternative to your code:
    Code:
    int main()
    {
       int n;
       cout<<"Inserisci le dimensioni della matrice NxN\n";
       cin>>n;
    
       // Create row pointers
       int **matrix = new int * [n];
    
       // Create the data for the matrix in one call.
       int *matrix_data = new int[n*n];
    
       // Create n*n matrix
       for (int i=0; i < n; i++, matrix_data+=n)
          matrix[i] = matrix_data;  // point row pointers to matrix data
    
       // delete n*n matrix
        delete []matrix[0];  // delete the beginning of the matrix data
        delete [] matrix;     // delete the row pointers.
    }
    Compare this with your code. Hopefully you see the major difference in how the matrix is created.

    In your version, you are calling new[]/delete[] n+1 times, while in the code above, there are only 2 calls, regardless of the size of the matrix. If n were 1000, you would be calling the allocator 1001 times, while the version above only calls it 2 times.

    Basically this method:

    1) saves time (since the allocator is only being called twice for new[] and delete[]),
    2) Memory fragmentation is minimized
    3) The resulting matrix has the same layout as a regular 2 dimensional array, in other words, the data is contiguous.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: Dynamic Matrix

    It works! Thank you, and thanks for the new way of creating dynamic matrix (more efficient). This is the best programming forum i've ever seen. Thank you again

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width