C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is…
The following solution is suggested to handle the subject “C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is…“. Let’s keep an eye on the content below!
Question “C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is…”
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 1320 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. Do not use global variables.
Answer
SOLUTION-
I have solve the problem in C ++ code with comments and screenshot
for easy understanding 🙂
CODE-
// C++ program //headerfile
#include
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); //constructor
~MagicSquare(); //destructor
bool isMagic() const
void setValues();
void display() const;
};
// A constructor that takes in the matrix’s size and generates the magic square according the given algorithm
MagicSquare::MagicSquare(int n)
{
// Check if input is not even, then set size at n. Otherwise, by default, set n to 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.
}
}
// Fill the matrix using the algorithm
int row=size-1 // set row to the last row
int col=size/2 // set col to the middle column
// Loop size*size times
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++;
// Move into square right and then down
row++;
If(row == size); // wrap around if row is beyond matrix
row = 0
col++;
// If col is not within the matrix, wrap around
col = 0
}
}
// Function to determine if the matrix contains a magic square.
bool MagicSquare
{
int sum = 0, rowSum, colSum;
// Calculate the sum of the first row
for(int i=0;i
// Loop to calculate column sum and row sum
for(int i=0;i
rowSum = 0 ;
colSum = 0;
for(int j=0;j
rowSum += magic[i][j];
colSum += magic[j][i];
}
// Check if the sum of ith row & ith column equals sum
if((rowSum != sum) || (colSum != sum))
// If not returned false, return false
}
// calculate main diagonal sum
int diagonalSum = 0;
for(int i=0;i
If(diagonalSum!= sum) // Check diagonal sum = sum
False return
// Calculate other diagonal sums
diagonalSum = 0;
for(int i=0;i
// Check other diagonal sum = sum
if(diagonalSum != sum)
False return
Return true // It is a magic square
}
// Function to display the matrix
Display() const void MagicSquare
{
for(int i=0;i
for(int j=0;j
}
// Function to modify matrix values
void MagicSquare – setValues()
{
Int row, col. value
char choice;
// Loop until the user desires
Do
{
// enter the row and column to be modified in the matrix
cout”Enter row (1 -“<
// enter the value you wish to set
cout”Insert the value you want to set:
cin>>value;
// Update the value at a given position
magic[row-1][col-1] = value;
// Ask user if they want to make more changes
Do you wish to make further changes? (y/n) “;
cin>>choice;
while(choice == ‘y’ || choice == ‘Y’);|while(choice == “y” || choice == “Y”);}
}
// Destructor to remove the dynamically allocated array
MagicSquare::~MagicSquare()
{
for(int i=0;i
delete[] magic;
}
Int main()
{
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())
coutendlThe matrix above is a MAGIC QUARE: “endl
Other
coutendlThe matrix above is not a MAGIC QUARE: “endl
magic.setValues() // Allow user to update the values
magic.display() // Display the updated array
// Check and display the updated matrix to determine if it is magic square.
if(magic.isMagic())
coutendlThe matrix above is a MAGIC QUARE: “endl
Other
coutendlThe matrix above is not a MAGIC QUARE: “endl
Return 0
}
//Finish of the program
SCREENSHOT-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
—————-PLEASE RATE THE ANSWER———–THANK
YOU!!!!!!!!———–
Conclusion
Above is the solution for “C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is…“. We hope that you find a good answer and gain the knowledge about this topic of engineering.