In: Computer Science
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
}
#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