Community discussion forum

2D Dynamic Arrays in C++

Tags:
  • 5 years ago

    Hi, I've been searching for a way to declare and use 2Dimensional and Dynamic Arrays.


    I tryed using malloc:


    I built a 1D Dynamic Array;
    Created a Struct with this 1D Array inside
    then i created another 1D Dynamic Array containing these Structs.


    Then I tryed to access the data like this: arrayOfStructs.arrayInsideStruct[j], but it didnot work.


    Could anyone help me?

  • 5 years ago
    slap some source code here. that always helps.

    ohh, and dont bother with malloc. that was replaced by the 'new' operator in c++.

    by dynamic array, do you mean an array of variable size ? if so, you will need to use a vector class.

    anyways... try somthing like this...

    Code:

    #include <iostream>
    using namespace std;

    int main() {
       
       const int MAX_X=5, MAX_Y=10;
       int x,y;
       
       int my2DArray[MAX_X][MAX_Y];
       
       for (y=0; y<MAX_Y; y++) {
           for (x=0;x<MAX_X; x++) {
               my2DArray[x][y] = x + y;
           }
       }
       
       for (y=0; y<MAX_Y; y++) {
           for (x=0;x<MAX_X; x++) {
               cout << my2DArray[x][y] << ' ';
           }
           cout << endl;
       }
       return 0;
    }


    an example i wrote on how 2d arrays work.
  • 5 years ago

    so here is ur solution of dynamic memory allocation
    reply me soon ..........................




    include<iostream.h>



    void main()
    {
    int  **a,m,n;  // double pointer for 2d arry


    cout<<" \n enter the number of rows and coloums for matrix:";
    cin>>m>>n;


    a=new int *[m];    // dynamic allocation of pointers array for rows
    for(int i=0;i<m;i++)
    a=new int [n];     // dynamic allocation of coloums for each rows


    cout<<"\n enter the elements:";
    for(i=0;i<m;i++)
      for(int j=0;j<n;j++)
         cin>>a[j];


    cout<<"\n the elements r:";
    for(i=0;i<m;i++)
    {
      for(int j=0;j<n;j++)
         cout<<a[j]<<"  ";
    cout<<"\n";
    }


    }

  • 4 years ago

    Hey, i'm new to C++ and was doing a school project on 2D arrays. the 2nd code posted here doesn't work


    a=new int [n];     // dynamic allocation of coloums for each rows


    here or somewhere near here the compiler says that '=' cannot convert from 'int' to 'int*'


    and there's another place that has problems with type conversion


  • 4 years ago

    Hi'
    I think vector<vector<Type>>  dynamic2darray   may work.
    Have a try!

  • 4 years ago

    Hi!
    to dynamically allocate a 2D array, i recently used the following code. This doesn't directly creates a 2-d array. It actually creates a 1-D array of size m*n (where m and n are the num of rows n cols in the reqd 2-d array), then uses an addressing technique to access the i,j element:


    float fArray;      //declares an int pointer for array
    int m,n;           //the size of array
    cin>>m>>n;
    fArray = new float[m
    n];   //allocates memory for 1-d array of size m*m



    now, u can access the [j] element by the following expression:


    ((float *)fArray + (im) + j)


    so, everywhere, u want to refer to fArray[j], just write the above expression.


    Enjoy!

  • 1 year ago

    Hi, I am a newbie to this forum and this is my first post.  I am learning C++ with a book "Financial Modeling Using C++".  I encountered some compile time errors for the source codes included there, in particular multidimensional dynamic array examples.

    Since the publisher's website does not indicate any correction information and the author's contact information is not available, I would like to seek an advice here.

    Please see the following code:

    // Example 11.8: 3-D dynamic array

    #include <iostream>

    using namespace std;

    int main()

    {

    int i, j, k, m, dim1, dim2, dim3;cout << "Enter array dimensions: ";

    cin >> dim1 >> dim2 >> dim3;

    int (*pa)[dim2][dim3] = new int[dim1][dim2][dim3];  // error C2057

    // initialize array

    m = 0;

    for (i = 0; i < dim1; i++)

    for (j = 0; j < dim2; j++)

    for (k = 0; k < dim3; k++)

    pa[i][j][k] = m++;

    cout <<
    "Value of element (0,0,0): " << pa[0][0][0] << endl;

    cout << "Value of element (1,2,1): " << pa[1][2][1] << endl;

    cout << "Value of last element: " << pa[dim1 - 1][dim2 - 1][dim3 - 1];

    cout << "\n\n";

    delete[] pa;

    // Wait for the user to read the output on the console

    system("PAUSE");return 0;

    }

    The first message of compile time errors I had is "error C2057: expected constant expression" regarding the declaration of pointer variable.

    Could you kindly provide me with any suggestion for correction?  Since I am still a novice, I would appreciate a suggestion without use of advanced concepts in C++.  Many thanks in advance.

  • 1 year ago


    #include <iostream>

    using namespace std;

    int main()
    {

    int i, j, k, m, dim1, dim2, dim3;cout << "Enter array dimensions: ";
    cin >> dim1 >> dim2 >> dim3;

    //int (*pa)[dim2][dim3] = new int[dim1][dim2][dim3];  // error C2057   [old code segment]

    /*new rectified lines of code start*/

    int ***pa = new int **[dim1]; 

    // initialize array

    for (i = 0; i < dim1; i++) {
     pa[i]= new int *[dim2];
    }

    for (i = 0; i < dim1; i++) {
     for (j = 0; j < dim2; j++) {
      pa[i][j]= new int[dim3];
     }
    }

    /*new rectified lines of code end*/

    m = 0;
    for (i = 0; i < dim1; i++)
    for (j = 0; j < dim2; j++)

    for (k = 0; k < dim3; k++)

    pa[i][j][k] = m++;

     


    cout << "Value of element (0,0,0): " << pa[0][0][0] << endl;
    cout << "Value of element (1,2,1): " << pa[1][2][1] << endl;   //[dim1,dim2,dim3 expected  values- >= 1,2,1]

    cout << "Value of last element: " << pa[dim1 - 1][dim2 - 1][dim3 - 1];
    cout << "\n\n";

    delete[] pa;
    // Wait for the user to read the output on the console

    system("PAUSE");return 0;
    }

  • 1 year ago

    Thank you very much, sidd.biswas!!!  I feel I need to learn how to use multiple dereference operators "*" by refering to some material other than the book I mentioned above, which lacks proper explanations.

  • 1 year ago
  • 1 year ago

    According to your title, you are writing in C++. I suggest you use vectors (std::vector). These are dynamic.

    If the data you want to save in the arrays needs to be indexed by name, use a map instead (std::map)

    For more info, look here: http://www.cplusplus.com/

    Click on the "STL: Standard Template Library" link.

    Good luck.
    Alexis

  • 1 year ago
    try this code for dynamic memory allocation for two dimensional array . this program is dispaly random number #include #include using namespace std; void main() { int **r,n; int i,j; cout <<"Enter a Number"; cin >> n; r = new int *[n]; for(int i=0;i
  • 1 year ago
    Is anyone interested in working on Graphics Technologies within the Windows Platform? -Ryan Phillips engineerfinder@live.com
  • 1 year ago
    yes dear why not but what is work

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback