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