## Dynamic allocation of matrix?

Write here if you have problems with your C++ source code

Moderator: Board moderators

andmej
Experienced poster
Posts: 158
Joined: Sun Feb 04, 2007 7:45 pm
Location: Medellin, Colombia

### Dynamic allocation of matrix?

Hello,

I know I can allocate an array dynamically like this:

Code: Select all

``````#include <iostream>
int * array;

int main(){
int n;
cin >> n;

array = new int[n];
//~ Use the array here...
delete [] array;
return 0;
}
``````
Do you know how can I allocate a matrix of size n*n in a similar fashion? That is, I don't know how big the matrix will be until runtime and need to read the size.

I'd appreciate your help. Thanks in advance.
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.

Are you dreaming right now?
http://www.dreamviews.com

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

### Re: Dynamic allocation of matrix?

You can allocate an array of size n*n, and do extra arithmetic (which you can encapsulate in macros or classes) to access its elements:

Code: Select all

``````int *matrix = new int[n*n];
#define MATRIX(i, j) matrix[(i)*n + (j)]

MATRIX(2, 3) = 42;
cout << MATRIX(2, 3) << endl;

delete[] matrix;
``````
Or allocate an array of int*, and initialize each member with a pointer to another allocated array:

Code: Select all

``````int *matrix = new int*[n];
for (int i = 0; i < n; i++) matrix[i] = new int[n];
...
matrix[2][3] = 42;
...

for (int i = 0; i < n; i++) delete[] matrix[i];
delete[] matrix;
``````
And with STL this solution is even shorter:

Code: Select all

``````vector<vector<int> > matrix(n, vector<int>(n, 0));
matrix[2][3] = 42;
cout << matrix[2][3] << endl;
...
``````

andmej
Experienced poster
Posts: 158
Joined: Sun Feb 04, 2007 7:45 pm
Location: Medellin, Colombia

### Re: Dynamic allocation of matrix?

Is the STL solution slower than the other solutions?
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.

Are you dreaming right now?
http://www.dreamviews.com

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

### Re: Dynamic allocation of matrix?

Yes, unfortunately it's quite a bit slower, maybe about 1.5-3 times slower than other codes, but that heavily depends on the compiler and optimization flags.

Megov
New poster
Posts: 3
Joined: Sun Apr 06, 2008 8:20 pm

### Re: Dynamic allocation of matrix?

By the way there is a mistake in second example of code : instead of "int *matrix" you need to write "int **matrix"

shiggy
New poster
Posts: 3
Joined: Sat Jan 19, 2008 5:28 am

### Re: Dynamic allocation of matrix?

You could write a function like:

Code: Select all

``````int** create_2d_array(int rows, int cols)
{
int** array = new int*[rows];

for (int i = 0; i < rows; i++)
array[i] = new int[cols];

return array;
}

//...

int** array = create_2d_array(5,5);
array[0][0] = 1;
array[3][4] = 1;
//etc
``````
Replacing int with whatever type you need