We help IT Professionals succeed at work.

Magic Square C#

jmkotman
jmkotman asked
on
Medium Priority
3,966 Views
Last Modified: 2008-01-09
Write a program which:
--Writes a C# console application program that finds all possible solutions for 3 X 3 Magic Square.
--Use a recursive function to find all the possible permutation of numbers 1 to 9 in the grid.
--Write a method TestMagic to check if a given solution is a solution of the Magic Square.
--Print out to the screen all the possible solutions.
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Looks like a homework assignment to me...

Show us your progress so far and let us know specifically where you are having trouble.
Graphics Expert
CERTIFIED EXPERT
Commented:
Hi jmkotman,

I think you should first create an algorithm, by using some pseudo code or simple description.
Don't go to C# now. First try to find how to programmactly write all possible sequences and how to check each one for the correct solution. May be you can discard some combinations, thus avoiding unnecessary CPU work.

If you aren't able to have a recursive algorithm right now, go ahead, with any algorithm you believe as feasible.

Can you calculate, in advance, how many permutations are possible? It is an important information, because there are problems that spend millions of years to be solved... I don't think it is the case, but, how many?

You may want post the algorithm you have in mind. Don't worry about correctness, write what you think reasonable. No problem if it has errors. I'm sure that, as long you solve the algorithm, you'll write the code.

Jose

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
If efficiency is important to you, and it may not be since 9! is small enough to solve in a reasonable time,
then you may want to prune partial solutions as soon as you can prove that it cannot be part of a magic square so you can skip over entire sets of recursive calls.
But before worrying about that, it may help if you can just start working on any small part of the problem that you do understand.  The experience you gain on that could help your understanding of how other parts mighr be handled, and if you show us what you are doing we should be better able to know how what kind of guidance you need.

Commented:
I would suggest going reverse in this particular case.
 - first, write a method that will print the 3x3 square that you supply to it. this is so that you can see what you are doing and so that you can print the square at will [during debugging process].
 - second, write TestMagic. this should be pretty easy, just check for the sums for each row, column & two diagonals.
 - before you begin writing recursive function, come up with an algorithm. think how would you list all permutations on paper. permutations of 3-4 unique numbers seems to be a good choice. you can then extend the method to use more numbers.
Jose ParrotGraphics Expert
CERTIFIED EXPERT

Commented:
Hi, jmkotman,

I'd like hear from you about the progress...

Jose
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.