Advertisement

10.01.2008 at 10:06PM PDT, ID: 23780540
[x]
Attachment Details

Magic Square with variable dimensions

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6
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.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
#include <iostream>
using namespace std;
 
void fillMagicSquare(int newarray[][n], int size);
 
 
int main()
{
int n = 1;
cout << "Please enter the size of the matrix, it must be an odd number: " << endl;
 
	while (true)
	{
			cin >> n;
			int magic_square[n][n];
			if (n%2==0)
				{
					cout << "You must choose an odd number! Choose Again: " << endl;
				}
			else
				{
				fillmagicsquare(arra, n);
				}
	}
}
 
 
 
void fillMagicSquare(int newarray[][n], int size)
{
size = n2;
int nsqr = n2 * n2;
int i=0, j=n2/2;
 
}
Answered By: Infinity08
Expert Since: 07/08/2003
Accepted Solutions: 3115
Computer Expertise: Advanced
Infinity08 has been an Expert for 5 years 6 months, during which he has posted 17591 comments and answered 3115 questions. Infinity08 is just one of 346 experts in the Microsoft Visual C++ Zone. 4 experts collaborated on this answer, which was graded an "A" by the asker.
 
 
 
 
20081119-EE-VQP-47 / EE_QW_2_20070628