1

So i am trying to sum the diagonals and compare it to a magic constant number. If they are the same, then it is a magic square. I was able to write the code for the diagonals however it is showing me incorrect result. My code is here:

 public static boolean checkDiagonals(int[][] array, int magicConstant){
   int total = 0;
   int wrong = 0;
   for(int i = 0; i < array.length; i++){
     total= total + array[i][i] + array[array.length - i-1][array.length - i-1];
   }
   if(total!=magicConstant){
     wrong++;
  }
  System.out.println("There were " + wrong + " invalid diagnals.");
  return wrong == 0;
}

My output is this

Size: 3

1 1 1

5 5 5

9 9 9

1 1 1
5 5 5
9 9 9
The magic constant is 15

There were 1 invalid diagnals.

This is NOT a magic square.

Size: 3

8 1 6

3 5 7

4 9 2

8 1 6
3 5 7
4 9 2
The magic constant is 15

There were 1 invalid diagnals.

This is NOT a magic square.

The correct output should show that the second one is Magic square however my program says it is not.

Why i believe there is something wrong with my code is that i am getting There were 1 invalid diagnals. for every square so there is something wrong here.

Edit I have a problem getting the correct output. I believe it has got something to do with addition of digonals Why is it that it keeps printing 1 invalid diagonal for every square. The output that i have shown is just for 2 squares however when i try it with other squares, it keeps printing 1 invalid diagonal.

Saad
  • 399
  • 8
  • 25
  • Possible duplicate of [Adding the diagonal values in a 2d array](http://stackoverflow.com/questions/6565862/adding-the-diagonal-values-in-a-2d-array) – myselfmiqdad Oct 31 '16 at 02:17
  • No it is not. I am getting the wrong output. My question is not how to add the diagonals. @miqdadamirali – Saad Oct 31 '16 at 02:43
  • can you explain what your for loop is supposed to be computing? step through it on paper and find your mistake – softwarenewbie7331 Oct 31 '16 at 03:00
  • My for loop is supposed to add the diagonals. So instead of nested loops i am using just one for loop that will go through both diagonals and add the values to the `total` variable. The total will then compare it to magic Constant value. @softwarenewbie7331 – Saad Oct 31 '16 at 03:01
  • 1
    so you sum for 2 diagonals and check against the expected value for 1 diagonal? is that expected behavior? If the goal is to 'count' number of incorrect diagonals, you probably cannot use a single loop – softwarenewbie7331 Oct 31 '16 at 03:08
  • No, the magic constant value is calculated like this: (n * (n2 + 1)) / 2 . It is not the sum of any one diagonal. @softwarenewbie7331 – Saad Oct 31 '16 at 03:09
  • Output the total value, please. – MordechayS Oct 31 '16 at 03:12
  • FYI the magic constant is the expected value of every (ONE) diagonal,row,column of the magic square – softwarenewbie7331 Oct 31 '16 at 03:15
  • Okay, i just searched it on Wikipedia. You're right. Now, if i want to use just one loop counter that moves along both diagonals, how will i do that? @softwarenewbie7331 – Saad Oct 31 '16 at 03:18
  • separate the 'total' into total1 and total2, check them separately after the loop – softwarenewbie7331 Oct 31 '16 at 03:18
  • How will i separate total into total1 and total2? Can you please explain a bit. @softwarenewbie7331 – Saad Oct 31 '16 at 03:21

1 Answers1

0

See discussion in comments for more details:

 public static boolean checkDiagonals(int[][] array, int magicConstant){
   int total1 = 0;
   int total2 = 0;
   int wrong = 0;
   for(int i = 0; i < array.length; i++){
     total1= total1 + array[i][i];
     total2 = total2 + array[array.length - i-1][array.length - i-1];
   }
   if(total1!=magicConstant){
     wrong++;
  }
   if(total2!=magicConstant){
     wrong++;
  }
  System.out.println("There were " + wrong + " invalid diagnals.");
  return wrong == 0;
}
  • I was about to say that i got it but then you beat me to it and posted the correct solution. Thank you very much for your time though. I had no idea that the magic constant was sum of each diagonal. Just learned that from you. Much obliged sir! – Saad Oct 31 '16 at 03:30