Friday, October 24, 2014

Magic Squares

Was just remembering my childhood days this diwali, during childhood which we used to have lots of fun with friends and used to play a lot.

Suddenly remembered that trick which we used to use to fool around with friends, and that's magic-squares.

Magic-Squares is just arrangement of numbers in the square shape where, sum of each of its rows and sum of each of its column and sum of each diagonal is equal to same number.

So now being a computer science enthusiastic, I thought ok.. let me tell my computer (which had g++ compiler) to solve magic-squares.

Here is the result of my computer did. It did all these below ones in less than a fraction of a second ;) :P

NOTE:
After the symbol => is the rowSum
After the symbol \/ is the columnSum
RTLB -- Sum of numbers across Right Top to Left Bottom Diagonal
LTRB -- Sum of numbers across Left Top to Right Bottom Diagonal



Well for those who are interested in having a look at the code, here it is for them.

This is our main function:
 int main()  
 {  
   int num = 0;  
   cout << "Enter any odd number: ";  
   cin >> num;  
   cout << endl << endl;  
   if(num % 2 == 0) {  
     cout << "Even number not supported !! " << endl;  
     return 1;  
   }  
   MagicSquare mSq(num);  
   mSq.computeMagicSquare();  
   return 0;  
 }  

This is the main part where the magic square is begin populated.
   void getRowAndColumn(int& row, int& col, const int counter)  
   {  
     if(counter == 1) { col = num / 2; return;} // Initial condition to insert 0  
     else if(row == 0 && (col == num / 2)) { row = num - 1; col++; return;}  
     else if(col+1 == num && row == 0) { row++; return;}  
     else if(col+1 == num) { col = 0; row--; return;}  
     else if(row == 0) { row = num -1; col++; return;}  
     else if((row!=0) && (col!=num-1) && magicSquare[row-1][col+1] != 0) { row++; return;}  
     else { row--; col++; return;}  
   }  
   void computeMagicSquare(void)  
   {  
   // Printing Dummy Magic Square  
   // Solution for magic square   
   // Step 1 : Placing 1 in the middle of the first row 1st row of the matrix  
     cout << "Printing matrix template:" << endl;  
     printMagicSquare();  
     int counter = 1;  
     int row = 0;  
     int col = 0;  
     while(counter <= num*num)  
     {  
       getRowAndColumn(row, col, counter);  
       // cout << "Row = " << row << "Col = " << col << "Counter = " << counter << endl;  
       if(row < 0 || row >= num || col < 0 || col >= num) cout << "Error in calculating Row/Column Row = " << row  
         << "Column = " << col << endl;  
       magicSquare[row][col] = counter++;  
     }  
     cout << "\n\nPrinting magicSquare: " << num << "*" << num << endl;  
     printMagicSquare();  
   }  
This is the printing portion of the magicSquares.
   void printMagicSquare(void)  
   {  
     int LTRBDiagSum = 0;  
     int LBRTDiagSum = 0;  
     int *columnSum = new int[num];  
     for(int row = 0; row < num; row++)  
     {  
       int rowSum = 0;  
       for(int col = 0; col < num; col++)  
       {  
         rowSum += magicSquare[row][col];  
         if(row == col) LTRBDiagSum += magicSquare[row][col];  
         if(row+col == num-1) LBRTDiagSum += magicSquare[row][col];  
         columnSum[col] += magicSquare[row][col];  
         cout << "\t" << magicSquare[row][col];  
       }  
       cout << " => " << rowSum << endl;  
     }  
     cout << "RTLB(" << LBRTDiagSum << ")";  
     for(int i = 0; i < num; i++)  
     {  
       cout << "\\/";  
       if(i != num-1) cout << "\t";  
     }  
     cout << "LTRB(" << LTRBDiagSum << ")";  
     cout << endl;  
     for(int i = 0; i < num; i++)  
     {  
       cout << "\t" << columnSum[i] ;  
     }  
     cout << endl;  
     delete[] columnSum;  
   }  
And finally our class looked like this
 class MagicSquare {  
   public:  
   MagicSquare(int n)  
   {  
     num = n;  
     magicSquare = new int* [num];  
     for(int i = 0; i < num; i++)  
     {  
       magicSquare[i] = new int[num];  
     }  
   }  
   ~MagicSquare()  
   {  
     for(int i = 0; i < num; i++)  
     {  
       delete[] magicSquare[i];  
     }  
     delete[] magicSquare;  
   }  
   void printMagicSquare(void);  
   void getRowAndColumn(int& row, int& col, const int counter);  
   void computeMagicSquare(void);  
   private:  
   int **magicSquare;  
   int num;  
 };  


Well for those is really interested to execute this source code at your end, you can get the code from here.

I sincerely thank this blog for helping me formatting above code snippets.

Yeah thats it about magic-squares. Lets see if time permits will try to enhance code for even numbers as well.

No comments:

Post a Comment