Solved

Magic Square C#

Posted on 2006-10-31
5
3,650 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.
0
Comment
Question by:jmkotman
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17844410
Looks like a homework assignment to me...

Show us your progress so far and let us know specifically where you are having trouble.
0
 
LVL 18

Accepted Solution

by:
JoseParrot earned 500 total points
ID: 17846745
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 17846898
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.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 17849995
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.
0
 
LVL 18

Expert Comment

by:JoseParrot
ID: 17853392
Hi, jmkotman,

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

Jose
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is an explanation of a simple data model to help parse a JSON feed
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now