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

