Pivoting content: arts and crafts, electronics, technology, and a little dose of everything else! Every Monday and Thursday (hopefully)!


Working on kyaratar.com and other projects.

C++ - Magic Square X2

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active

Magic Square X2 is an implementation of the magic square arithmetic from Chinese mathematics. A C++ application and code is provided for learning purposes.

Magic Square:

Basically magic square is close-to Sudoku, but not quite. You specify a number on how big the matrix will be and that will also determine what numbers go in diagonal, row, and column all add up to. The key catch is you can only use each number once. Here is an image below that shows an example of what a basic magic square looks like:

In the above picture the specified number is 3, so the matrix is a 3x3. So the number that corresponds to a 3x3 is 15. Hence each diagonal, row, and column all add up to 15.

Video Demonstration:

This is a really old video of me explaining and showing a demo of the small console application created using CodeBlocks.

Code Explanation:

The code isn't well-written. I could have done a switch case statement in an infinite while loop for selecting the operations. Maybe I could have used a recursive function for iterating through the matrix instead of using nested for loops (I think). If you want to change it so it can also solve even numbers just change the one if statement for checking if the user input is odd. You can also remove that statement if you want to just do the magic square for all given numbers.


The code is relatively short!

Show Code

Hide Code

Have Fun:

That's all there is for this article. If you have any questions or comments go ahead and do so below or in the video.

Tags: Computer Science, C++

Comments powered by CComment