Solved

Magic Square C#

Posted on 2006-10-31
5
3,684 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

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:
Jose Parrot 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:Jose Parrot
ID: 17853392
Hi, jmkotman,

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

Jose
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

630 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