Need help with method to check if a sqaure is normal.

Hi all,

Please keep in mind that I'm a beginner, not only when it comes to java but also to programming in general. So I hope you'll all be patient with me. Now off to the problem.

For a school assignment I have to write an app that deals with magic squares. Taken directly from my assignment:

A magic square of order n is a square in which all n2 integers are distinct, greater than 0 and are chosen in such a way, that the n numbers in each row, column and diagonal of the square add up to the same constant. A normal or pure square is restricted to containing the integers from 1 to n^2.

My task is to write a code that checks if an arbitrary square is a magical square. The first thing for me to do is ot write a method that checks if the square is a normal square:A normal square is restricted to containing the integers from 1 to n^2.

I came up with this:

public static void isNormal(int[ ][ ] matrix)

{

int temp = 0;

int normal = 0;

outer: for(int i = 0;i<matrix.length;i++){

for(int j = 0;j<matrix.length;j++){

if (matrix[ i ][ j ] != temp && matrix[ i ][ j ]!= 0 && matrix[ i ][ j ] >< matrix.length*matrix.length){

temp = matrix[ i ][ j ] ;

normal = 1;}

else{ normal = 0; break outer;} }}

if(normal == 0)System.out.println("The given square is not a normal sqaure.");

if(normal == 1)System.out.println("The given square is a normal sqaure.");

It compiles without a problem. However, no matter what square I choose normal allways equals 0. So the result is allways that the square is not normal. I just can't figure out what's wrong. Please help me out.

Thanks in advance.

EDIT: For some reason the last condtion in the "if" doesnt display right on the forum. It should be: matrix [ i ] [ j ] smaller than matrix.length*matrix.length

null

[1929 byte] By [IDDQDa] at [2007-11-27 11:20:02]
# 1

Oh Christ I figured it out myself while I was typing my EDIT. The last condtion in the if should be matrix [ i ] [ j ] smaller OR EQUAL TO matrix.length*matrix.length. I feel really silly. Thanks to everyone that was about to help me.!

IDDQDa at 2007-7-29 14:40:15 > top of Java-index,Java Essentials,New To Java...
# 2

I have concerns with your code.

1) using labels and breaks to the label is considered bad form in most cases.

2) you define normal as an int when it truly should be a boolean.

You know that you can test for other conditions in your for loop and break out of the loop if the conditions aren't met.

Something like this looks more natural to me:

public static boolean isNormal(int[][] matrix)

{

int temp = 0;

//int normal = 0; // this is not naturally an int

boolean normal = true; // assume it's true

//outer: for (int i = 0; i < matrix.length; i++) //get rid of

// test that it's still normal in the for loop:

for (int i = 0; i < matrix.length && normal; i++)

{

for (int j = 0; j < matrix.length && normal; j++)

{

if (matrix[i][j] != temp && matrix[i][j] != 0

&& matrix[i][j] <= matrix.length * matrix.length)

{

temp = matrix[i][j];

//normal = 1; do nothing; normal still true

}

else

{

//normal = 0;

//break outer;

normal = false;

}

}

}

return normal; // isNormal is a question, the method should return the answer

}

petes1234a at 2007-7-29 14:40:15 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks for your input. The code is much more elegant that way. I knew about boolean values but have never actually used them.

IDDQDa at 2007-7-29 14:40:15 > top of Java-index,Java Essentials,New To Java...
# 4

An easy way to check if a square is "normal" is to create an array of booleans of size (matrix.length)^2 and then loop through all the values of your matrix. If you encounter a value V that is less than 1 or larger than (matrix.length)^2, return false, else set index V-1 to true in your boolean-array.

After that, loop through your entire boolean-array and if you encounter a false, return false immediately. If the loop ends (thus all values are set to true), return true.

prometheuzza at 2007-7-29 14:40:15 > top of Java-index,Java Essentials,New To Java...
# 5

Thanks guys. I've run enough sqaures through the code to know for sure that it works properly now. I'm going to take a break know, java has got me beat. I'll bump this thread tomorrow, as I still need to write code to check if a square is panmagical or not. Something tells me those broken diagonals will be a royal pain. Cheers again, and have a good day.

IDDQDa at 2007-7-29 14:40:15 > top of Java-index,Java Essentials,New To Java...