Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Magic square class (modular math problem)

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Magic square class (modular math problem)

    Hi, I'm new to Java. I am trying to write a program that will print magic squares of odd order.

    Here is the code:

    class MagicSquare
    {
         public static void main (String args[])
         {
              if(args.length > 1)
              {
                   System.out.println("Too many arguments entered");
              }
              else
              {
                   int sqrOrd = Integer.parseInt(args[0]);
                   int magSqr[][] = new int [sqrOrd][sqrOrd];
                   if (sqrOrd % 2 == 0)
                   {
                        System.out.println("Square order is even");
                   }
                   else
                   {
                        int rowNum;
                        int colNum;
     
                        for (rowNum = 0; rowNum < sqrOrd; rowNum++)
                        {
                             for (colNum = 0; colNum < sqrOrd; colNum++)
                             {
                                  magSqr[rowNum][colNum] = -1;
                                  //System.out.print(magSqr[rowNum][colNum]);
                             }
                             System.out.println();
                        }
     
                        int maxNum = sqrOrd * sqrOrd;
                        rowNum = 0;
                        colNum = sqrOrd / 2 + 1;
                        magSqr[rowNum][colNum] = 1;
     
                        for (int curNum = 1; curNum <= maxNum; curNum++)
                        {
                             if (magSqr[(rowNum - 1 + sqrOrd) % sqrOrd][(colNum + 1) % sqrOrd] == -1)
                             {
                                  rowNum = (rowNum - 1 + sqrOrd) % sqrOrd;
                                  colNum = (colNum - 1 + sqrOrd) % sqrOrd;
                             }
                             else
                             {
                                  rowNum = (rowNum + 1) % sqrOrd;
                             }
                             magSqr[rowNum][colNum] = curNum;
                        }
                        for (rowNum = 0; rowNum < sqrOrd; rowNum++)
                        {
                             for (colNum = 0; colNum < sqrOrd; colNum++)
                             {
                                  System.out.print(magSqr[rowNum][colNum] + "  ");
                             }
                             System.out.println();
                        }
                   }
              }
         }       
    }

    My main problem is that, while the code compiles without any errors or warnings, the program does not print a magic square; it print an array where only some of the elements are replaced by the numbers 1 through n^2.

    I think I am having some problem with the math controlling the if statements. Any help would be appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Magic square class (modular math problem)

    See Magic squares help - Java Forums

Similar Threads

  1. Magic Squares, input confusion
    By bengiles89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 08:40 PM
  2. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  3. switch and math problem issues
    By emartino in forum Java Theory & Questions
    Replies: 1
    Last Post: January 19th, 2010, 05:34 PM
  4. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM
  5. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM