Question

In: Computer Science

Assingment: for c++ A magic square is an n x n matrix in which each of...

Assingment: for c++

A magic square is an n x n matrix in which each of the integers 1, 2, 3...n2 appears exactly once and all column sums, row sums, and diagonal sums are equal. For example, the attached table shows the values for a 5 x 5 magic square in which all the rows, columns, and diagonals add up to 65.

The following is a procedure for constructing an n x n magic square for any odd integer n. Place 1 in the middle of the top row. Then, after integer k has been placed, move up one row and one column to the right to place the next integer k+1 unless one of the following occurs:

-If the move takes you above the top row in the jth column, move to the bottom of the jth column and place k+1 there.

-If the move takes you outside to the right of the square in the ith row, place k+1 in the ith row on the left side.

-If the move takes you to an already filled square or if you move out of the square at the upper right-hand corner, place k+1 immediately below k.

Write a program to create a magic square. Get the size of the square (an odd integer) from the user. You must use a static matrix for the magic square. A solution using a dynamic matrix is unacceptable.

My code works, but the diagonals do not work, any help would be great, with documentation.

Here's my code:

#include
#include
using namespace std;

int main()
{
int n; //variable for array size (n x n)

cout<< "Enter an odd integer less than 50: ";
cin>>n;

int MagicSq[50][50];

// Clears the array of any unwanted data
for(int x = 0; x < n; x++)
{
for(int y = 0; y < n; y++)
{
MagicSq[x][y] = 0;
}
}

// the variables being used for the arrays
int Row,
Col;
int x =0 ;
int y= n / 2;

// Filling in each element of the array using the magic array
for ( int value = 1; value <= n*n; value++ )
{
MagicSq[x][y] = value;
// Finding the next cell
Row = (x - 1) % n;
Col = (y + 1) % n;


// If the cell is empty
if ( MagicSq[Row][Col] == 0 )
{
x = Row;
y = Col;
}
else
{
// The cell is full, so use the cell above the previous one.
x = (x + 1 + n) % n;
}

}


for(int i=0; i {
for(int j=0; j cout << MagicSq[i][j]<<" ";
cout << endl;
}
return(0); //End
}

Solutions

Expert Solution

#include <iostream>

using namespace std;
void generateSquare(int n){

int MagicSq[50][50];

// Clears the array of any unwanted data
for(int x = 0; x < n; x++)
{
for(int y = 0; y < n; y++)
{
MagicSq[x][y] = 0;
}
}
{
MagicSq[n][n];
  
// Initialize position for 1
int i = n/2;
int j = n-1;
  
// One by one put all values in magic square
for (int num=1; num <= n*n; )
{
if (i==-1 && j==n) //3rd condition
{
j = n-2;
i = 0;
}
else
{
// 1st condition helper if next number
// goes to out of square's right side
if (j == n)
j = 0;
  
// 1st condition helper if next number
// is goes to out of square's upper side
if (i < 0)
i=n-1;
}
if (MagicSq[i][j]) //2nd condition
{
j -= 2;
i++;
continue;
}
else
MagicSq[i][j] = num++; //set number
  
j++; i--; //1st condition
}
  
// Print magic square
  
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
cout << MagicSq[i][j]<<" ";
cout << endl;
}
} }

int main()
{
int n; //variable for array size (n x n)

cout<< "Enter an odd integer less than 50: ";
cin>>n;


generateSquare (n);
return 0;
} output2


Related Solutions

THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers...
THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n^2 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. Write a program that randomly generates 16 numbers, and it assigns them to the array after testing that the number was not already assigned. The program should test whether they form a...
Theorem. If A is an n by n square matrix, then the following statements are equivalent....
Theorem. If A is an n by n square matrix, then the following statements are equivalent. A is invertible. The system Av=b has at least one solution for every column-vector b. The system Av=b has exactly one solution for every column-vector b The system Av=b has only the trivial solution (0,0,0,...0) ( Homogeneous systems). The reduced row-echelon form of A is the identity matrix. A is a product of elementary matrices. Can you please help me to give some short...
Suppose C is a m × n matrix and A is a n × m matrix....
Suppose C is a m × n matrix and A is a n × m matrix. Assume CA = Im (Im is the m × m identity matrix). Consider the n × m system Ax = b. 1. Show that if this system is consistent then the solution is unique. 2. If C = [0 ?5 1 3 0 ?1] and A = [2 ?3   1 ?2    6 10] ,, find x (if it exists) when (a) b =[1...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figures below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure B. In a program you can stimulate a magic square using a two-dimensional list. Write a function...
Let A ∈ Mat n×n(R) be a real square matrix. (a) Suppose that A is symmetric,...
Let A ∈ Mat n×n(R) be a real square matrix. (a) Suppose that A is symmetric, positive semi-definite, and orthogonal. Prove that A is the identity matrix. (b) Suppose that A satisfies A = −A^T . Prove that if λ ∈ C is an eigenvalue of A, then λ¯ = −λ. From now on, we assume that A is idempotent, i.e. A^2 = A. (c) Prove that if λ is an eigenvalue of A, then λ is equal to 0...
C++ Give an example of overloading by implementing a function square(x), which calculates the square of...
C++ Give an example of overloading by implementing a function square(x), which calculates the square of x, (also known as x*x or x 2 ) where the input can be int or double.
For matrices, a mulitplicative identity is a square matrix X such XA = AX = A...
For matrices, a mulitplicative identity is a square matrix X such XA = AX = A for any square matrix A. Prove that X must be the identity matrix. Prove that for any invertible matrix A, the inverse matrix must be unique. Hint: Assume that there are two inverses and then show that they much in fact be the same matrix. Prove Theorem which shows that Gauss-Jordan Elimination produces the inverse matrix for any invertible matrix A. Your proof cannot...
Let A be an m x n matrix and b and x be vectors such that...
Let A be an m x n matrix and b and x be vectors such that Ab=x. a) What vector space is x in? b) What vector space is b in? c) Show that x is a linear combination of the columns of A. d) Let x' be a linear combination of the columns of A. Show that there is a vector b' so that Ab' = x'.
Problem 1 1.1 If A is an n x n matrix, prove that if A has...
Problem 1 1.1 If A is an n x n matrix, prove that if A has n linearly independent eigenvalues, then AT is diagonalizable. 1.2 Diagonalize the matrix below with eigenvalues equal to -1 and 5. 0 1   1   2 1 2 3 3 2 1.3 Assume that A is 4 x 4 and has three different eigenvalues, if one of the eigenspaces is dimension 1 while the other is dimension 2, can A be undiagonalizable? Explain. Answer for all...
Prove the trace of an n x n matrix is an element of the dual space...
Prove the trace of an n x n matrix is an element of the dual space of all n x n matrices.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT