Hello everyone,
I have been trying to get this code to work, but so far all it does is tell me :
Input the order of the Matrix(Keep it Odd):3
1.11286e-307
1
0
1.11341e-308
1.11286e-307
0
0
1.11343e-308
1.11286e-307
0
0
1.11342e-308
Press any key to continue . . .

I am supposed to create a magic square for any odd number order that anyone puts in. Put in a 3 and get a 3x3 magic square. And it MUST be done using pointers. This is what I have so far:

#include <iostream>
using namespace std;
void main(){
int n,i,j;
int m=1;
cout<<"Input the order of the Matrix(Keep it Odd):";
cin>>n;

double **ms;
	//allocated the space for the rows and the inputs for each row
	ms=new double*[n];
	for(i=0;i<n;i++)
		ms[i]=new double[n];
	//trying to populate the magic square
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
		    ms[i][j]=m;
			m++;
		if (ms[(i+n-1)%n][(j+n+1)%n])
		  i = (i+n+1)%n ;
		 else
		  i = (i+n-1)%n , j = (j+n+1)%n ;
	//print the magic square
	for(i=0;i<=n;i++){
		for(j=0;j<=n;j++)
			cout<<ms[i][j]<<" "<<endl;
	}
	//deallocate the space
	for(i=0;i<=n;i++)
		delete [] ms[i];
	delete []ms;
}

Could someone please tell me why it wont let me populate it to create the magic square with 1 in the middle of the first row and so on?

i forgot to mention the way that i'm trying to populate the magic square. Lets say n=3, we would get a 3x3 matrix.
we would start with a 1 in the middle of row 0.
010 010 010
000 000 300
000 002 402
all the 0's are the spaces we haven't populated yet.

basically we start with 1 and keep counting until we have all the spaces filled in (1-9). This is accomplished by moving up 1 row and 1 space to the right. since theres no row above row 0, we move unto the last row. Then again up 1 row and over 1 space. there are no spaces past column 3, we move onto column 1 and put 3. We cant move from the normal way from 3, up 1 row and over 1 space since its occupied by 1 so we move 1 space down from 3, putting 4.
Then we move again up 1 row and 1 space over until it looks like this:
816
357
492
it should work like this for any odd number magic squares.

Ok, comments.

Indenting is your friend, feel free to use him consistently.

First, why use doubles for the elements of the square. They should all be integer values, right?

I don't think I'd use a for(i ... for (j ... to fill the box. I would pre-initialize i and j (to be the center top like you propose) and then use a for (m = 1; m <= n * n; m++) as the loop.

Did you intentionally not put any braces on your for statements that starts on lines 15 and 16?

If you want to insist on using the for loops as written, consider removing the i++ and j++ from then ones on line 15 and 16. You went to a lot of work on lines 20 and 22 to set them where you wanted them, why would you want to increment them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.