5 Replies - 7527 Views - Last Post: 25 September 2011 - 12:48 PM Rate Topic: -----

#1 MrJabbaNoHere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

I used what I could learn from http://www.geocities...eload_coolmenus

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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • 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?
Was This Post Helpful? 1
  • +
  • -

#3 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • 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;
}

Was This Post Helpful? 2
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • 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

Was This Post Helpful? 2
  • +
  • -

#5 MrJabbaNoHere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#6 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1