I need this written in C++. Magic Squares. An n x n matrix that is filled…
The following solution is suggested to handle the subject “I need this written in C++. Magic Squares. An n x n matrix that is filled…“. Let’s keep an eye on the content below!
Question “I need this written in C++. Magic Squares. An n x n matrix that is filled…”
I need this written in C++. Magic Squares. An n x n matrix that
is filled with the numbers 1,2,3,…,n2 is a magic square
if the sum of the elements in each row, in each column, and in the
two diagonals is the same value.
The following algorithm will construct magic n x n squares; it only
works if n is odd: Place 1 in the middle of the bottom row. After k
has been placed in the (i,j) square, place k+1 into the square
right and down, wrapping around the borders. However, if you reach
a square that has already been filled, then you must move one
square up instead.
Here is the 5 x 5 square that you get if you follow this
method:
11 18 25 2 9
10 12 19 21 3
4 6 13 20 22
23 5 7 14 16
17 24 1 8 15
Make a function with an array argument
that will make a magic square.
Make another function that will check
if its array argument is a magic square.
Make a third function that can be
passed a matrix of the same size as the magic squares created by
your first function, and will allow the user to enter any number of
changes the user wants to make to the values of the matrix.
Make a main function that will make a
magic square, print it, check if it is a magic square, allow the
user to change it, print it again, and check if it is a magic
square after the changes.
Also: Define a class
MagicSquare, which will store a square matrix in a dynamically
allocated two-dimensional array. Create a magic square by the
constructor, and provide methods isMagic and setValues
corresponding to the second and third functions described above.
Provide a main function to test your class as above.
Answer
// C++ program to create Magic Square. The user can modify the values to determine if the magic square is generated.
Use namespace std
MagicSquare class
{
private:
int **magic // Dynamic array to store the magic circle
int size // Size of the matrix
public:
MagicSquare(int n);
bool isMagic() const
void setValues();
void display() const;
};
// function declaration
void makeMagicSquare(int **matrix, int size);
bool isMagicSquare (int **matrix; int size);
void makeChanges(int **matrix, int size);
void printMatrix(int **matrix, int size);
Int main()
{
Const int size = 5.
// Create a matrix of the size you need and set all elements to 0
int **matrix = int*[size]
for(int i=0;i
for(int j=0;j
}
cout”Creating magic quad of size “size” using :”<
“Make changes to the matrix”endl
cout”Creating magic quad of size “size” using class endl
magic.setValues() // Allow user to update the values
Return 0
// A constructor that takes in the size of the matrix to generate the magic square according the given algorithm
// Fill the matrix using the algorithm
// Loop size*size times
// Move into square right and then down
// If col is not within the matrix, wrap around
// Function to determine if the matrix contains a magic square.
// Loop to calculate column sum and row sum
// Check if the sum of ith row & ith column equals sum
// calculate main diagonal sum
int diagonalSum = 0;
If(diagonalSum!= sum) // Check diagonal sum = sum
// Calculate other diagonal sums
for(int i=0;i
Return true // It is a magic square
}
// Function to display the matrix
// Function to modify matrix values
// Loop until the user desires
// Ask user if they want to make more changes
// Loop size*size times
// Move into square right and then down
// If col is not within the matrix, wrap around
// Function to return true if the input matrix size is magic square, otherwise return false
// Loop to calculate column sum and row sum
// Check if the sum of ith row & ith column equals sum
// calculate main diagonal sum
int diagonalSum = 0;
If(diagonalSum!= sum) // Check diagonal sum = sum
// Calculate other diagonal sums
for(int i=0;i
Return true // It is a magic square
}
// Function to allow the user make adjustments to the input matrix size.
// Loop until the user desires
// Ask user if they want to make more changes
}
// Function to display the matrix size
//End of the program
Above is the solution for “I need this written in C++. Magic Squares. An n x n matrix that is filled…“. We hope that you find a good answer and gain the knowledge about this topic of engineering.
printMatrix(matrix, size);
if(isMagicSquare(matrix,size))
cout<<"MAGIC SQUARE. "<
cout”NOT MAGIC SQUARE. “<
makeChanges(matrix, size);
printMatrix(matrix,size);
if(isMagicSquare(matrix,size))
cout<<"MAGIC SQUARE. "<
cout”NOT MAGIC SQUARE. “<
MagicSquare magic(5) // Create magic squares of size 5
magic.display() // Display the array
// Check and display the magic square of the matrix
if(magic.isMagic())
coutendl “<
coutendlThe matrix above is not a MAGIC QUARE. “<
magic.display() // Display the updated array
// Check and display the updated matrix to determine if it is magic square.
if(magic.isMagic())
coutendl “<
coutendlThe matrix above is not a MAGIC QUARE. “<
}
MagicSquare::MagicSquare(int n)
{
// Check if input is not even, then set size at n. Otherwise, by default set n at 5.
if(n%2 != 0)
Size = n
Other
Size = 5
// Create the dynamic array size*size
magic = new int*[size]
for(int i=0;i
magic[i] = new size
for(int j=0;j
magic[i][j] = 0, // Initialize all entries to 0.
}
}
int row=size-1 // set row to the last row
int col=size/2 // set col to the middle column
for(int Cnt = 1;cnt=size*size
{
// If location (row,col), is full, move one square up
if(magic[row][col] != 0)
{
row–;
If(row ==-1) // Wrap around if row is outside the matrix
row = size-1
row–;
if(row == -1)
row = size-1
col–;
If(col ==-1) // Wrap around if col is outside the matrix
col = size-1
continue;
}
other // If the space is empty, fill it with cnt.
magic[row][col] = cnt++;
row++;
If(row == size); // wrap around if row is beyond matrix
row = 0
col++;
col = 0
}
}
bool MagicSquare
{
int sum = 0, rowSum, colSum;
// Calculate the sum of the first row
for(int i=0;i
for(int i=0;i
rowSum = 0 ;
colSum = 0;
for(int j=0;j
rowSum += magic[i][j];
colSum += magic[j][i];
}
if((rowSum != sum) || (colSum != sum))
// If not returned false, return false
}
for(int i=0;i
False return
diagonalSum = 0;
// Check other diagonal sum = sum
if(diagonalSum != sum)
False return
Display() const void MagicSquare
{
for(int i=0;i
for(int j=0;j
}
void MagicSquare – setValues()
{
Int row, col. value
char choice;
Do
{
// enter the row and column to be modified in the matrix
cout”Enter row (1 -“<
// enter the value you wish to set
cout”Enter your value:
cin>>value;
// Update the value at a given position
magic[row-1][col-1] = value;
Do you wish to make further changes? (y/n) “;
cin>>choice;
while(choice == ‘y’ || choice == ‘Y’);|while(choice == “y” || choice == “Y”);}
}
// Create a magic square with odd size using the function
void makeMagicSquare(int **matrix, int size)
{
int row=size-1 // set row to the last row
int col=size/2 // set col to the middle column
for(int Cnt = 1;cnt=size*size
{
// If location (row,col), is full, move one square up
if(matrix[row][col] != 0)
{
row–;
If(row ==-1) // Wrap around if row is outside the matrix
row = size-1
row–;
if(row == -1)
row = size-1
col–;
If(col ==-1) // Wrap around if col is outside the matrix
col = size-1
continue;
}
other // If the space is empty, fill it with cnt.
matrix[row][col] = cnt++;
row++;
If(row == size); // wrap around if row is beyond matrix
row = 0
col++;
col = 0
}
}
bool isMagicSquare (int **matrix and int size).
{
int sum = 0, rowSum, colSum;
// Calculate the sum of the first row
for(int i=0;i
for(int i=0;i
rowSum = 0 ;
colSum = 0;
for(int j=0;j
rowSum += matrix[i][j];
colSum += matrix[j][i];
}
if((rowSum != sum) || (colSum != sum))
// If not returned false, return false
}
for(int i=0;i
False return
diagonalSum = 0;
// Check other diagonal sum = sum
if(diagonalSum != sum)
False return
void makeChanges(int **matrix, int size)
{
Int row, col. value
char choice;
Do
{
// enter the row and column to be modified in the matrix
cout”Enter row (1 -“<
// enter the value you wish to set
cout”Enter your value:
cin>>value;
// Update the value at a given position
matrix[row-1][col-1] = value;
Do you wish to make further changes? (y/n) “;
cin>>choice;
while(choice == ‘y’ || choice == ‘Y’);|while(choice == “y” || choice == “Y”);}
void printMatrix(int **matrix, int size)
{
for(int i=0;i
for(int j=0;j
}
Output:
Conclusion