C++ Programming GalleryMath and others
Sudoku Magic Square in C++

Solution to the classic mathematical problem of the magic square (odd order). For those who do not know what it is about, you have to place the first n natural numbers (n odd) in an nxn matrix in such a way that the sum of any row, column or diagonal must always be the same.
Example:
Magic Square of order 3 :
8 1 6
3 5 7
4 9 2
Sum = 15
GENERAL ALGORITHM
A way to easily construct magic squares of odd order.
- Let’s take any arithmetic series, for convenience the series of natural numbers, and place the number 1 in the middle cell of the top row.
- The digit consecutive to any one must be placed in the cell that follows it diagonally upward and to the right.
- If in doing this we leave the square at the upper boundary of its outline, we jump to the cell of the next column to the right and in its lower row, if we leave from the right, we continue in the first cell, starting from the left, of the upper row.
- When the next cell is occupied, the consecutive number of the series is placed in the cell immediately below the preceding number, thus starting a new path in the direction of the diagonal.
Developed by:
YACSHA – Software & Desing, since 1999, Lima – Perú
The World of Chaos – EL MUNDO DEL CAOS – Unlimited Programming
You can DOWNLOAD the SOURCE CODE and executable software for FREE from here:
Join The World of Chaos Developer Community😃
Contribute to the project on Github!HISTORY
- Version 3 – 21-IV-2024
- Use setfill(‘ ‘) setw(4) in cout, to replace the old printf(“%4d”) and display the matrices arranged symmetrically by rows and columns
- Version 2 – 19-IV-2024
- Update math-and-others\cuadmagi – Porting to VC++ 2017
- Version 1 – 17-IV-2001
- First version for Borland C++ 3.1 and Turbo C 3.0