Hi there,

What is the easiest way to declare and initialize 2d dynamic array in class in C++ ?? Please tell me the easiest way, I have used 2d dynamic array in Java. Thanks !

The easiest solution for this is to use a vector of vectors and let the library do the heavy lifting, but I assume that that isn't available to you for this assignment. If that is the case, then I'll walk you through what you'd need to do here. Declaring the array, which in this case amounts to a pointer to the underlying type, is pretty straightforward:

double *matrix;     // or `int matrix[];`

This by itself doesn't get you very far, needless to say. In order to allocate the array, you would have to allocate enough space for the rows times columns elements:

matrix = new double[rows * cols];

Now, it's important to keep track of the rows and columns, because the compiler doen't know that the block of data you just allocated is a 2D array; thus, you will probably want to wrap the array into a class:

class Matrix  
{
private:
    double data[];
    unsigned int rows, cols;

public:
    Matrix(unsigned int r, unsigned int c): data(0), rows(r), cols(c);

    double operator()(int x, int y);
};

// this would go in the implementation file
Matrix::Matrix(unsigned int r, unsigned int c)
{
    int size = rows * cols;

    data = new double[size];

    // initialize the data to zero
    for (int i = 0; i < size; i++)
    {
        data[i] = 0.0;
    }
};

double Matrix::operator()(unsigned int x, unsigned int y)
{
    if (x > rows || y > cols)
    {
        throw MatrixIndexException();  // you'll need to define this 
                                       // exception class somewhere yourself
    }
    else
    {
        return data[x * y];
    }
}

(If you really feel you need to have the array indexing in the form of array[x][y] instead of array(x, y), see this thread on cplusplus.com, though it presents a rather different approach to the solution overall.)

As for initializing the array, I'm afraid there's not much you can do about it beyond having a for() loop as show above. I would recommend making a copy c'tor and overloading the assignment operator, but that I'll leave to you.

thanks Schol-R-LEA ! I am new to programming i will work hard to understand your code. Thanks again !

What is the easiest way to declare and initialize 2d dynamic array in class in C++ ??

By far the easiest way is to use the vector class:

vector<vector<int>> foo; // 2D vector of int

Given that std::vector is essentially required to be implemented as a dynamic array, this technically meets your requirements. ;)

If you actually want to do the grunt work yourself, dynamic arrays are nothing more than pointers to sufficient memory. Since Schol-R-LEA introduced a class-based approach, here's the most common C-like (ie. low level) approach that uses nothing but pointers (which can be wrapped in a class for convenience if needed):

/*
    Allocate memory for an NxM table
*/
int **p = new int*[N];

for (int i = 0; i < N; i++) {
    p[i] = new int[M];
}

/*
    Test it out by filling it with values and displaying them
*/
for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
        p[i][j] = i * j;
    }
}

for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
        cout << p[i][j] << ' ';
    }

    cout << '\n';
}

/*
    Don't forget to release the memory when you're done.
    Just reverse the process of allocation using delete.
*/
for (int i = 0; i < N; i++) {
    delete[] p[i];
}

delete[] p;

Edited 3 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.