Results 1 to 4 of 4
  1. #1
    mjpam is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default Magic squares help

    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:

    Java 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
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Can you explain the logic of your program?
    problem with the math controlling the if statements
    Please explain what the logic for each if statement and what is wrong with it.

  3. #3
    felixtfelix is offline Member
    Join Date
    Dec 2007
    Location
    Mumbai, India
    Posts
    37
    Rep Power
    0

    Default

    Here you go,

    public class MagicSquare {
    private int n;
    private int square[][];

    public MagicSquare(int n){
    this.n = n;
    square = new int[n][n];
    }

    public void initialize(){
    for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
    square[i][j] = 0;
    }
    }
    square[0][n / 2] = 1;
    }

    public void construct(){
    initialize();

    int rowNum = 0;
    int colNum = n / 2;

    for(int i=2;i <= n * n;i++) {
    if (square[(rowNum - 1 + n) % n][(colNum + 1) % n] == 0){
    rowNum = (rowNum - 1 + n) % n;
    colNum = (colNum + 1) % n;
    }else{
    rowNum = (rowNum + 1) % n;
    }
    square[rowNum][colNum] = i;
    }
    print();
    }

    public void print(){
    for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
    System.out.print(square[i][j] + " ");
    }
    System.out.println();
    }
    }

    public static void main (String args[]){

    if(args.length > 1){
    System.out.println("Too many arguments entered");
    System.exit(0);
    }
    else if(Integer.parseInt(args[0]) % 2 == 0){
    System.out.println("Square order is even");
    System.exit(0);
    }
    int num = Integer.parseInt(args[0]);

    MagicSquare square = new MagicSquare(num);
    square.construct();
    }
    }

    One can check the logic in the following link.
    Magic square - Wikipedia, the free encyclopedia

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    I once did a version that doesn't need the nxn square matrix, i.e. it generates the numbers from top to bottom, left to right; it's C code, here it is.

    kind regards,

    Jos

Similar Threads

  1. Magic Squares, input confusion
    By bengiles89 in forum New To Java
    Replies: 6
    Last Post: 04-29-2010, 01:13 AM
  2. Magic squares LUX method
    By gandalf5166 in forum New To Java
    Replies: 14
    Last Post: 04-17-2010, 04:30 PM
  3. Calculating Squares and Cubes in a table
    By aldorfski_17 in forum New To Java
    Replies: 4
    Last Post: 03-22-2010, 07:17 PM
  4. help with perfect squares
    By AmplifiedKid in forum New To Java
    Replies: 1
    Last Post: 09-19-2009, 07:44 PM
  5. Perfect Squares
    By divyachaparala in forum New To Java
    Replies: 4
    Last Post: 02-05-2008, 09:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •