I have to do this. These are the instructions.
Write a program that queries the user for an odd integer n, where n is a 3, a 5, or a 7. Create a 7x7 static matrix and use it to produce an n x n magic square as described in the problem.
The problem is the Magic Square is going above the number 7 it will take any number for some reason. I need it to be a 7x7 static matrix as described in the problem. Please help me out.
Here is my code,
/*--------------------------------------------------------------------
Program to construct magic squares of odd size.
Input: Size of square (odd #)
Output: A magic square
----------------------------------------------------------------------*/
const int MAX_DIM = 21;
typedef int IntTable[MAX_DIM][MAX_DIM ];
void createMagic(IntTable square, int n);
/*---------------------------------------------------------------------
Construct a magic square of odd size n.
Precondition: size of square is odd positive integer.
Postcondition: square stores an n x n magic square.
----------------------------------------------------------------------*/
void display(IntTable t, int n);
/*---------------------------------------------------------------------
Display an n x n magic square.
Precondition: None.
Postcondition: square is output to cout.
----------------------------------------------------------------------*/
//--- Definition of createMagic()
void createMagic(IntTable square, int n)
{
if (n % 2 == 0)
{
cerr << "Size of magic square must be odd.\n";
exit(1);
}
int row,
col;
for (row = 0; row < n; row++)
for (col = 0; col < n; col++)
square[row][col] = 0;
row = 0;
col = n / 2;
for (int k = 1; k <= n * n; k++)
{
square[row][col] = k;
Put your code in code tags, so that it is readable. Explain your problem more clearly: I see no attempt to test if n is 3, 5 or 7. MAXDIM=21 bears no resemblance to 7.
That looks good but doesn't work on my compiler, it says is_integral_v' is not a member of 'std'|,so don't know how to fix that.
It works on a online compiler, the only issue i have with that code is if i enter a magic square number of 1 it will work and doesn't say that its not valid.
There is says that is_integral_v' belongs to c++17 standard, so one needs to compile with std=c++17, and it should work.
With compilers, I always try to have the latest version, and I always compile to the latest standard, even though there are only a handful of things available in c++23 at the moment, there may have been some improvement in the compiler from previous versions. The compiler vendors publish lists of the things they have fixed.
C++20 is the current standard,so you should consider upgrading your compiler.
The following macro names are predefined in every translation unit.
__cplusplus
denotes the version of C++ standard that is being used, expands to value 199711L(until C++11), 201103L(C++11), 201402L(C++14), 201703L(C++17), or 202002L(C++20)