Magic Square C#

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.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Jose ParrotConnect With a Mentor Graphics ExpertCommented:
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.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Looks like a homework assignment to me...

Show us your progress so far and let us know specifically where you are having trouble.
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.
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 ExpertCommented:
Hi, jmkotman,

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.