Solved

Magic Square C#

Posted on 2006-11-05
2
3,761 Views
Last Modified: 2012-06-22
I am writing a Magic Square program that finds all the possible combinations of 1 to 9.  I wrote the recursive part of it but it seems to just be going on forever.  I want to print out all the possible ways of 1 to 9 without any repetitions.  Is this right?
using System;

namespace ConsoleApplication4
{
      class Class1
      {
            [STAThread]
            static void Main(string[] args)
            {
                  int n=1;
                  Magic(n);
            }

            public static void Magic(int n)
            {
                  int[] A = new int[10];
                  int[] B = new int[10];

                  for(int i = 1; i <= 9; i++)
                  {
                        if(B[i] == 0) // if i is free
                        {
                              B[i]=1;
                              A[n]=i;

                              if(n<9)
                              {
                                    Magic(n+1);//recurse
                              }
                              else
                              {
                                    TestMagic();//at the end
                              }

                              B[i]=0;
                        }
                        Console.Write(A[n]);
                  }
            }

            public static void TestMagic()
            {

            }
      }
}
0
Comment
Question by:jmkotman
2 Comments
 
LVL 16

Accepted Solution

by:
muzzy2003 earned 500 total points
ID: 17877666
No, this code gets in an infinite loop. Try something like this:

namespace ConsoleApplication
{
    class Class1
    {
        static int[] A = new int[9];

        [STAThread]
        static void Main(string[] args)
        {
            for (int i = 0; i < 9; i++)
            {
                Array.Clear(A, 0, 9);
                A[0] = i + 1;
                Magic(1);
            }
        }

        public static void Magic(int n)
        {
            for (int i = 0; i < 9; i++)
            {
                bool newNumber = true;
                for (int j = 0; j < n; j++)
                {
                    if (A[j] == i + 1)
                    {
                        newNumber = false;
                        break;
                    }
                }
                if (newNumber)
                {
                    A[n] = i + 1;
                    if (n == 8)
                        Print();
                    else
                        Magic(n + 1);
                }
            }
        }
       
        private static void Print()
        {
            Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}{7}{8}", A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8]);
        }
    }
}
0
 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17880013
you could do what muzzy2003;
or just exchange this part:
          public static void Magic(int n)
          {
         static      int[] A = new int[10];
          static     int[] B = new int[10];

               for(int i = n; i <= 9; i++)
               {

The problem is that the static makes the number remember they were already used, and the i=n makes the n has a better validity, it will
stop as soon as n=9, and will not go depper and depper, even if n is bigger then 9.
I didnt understand why you set again B[i]=0; it will then go forever.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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

12 Experts available now in Live!

Get 1:1 Help Now