Creating a Magic Square in java

ch1maera picture ch1maera · Mar 16, 2016 · Viewed 34.3k times · Source

I have to write a program that takes in an odd number from the user and creates a magic square. A magic square is one where the sum of each row, column, and diagonal is the same. These are the characteristics for writing the code:

  1. Ask the user for an odd number
  2. Create an n by n array.
  3. Follow these steps to create a magic square.
    a. Place a 1 in the middle of the first row.
    b. Subtract 1 from the row and add 1 to the column.
    i. If possible place the next number at that position.
    ii. If not possible, follow these steps.
    1. If in row -1, then change to last row
    2. If in last column change to first column
    3. If blocked, then drop down to next row (from original position)
    4. if in the upper right corner, then drop down to next row.
  4. Print the array

I've written the code but when I run it, the program puts in all the numbers except for the number two; for some reason, my program skips over it. For example, if I put in the number 3 as the odd number, my output is:

6 1 0 
3 4 5 
9 7 8 

0 isn't supposed to be there but the number two is. Here is my code:

public static void main(String[] args) {
    System.out.print("Give an odd number: ");
    int n = console.nextInt();
    int[][] magicSquare = new int[n][n];

    int number = 1;
    int row = 0;
    int column = n / 2;
    while (number <= n * n) {
        magicSquare[row][column] = number;
        number++;
        row -= 1;
        column += 1;
        if (row == -1) {
            row = n - 1;
        }
        if (column == n) {
            column = 0;
        }
        if (row == 0 && column == n - 1) {
            column = n - 1;
            row += 1;
        } else if (magicSquare[row][column] != 0) {
            row += 1;
        }
    }

    for (int i = 0; i < magicSquare.length; i++) {
        for (int j = 0; j < magicSquare.length; j++) {
            System.out.print(magicSquare[i][j] + " ");
        }
        System.out.println();
    }
}

Could someone tell me where I went wrong and why my program is skipping the number 2? *This is a homework question so code only answers, please. Thanks.

Answer

ManoDestra picture ManoDestra · Mar 16, 2016

The logic of your square ensures that the top right corner is never written to.

if in the upper right corner, then drop down to next row.

And here's the code that does it...

if (row == 0 && column == n - 1) {
    column = n -1;
    row += 1;

So, you're always going to move away from that location before you enter any value on your next iteration. You don't actually need that line that sets the column to n - 1 as it's already n - 1 by definition of the logic above it.

You're actually writing the value 2 to the second row, third column. It is then later overwritten by the value 5. If you output the values of your array after each iteration of your program, then you'll see how the state of your model is changing.