1. Member
Join Date
Jun 2010
Posts
1
Rep Power
0

## 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. 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. Member
Join Date
Dec 2007
Location
Mumbai, India
Posts
37
Rep Power
0
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. 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

#### Posting Permissions

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