Tuesday, July 14, 2009

Example program of magic square in c language?

show the whole magic program in c language.

Example program of magic square in c language?
Homework? :-)


See the link to the source below.
Reply:#include "stdafx.h"


#include %26lt;vector%26gt;





using namespace std;





void OddMagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n);


void DoublyEvenMagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n);


void SinglyEvenMagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n);


void MagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n);


void PrintMagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n);





int main(int argc, char* argv[])


{


int n;


printf("Enter order of square: ");


scanf("%d", %26amp;n);





vector%26lt;vector%26lt;int%26gt; %26gt; matrix(n, vector%26lt;int%26gt; (n, 0));





if (n%26lt;3)


{


printf("\nError: n must be greater than 2\n\n");


return -1;


}





MagicSquare(matrix, n);





//Print results


PrintMagicSquare(matrix, n);





return 0;


}





void MagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix,int n)


{


if (n%2==1) //n is Odd


OddMagicSquare(matrix, n);


else //n is even


if (n%4==0) //doubly even order


DoublyEvenMagicSquare(matrix, n);


else //singly even order


SinglyEvenMagicSquare(matrix, n);


}





void OddMagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n)


{


int nsqr = n * n;


int i=0, j=n/2; // start position





for (int k=1; k%26lt;=nsqr; ++k)


{


matrix[i][j] = k;





i--;


j++;





if (k%n == 0)


{


i += 2;


--j;


}


else


{


if (j==n)


j -= n;


else if (i%26lt;0)


i += n;


}


}


}





void DoublyEvenMagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n)


{


vector%26lt;vector%26lt;int%26gt; %26gt; I(n, vector%26lt;int%26gt; (n, 0));


vector%26lt;vector%26lt;int%26gt; %26gt; J(n, vector%26lt;int%26gt; (n, 0));





int i, j;





//prepare I, J


int index=1;


for (i=0; i%26lt;n; i++)


for (j=0; j%26lt;n; j++)


{


I[i][j]=((i+1)%4)/2;


J[j][i]=((i+1)%4)/2;


matrix[i][j]=index;


index++;


}





for (i=0; i%26lt;n; i++)


for (j=0; j%26lt;n; j++)


{


if (I[i][j]==J[i][j])


matrix[i][j]=n*n+1-matrix[i][j];


}


}





void SinglyEvenMagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n)


{


int p=n/2;





vector%26lt;vector%26lt;int%26gt; %26gt; M(p, vector%26lt;int%26gt; (p, 0));


MagicSquare(M, p);





int i, j, k;





for (i=0; i%26lt;p; i++)


for (j=0; j%26lt;p; j++)


{


matrix[i][j]=M[i][j];


matrix[i+p][j]=M[i][j]+3*p*p;


matrix[i][j+p]=M[i][j]+2*p*p;


matrix[i+p][j+p]=M[i][j]+p*p;


}





if (n==2)


return;





vector%26lt;int%26gt; I(p, 0);


vector%26lt;int%26gt; J;





for (i=0; i%26lt;p; i++)


I[i]=i+1;





k=(n-2)/4;





for (i=1; i%26lt;=k; i++)


J.push_back(i);





for (i=n-k+2; i%26lt;=n; i++)


J.push_back(i);





int temp;


for (i=1; i%26lt;=p; i++)


for (j=1; j%26lt;=J.size(); j++)


{


temp=matrix[i-1][J[j-1]-1];


matrix[i-1][J[j-1]-1]=matrix[i+p-1][J[j-...


matrix[i+p-1][J[j-1]-1]=temp;


}





//j=1, i


//i=k+1, k+1+p


i=k;


j=0;


temp=matrix[i][j]; matrix[i][j]=matrix[i+p][j]; matrix[i+p][j]=temp;





j=i;


temp=matrix[i+p][j]; matrix[i+p][j]=matrix[i][j]; matrix[i][j]=temp;


}








void PrintMagicSquare(vector%26lt;vector%26lt;int%26gt; %26gt; %26amp;matrix, int n)


{


for (int i=0; i%26lt;n; i++)


{


for (int j=0; j%26lt;n; j++)


printf(" %3d", matrix[i][j]);





printf("\n");


}





printf("\n\n");


}

love song

No comments:

Post a Comment