# Dynamic Memory Allocation for Two Dimensional Array

Page 1 of 1

## 5 Replies - 13533 Views - Last Post: 25 September 2011 - 12:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=248458&amp;s=ab8a9a6a4d6fb7773829964771839817&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MrJabbaNoHere

Reputation: 1
• Posts: 29
• Joined: 08-March 11

# Dynamic Memory Allocation for Two Dimensional Array

Posted 24 September 2011 - 08:43 PM

Hello everybody and thank you! I have to develop a magic square (exclusively of odd order) program. I know that if I use dynamic memory allocation instead of the obvious way, it will make my program look more interesting to the teacher (maybe a better grade?).

This method is useful for calculating magic squares with odd order. It begins by placing a 1 in the center of the top row), then incrementally placing subsequent numbers in the square one unit diagonally up and to the right. The counting is wrapped around if the number exits the boundaries of the array). When a square is encountered which is already filled, the next number is instead placed below the previous one and the method continues.

I've researched how to do it, but I can't get it to work. I get the following errors upon compiling in Unix using g++:
MagicSquare.cpp: In function `int main()':
MagicSquare.cpp:18: error: `n' cannot appear in a constant-expression

My code is as follows:
```#include <iostream>
using namespace std;
int main(void)
{
int n;
cout<<"Enter Magic Square Order: ";
cin>>n;
cout<<endl;

int squareSize=n*n;
int r=0, c=n/2;
int  **matrix;
matrix = new int*[n];

for (int i=1;i<=3;i++)
{
matrix = new int[n][n];
}
for (int k=1; k<=squareSize; ++k)
{
matrix[r][c] = k;

r--;
c++;

if (k%n == 0)
{
r += 2;
--c;
}
else
{
if (c==n)
c -= n;
else if (r<0)
r += n;
}
}

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

}
```

Thank you.

Is This A Good Question/Topic? 0

## Replies To: Dynamic Memory Allocation for Two Dimensional Array

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3795
• Posts: 13,738
• Joined: 08-August 08

## Re: Dynamic Memory Allocation for Two Dimensional Array

Posted 24 September 2011 - 09:10 PM

You could do it with loops, but since you appear to be using C++ and not C, why not use a vector?

### #3 C++ Programmer

Reputation: 20
• Posts: 548
• Joined: 12-June 08

## Re: Dynamic Memory Allocation for Two Dimensional Array

Posted 24 September 2011 - 09:12 PM

To dynamically allocate a 2D array you do it as follows.

```#include <iostream>

int main() {
int n = 5;
int** a;
a = new int*[n]
for (int i = 0; i < n; i++) {
a[i] = new int[n];
}
for (int i = 0; i < n; i++) {
delete [] a[i];
}
delete [] a;
return 0;
}
```

### #4 baavgai

• Dreaming Coder

Reputation: 7182
• Posts: 14,969
• Joined: 16-October 07

## Re: Dynamic Memory Allocation for Two Dimensional Array

Posted 25 September 2011 - 05:01 AM

You're close.
```// yes
int  **matrix; // a pointer to a pointer or list of pointers
matrix = new int*[n]; // a list of pointers, good

// what's with the match numbers?
// also, don't you want to loop by n?
// for (int i=1;i<=3;i++) {
for (int i=0; i<n; i++) {
// no, you already allocated matrix
// matrix = new int[n][n];
// even if new int[n][n] worked
// which you know it doesn't
// you still would have created an impressive memory leak

// this about what you want to do
// you have an array of pointers
// now, you want to allocate an array to each of those pointers
matrix[i] = new int[n];

```

You want more functions. One to create the matrix, one to show it, one to kill it, etc. Putting all those things in a class would be good.

This post has been edited by baavgai: 25 September 2011 - 05:02 AM

### #5 MrJabbaNoHere

Reputation: 1
• Posts: 29
• Joined: 08-March 11

## Re: Dynamic Memory Allocation for Two Dimensional Array

Posted 25 September 2011 - 12:43 PM

Thanks everyone for your help, I got it to work out! Everyone gets helpful post!

### #6 C++ Programmer

Reputation: 20
• Posts: 548
• Joined: 12-June 08

## Re: Dynamic Memory Allocation for Two Dimensional Array

Posted 25 September 2011 - 12:48 PM

Glad to hear that we could be of help! Feel free to ask us more questions if you need any thing