Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Magic Square (http://www.go4expert.com/articles/magic-square-t8690/)

Magic Square

This is very simple if you think in a simple manner.

A magic square is represented in a matrix form. May be all of you know this formula to satisfy magic square rule i.e. for size of matrix n*n sum of each row or column or diagonal should be n[n*n+1]/2.

means
for n=3 , sum(row or column or diag) = 15
for n=4 , sum(row or column or diag) = 34
for n=5 , sum(row or column or diag) = 65
You can say here n is order of magic square.

Example: for order of magic square 4
``` 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 ```

Now Just think it in a programmitically way!!!
Code: Cpp

`void BuildMagicSquare(){    int order=5,loop;    int MagicMat[5][5];    int SqrOfOrder = order * order;    int nCount=0, mid=order/2;    for(loop=1; loop<=SqrOfOrder ; ++loop)     {        MagicMat[nCount--][mid++] = loop;        if (loop % order == 0)         {             nCount += 2;             --mid;         }        else         {            if (mid==order)                 mid -= order;            else if (nCount<0)                 nCount += order;        }    }}`

 alramesh 6Feb2008 21:26

Re: Magic Square

I think this will work only for odd Order.

means for n=1,3,5,7, etc

 shabbir 7Feb2008 17:14

Re: Magic Square

Quote:
 Originally Posted by alramesh I think this will work only for odd Order. means for n=1,3,5,7, etc
Quote:
 Originally Posted by debleena_doll2002 Yes !!! It will not work for odd order
Now I have a doubt as to for what it will work.

 debleena_doll2002 7Feb2008 17:36

Re: Magic Square

I putted wrongly!!! It will work for only Odd Order...
For even order it will not work.

 debleena_doll2002 7Feb2008 17:38

Re: Magic Square

Can you delete my post
Quote:
 Yes !!! It will not work for odd order

 shabbir 7Feb2008 18:29

Re: Magic Square

Quote:
 Originally Posted by debleena_doll2002 Can you delete my post
Done.

 debleena_doll2002 7Feb2008 18:59

Re: Magic Square

Thanks Shabbir!!!

 coderzone 9Feb2008 12:45

Re: Magic Square

Admin, do you think this should be an Article because it does not work correctly as well as Nothing is explained.

 shabbir 9Feb2008 12:57

Re: Magic Square

No. Because its a contribution and not a query and so it goes into the Article / Tutorial / Source code section. Now how well its explained will make it a contender for the Article of the month.

 coderzone 9Feb2008 13:00

Re: Magic Square

Agreed

Re: Magic Square

Quote:
 Originally Posted by Obaid Admin, do you think this should be an Article because it does not work correctly
On that time i did'nt think for even order as raised ny Alramesh. I was working on that. but in these days i was busy so to give for even order may be late.

Quote:
 Originally Posted by Obaid as well as Nothing is explained.
Really i dont know how much explain of any topic should be. I think people will ask If intrested then i will explain.

 coderzone 11Feb2008 11:15

Re: Magic Square

Quote:
 Originally Posted by asadullah.ansari Really i dont know how much explain of any topic should be. I think people will ask If intrested then i will explain.
There is nothing personal against you but just my expectation from the Articles from this site is quite high. Look at some of the articles by Shabbir, Pradeep, bashamsc, Karpov2007 and they have code which is well commented and easy to understand even for a layman like me.

Re: Magic Square

Sorry to all of you for delayed ... Now this code will work for all order . I have tested on Unix/ solaris

Code:

```#include <vector> #include<iostream.h> using namespace std; //There two series will be on even in case of magic square // One of even order will be for multiple of 4 void BuildDoublyEvenMagicSquare(vector<vector<int> > &mat, int Order); //Other of even order will be for multiple of 2 void SinglyEvenMagicSquare(vector<vector<int> > &mat, int order); // For odd order void BuildOddMagicSquare(vector<vector<int> > &mat, int Order); // For odd order void BuildOddMagicSquare(vector<vector<int> > &mat, int Order) {   int SqrOfOrder = Order * Order;   int start=0, mid=Order/2;    // start position   for (int loop=1; loop<=SqrOfOrder; ++loop)   {     mat[start--][mid++] = loop;     if (loop % Order == 0)     {       start += 2;       --mid;     }     else     {              if (mid==Order)         mid -= Order;       else if (start<0)         start += Order;     }      }    }             void BuildDoublyEvenMagicSquare(vector<vector<int> > &mat, int Order) {   vector<vector<int> > A(Order, vector<int> (Order, 0));   vector<vector<int> > B(Order, vector<int> (Order, 0));   int i, j;   //Building of matrixes I and  J   int index=1;   for (i=0; i<Order; i++)     for (j=0; j<Order; j++)     {       A[i][j]=((i+1)%4)/2;       B[j][i]=((i+1)%4)/2;       mat[i][j]=index;       index++;     }   for (i=0; i<Order; i++)     for (j=0; j<Order; j++)     {       if (A[i][j]==B[i][j])         mat[i][j]=Order*Order+1-mat[i][j];     } } void BuildSinglyEvenMagicSquare(vector<vector<int> > &mat, int order) {   int ho=order/2;   vector<vector<int> > C(ho, vector<int> (ho, 0));   // For Order is Odd     if (order%2==1)       BuildOddMagicSquare(C, order);   // For Order is Even   else   {     //For Order is Doubly Even Order     if (order % 4==0)       BuildDoublyEvenMagicSquare(C, order);     //For Order is Singly Even Order     else       BuildSinglyEvenMagicSquare(C, order);   }   int i, j, k;   for (i=0; i<ho; i++)     for (j=0; j<ho; j++)     {       mat[i][j]=C[i][j];       mat[i+ho][j]=C[i][j]+3*ho*ho;       mat[i][j+ho]=C[i][j]+2*ho*ho;       mat[i+ho][j+ho]=C[i][j]+ho*ho;     }   if (order==2)     return;   vector<int> A(ho, 0);   vector<int> B;   for (i=0; i<ho; i++)     A[i]=i+1;   k=(order-2)/4;   for (i=1; i<=k; i++)     B.push_back(i);   for (i=order-k+2; i<=order; i++)     B.push_back(i);   int temp;   for (i=1; i<=ho; i++)     for (j=1; j<=B.size(); j++)     {       temp=mat[i-1][B[j-1]-1];       mat[i-1][B[j-1]-1]=mat[i+ho-1][B[j-1]-1];       mat[i+ho-1][B[j-1]-1]=temp;     }   i=k;   j=0;   temp=mat[i][j]; mat[i][j]=mat[i+ho][j]; mat[i+ho][j]=temp;   j=i;   temp=mat[i+ho][j]; mat[i+ho][j]=mat[i][j]; mat[i][j]=temp; } int main() {   int Order;   cout<<"Enter the order of square which you wanna: ";   cin>>Order;   vector<vector<int> > mat(Order, vector<int> (Order, 0));   // For order less than 3 is meaningless so printing error   if (Order<3)   {     cout<<" Order Of Square must be greater than 2";     return -1;   }   // For Order is Odd     if (Order%2==1)       BuildOddMagicSquare(mat, Order);   // For Order is Even   else   {     //For Order is Doubly Even Order     if (Order % 4==0)       BuildDoublyEvenMagicSquare(mat, Order);     //For Order is Singly Even Order     else       BuildSinglyEvenMagicSquare(mat, Order);   }   // Display Results   for (int i=0; i<Order; i++)   {     for (int j=0; j<Order; j++)     {       cout<< mat[i][j]<<"  " ;     }     cout<<endl;   }   return 0; }```

 imrantechi 13Feb2008 14:13

Re: Magic Square

can you please try it out again and post it back,

i feel it was a good topic

 debleena_doll2002 13Feb2008 15:07

Re: Magic Square

Quote:
 Originally Posted by imrantechi can you please try it out again and post it back, i feel it was a good topic
Already explained!!! I dont think so that it should be post back.

Re: Magic Square

 crazytolearn57 26Feb2008 18:35

Re: Magic Square

good one

 shabbir 5Mar2008 09:42

Re: Magic Square

Nominate your favorite article of the month for Feb 2008

 aisha.ansari84 5Mar2008 17:24

Re: Magic Square

i feel its a nice article i suppose you can post back the one without any problems

Re: Magic Square

whatever i post in latter, It has no probs. just check it.

 alramesh 5Mar2008 20:12

Re: Magic Square

Good info!!!

Re: Magic Square

Quote:
 Originally Posted by shabbir Nominate your favorite article of the month for Feb 2008
I think it should not be nominated. I did'nt work hard for this. Just two hour.

 rahul.mca2001 6Mar2008 13:09

Re: Magic Square

i think the code has some problem

 shabbir 6Mar2008 13:18

Re: Magic Square

Quote:
 Originally Posted by asadullah.ansari I think it should not be nominated. I did'nt work hard for this. Just two hour. By any shabbir, Thank you very much for nominating this article.
The criteria is not how much you work on it but how much you have provided in the short possible span

Re: Magic Square

Quote:
 Originally Posted by rahul.mca2001 i think the code has some problem

Why you are thinking ? Just check and then if problem came then tell me . 100% i will solve.
I will be more pleasure If you got more issues in this program? welcome but first check

Re: Magic Square

Quote:
 Originally Posted by shabbir The criteria is not how much you work on it but how much you have provided in the short possible span
:iagree:

 parvez.yu 6Mar2008 16:51

Re: Magic Square

Quote:
 Originally Posted by rahul.mca2001 i think the code has some problem
the program works absolutely fine please check

 shabbir 18Mar2008 17:35

Re: Magic Square

Vote for article of the month for Feb 2008

 nileshkumar_1982 26Mar2008 15:47

Re: Magic Square

Nice article . I will vote for this.

 mindblaster 21Jun2009 17:56

Re: Magic Square

this is doesnt work :(

 deniz 2Jan2010 15:30

Re: Magic Square

so, where can i get the solution of this tedious magic square program?..

 deniz 2Jan2010 16:24

Re: Magic Square

Code:

```public class MagicSquare {     public static void main(String[] args) {         int N = Integer.parseInt(args[0]);         if (N % 2 == 0) throw new RuntimeException("N must be odd");         int[][] magic = new int[N][N];         int row = N-1;         int col = N/2;         magic[row][col] = 1;         for (int i = 2; i <= N*N; i++) {             if (magic[(row + 1) % N][(col + 1) % N] == 0) {                 row = (row + 1) % N;                 col = (col + 1) % N;             }             else {                 row = (row - 1 + N) % N;                 // don't change col             }             magic[row][col] = i;         }         // print results         for (int i = 0; i < N; i++) {             for (int j = 0; j < N; j++) {                 if (magic[i][j] < 10)  System.out.print(" ");  // for alignment                 if (magic[i][j] < 100) System.out.print(" ");  // for alignment                 System.out.print(magic[i][j] + " ");             }             System.out.println();         }     } }```

 deniz 2Jan2010 16:26

Re: Magic Square

Shabbir, could you check this one? I'm not quite sure if this gonna work out,eh.. Thanks..

 shabbir 2Jan2010 18:10

Re: Magic Square

Quote:
 Originally Posted by deniz (Post 62586) Shabbir, could you check this one? I'm not quite sure if this gonna work out,eh.. Thanks..
For What?

 deniz 13Jan2010 13:41

Re: Magic Square

Quote:
 Originally Posted by shabbir (Post 62593) For What?
i dont know how to create a magic square program in java. i need it badly..thanks

 All times are GMT +5.5. The time now is 00:08.