Hello everyone, I hate to make my first question as my first post, I would like to become a part of this seemingly great community, but I need to ask a question because I am stumped.
I need to create a Magic Square. This is my homework assignment, and I have spent 3 days now trying to figure it out. My instructor has not responded to my questions via email and hopefully you guys can help with this assignment.
Here are the instructions:
A magic square is an n x n matrix in which each of the integers 1,2,3...n 2 appears exactly once and all column sums, row sums, and diagonal sums are equal. For example, the following is a 5 x 5 magic square in which all of the sums add up to 65:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
The following is a procedure for constructing an n x n magic square for any odd integer n. Place 1 in the middle column 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 a move takes you above the top row in the ith column, move to the bottom of the ith column and place k + 1 there
* If a move takes you outside to the right of the square in the ith row, place k + 1 in the ith row at the left side.
* If a 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 construct n x n magic square for any odd value of n. "
My current code is started but the logic to actually enter the values I am stumped. I can only use <iostream>.
I know that I need to create a counting (for) loop to add the values into the array, but I literally am stumped. Hopefully someone will be up tonight who can help me out!
Thanks in advance, and I am happy to be on the site now.
using namespace std;
void fillMagicSquare(int newarray[n], int size);
int n = 1;
cout << "Please enter the size of the matrix, it must be an odd number: " << endl;
cin >> n;
cout << "You must choose an odd number! Choose Again: " << endl;
void fillMagicSquare(int newarray[n], int size)
size = n2;
int nsqr = n2 * n2;
int i=0, j=n2/2;