help with magic square

This is a discussion on help with magic square within the C++ Programming forums, part of the General Programming Boards category; Hi, I like to learn how to make a magic square. I know how to do a single like of ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    help with magic square

    Hi, I like to learn how to make a magic square.

    I know how to do a single like of array, but when it comes to making 5 dimension magic square i have know idea how to do . If someone can type the code on here so that i can study it and learn it, that will be grateful

    have a Happy Thanksgivings

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    5 dimensions?? I've already played with a 4D one.
    Or you're saying the small square with NxN dimensions whose sum of lined numbers is constant?
    If you have a N*N square, it'll have numbers from 1 to N*N. The sum of all numbers will be (N*N+1)*N/2, therefore one line will result in sum (N*N+1)*N/2/N.
    Example: in a 5*5 square all numbers sum up to 325, therefore a line will have sum 325/5 = 65. Then you'll have a linear system of equations. Have fun solving them

  3. #3
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    /blinks
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  4. #4
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void OddMagicSquare(vector<vector<int> > &matrix, int n);
    void DoublyEvenMagicSquare(vector<vector<int> > &matrix, int n);
    void SinglyEvenMagicSquare(vector<vector<int> > &matrix, int n);
    void MagicSquare(vector<vector<int> > &matrix, int n);
    void PrintMagicSquare(vector<vector<int> > &matrix, int n);
    
    int main(int argc, char* argv[])
    {
    	int n;
    	printf("Enter order of square: ");
    	scanf("%d", &n);
    
    	vector<vector<int> > matrix(n, vector<int> (n, 0));
    
    	if (n<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<vector<int> > &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<vector<int> > &matrix, int n)
    {
    	int nsqr = n * n;
    	int i=0, j=n/2;     // start position
    
    	for (int k=1; k<=nsqr; ++k) 
    	{
    		matrix[i][j] = k;
    
    		i--;
    		j++;
    
    		if (k%n == 0) 
    		{ 
    			i += 2; 
    			--j; 
    		}
    		else 
    		{
    			if (j==n) 
    				j -= n;
    			else if (i<0) 
    				i += n;
    		}
    	}
    }
    
    void DoublyEvenMagicSquare(vector<vector<int> > &matrix, int n)
    {
    	vector<vector<int> > I(n, vector<int> (n, 0));
    	vector<vector<int> > J(n, vector<int> (n, 0));
    
    	int i, j;
    
    	//prepare I, J
    	int index=1;
    	for (i=0; i<n; i++)
    		for (j=0; j<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<n; i++)
    			for (j=0; j<n; j++)
    			{
    				if (I[i][j]==J[i][j])
    					matrix[i][j]=n*n+1-matrix[i][j];
    			}
    }
    
    void SinglyEvenMagicSquare(vector<vector<int> > &matrix, int n)
    {
    	int p=n/2;
    
    	vector<vector<int> > M(p, vector<int> (p, 0));
    	MagicSquare(M, p);
    
    	int i, j, k;
    
    	for (i=0; i<p; i++)
    		for (j=0; j<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<int> I(p, 0);
    		vector<int> J;
    
    		for (i=0; i<p; i++)
    			I[i]=i+1;
    
    		k=(n-2)/4;
    
    		for (i=1; i<=k; i++)
    			J.push_back(i);
    
    		for (i=n-k+2; i<=n; i++)
    			J.push_back(i);
    
    		int temp;
    		for (i=1; i<=p; i++)
    			for (j=1; j<=J.size(); j++)
    			{
    				temp=matrix[i-1][J[j-1]-1];
    				matrix[i-1][J[j-1]-1]=matrix[i+p-1][J[j-1]-1];
    				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<vector<int> > &matrix, int n)
    {
    	for (int i=0; i<n; i++) 
    	{
    		for (int j=0; j<n; j++)
    			printf(" %3d", matrix[i][j]);
    
    		printf("\n");
    	}
    
    	printf("\n\n");
    }
    big146

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  2. Windows crashing Magic Square
    By KoshiB in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2006, 10:02 PM
  3. help on magic square
    By katway in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 06:44 PM
  4. magic square whoes
    By caws in forum C Programming
    Replies: 9
    Last Post: 03-30-2003, 10:36 PM
  5. Help with magic square program
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 06:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21