Skip to content

Instantly share code, notes, and snippets.

@CarlosRA97
Forked from juanfal/buildMagic.cpp
Created January 18, 2018 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosRA97/c0fdd62f3d0ff6bcb308646718be917e to your computer and use it in GitHub Desktop.
Save CarlosRA97/c0fdd62f3d0ff6bcb308646718be917e to your computer and use it in GitHub Desktop.
Using Siamese method fill a Magic Square Matrix
// buildMagic.cpp
// juanfc 2018-01-17
// Using Siamese method fill a Magic Square Matrix,
// odd fixed size.
// We have used a big square matrix, but used
// an extra parameter to restrict to that size
//
// Testing it building magic matrices up to a size
// checking they really are magic and printing them
// https://gist.github.com/6db44e77b75d3aacdfa70bdfb7972ef1
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
const int NMAX = 15;
typedef array<int, NMAX> TRow;
typedef array<TRow, NMAX> TSqMat;
TSqMat buildMagicSquare (int N);
void testAndPrintMagicSq(TSqMat m, int N);
int main()
{
for (int size = 1; size <= NMAX; size += 2)
testAndPrintMagicSq(buildMagicSquare(size), size);
return 0;
}
TSqMat buildMagicSquare (int N)
{
TSqMat m;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
m[i][j] = 0;
int i, j;
int key = 1;
int NN = N * N;
i = 0;
j = (N - 1) / 2;
m[i][j] = key++; // middle of the top row
while (key <= NN) {
int ii, jj; // try, look ahead beforehand
if (i > 0) ii = i-1;
else ii = N-1;
if (j < N-1) jj = j+1;
else jj = 0;
if (m[ii][jj] != 0) // taken cell
i = (i+1) % N; // go down
else {
i = ii; // alright, take them
j = jj;
}
m[i][j] = key++;
}
return m;
}
bool isMagic(TSqMat m, int N);
void testAndPrintMagicSq(TSqMat m, int N)
{
cout << endl << "Magic Square of size: " << N;
int s = 0;
for (int i = 0; i < N; i++)
s += m[i][0];
cout << ". Sum: " << s << endl;
cout << (isMagic(m, N)? "IS":"is NOT")
<< " magic" << endl;
// display it
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
cout << setw(4) << m[i][j];
cout << endl << endl;
}
}
int sumRow(TSqMat m, int row, int N);
int sumCol(TSqMat m, int col, int N);
bool isMagic(TSqMat m, int N)
{
bool ok = true;
int magic = sumRow(m, 0, N);
int i = 1;
while (i<N and ok)
ok = (magic == sumRow(m, i++, N));
i = 0;
while (i<N and ok)
ok = (magic == sumCol(m, i++, N));
if (ok) {
int s = 0;
for (int i = 0; i < N; ++i)
s += m[i][i];
ok = magic == s;
}
if (ok) {
int s = 0;
for (int i = 0; i < N; ++i)
s += m[i][N-1-i];
ok = magic == s;
}
return ok;
}
int sumRow(TSqMat m, int row, int N)
{
int s = 0;
for (int i = 0; i < N; ++i)
s += m[row][i];
return s;
}
int sumCol(TSqMat m, int col, int N)
{
int s = 0;
for (int i = 0; i < N; ++i)
s += m[i][col];
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment