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
Solved

Magic Square C#

Posted on 2006-10-31
5
3,666 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

860 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