# Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)

• 11-02-2011, 01:50 AM
XxVashxX
Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Got most of the methods done, and just need help the main and first method because they are the only ones I am unsure about in which I am getting incorrect output.

Here are the directions for the first and main methods.

"Write a method with the header
public static boolean allNumbers(int[][] arrTwoD)
that returns true if arrTwoDr contains all integers from 1 to n2, where
n = arrTwoD.length . [Assume that the number of rows in arrTwoD equals
the number of columns in arrTwoD.]
Hint:
• Declare an int[] array called numbers. Allocate n2 elements for this
array.
• Declare a boolean variable called flag and set it to true.
• Initialize each element of numbers to 0.
• Use a nested for loop to inspect every value in arrTwoD. Suppose m
is a value stored in the two-dimensional array, arrTwoD
o If m is less than 1 or greater than n2, then set flag to false.
o Otherwise
if numbers[m - 1] = 0 then set numbers[m - 1] = 1
if numbers[m - 1] = 1 then set flag to false.
• Return the value of flag."

"Write a main method with the following features
1. Declare and initializes the two-dimensional array.
DO NOT READ IN THE VALUES FROM THE KEYBOARD.
Here is an example of Java code that initializes a 4 by 4 matrix.
int[][] arr = { {16, 3, 2, 13}, {5, 10, 11, 8}, { 9, 6, 7, 12}, {4, 15,
14, 1}}
2. Declare a boolean variable called flag.
Assign flag a value with the statement
flag = allNumbers(arr);
3. If flag is true, then declare an int variable called value. Assign the
integer returned by the sumLRDiag method to this variable.
4. Set flag to false if the value returned by sumRLDiag is not equal to
value.
5. If flag is true, then use a loop to find the sum of each row. If any
sum is not equal to value, then set flag to false.
6. If flag is true, then use a loop to find the sum of each column. If any
sum is not equal to value, then set flag to false.
7. The last line of the main method generates output that tells us if our
matrix is a majic square. If flag is true then display the message
“The matrix contains a perfect square”. If flag is false, the display,
“The matrix is not a perfect square”.
Test your program with the following data
int[][] arr = { {16, 2, 3, 13}, {5, 11, 10, 8},
{ 9, 7, 6, 12}, {4, 14, 15, 1}};
int[][] arr = {{1,1}, {2, 3}};"

Heres my current code:
Code:

```public class MagicSquare {     public static boolean allNumbers(int[][] arrTwoD) {         int[] numbers = new int[arrTwoD.length * arrTwoD.length];         boolean flag = true;         for (int i = 0; i < numbers.length; i++) {             numbers[i] = 0;         }         for (int j = 0; j < arrTwoD.length; j++) {             for (int k = 0; k < arrTwoD[j].length; k++) {                 int m = arrTwoD[j][k];                 if (m < 1 || m > arrTwoD.length * arrTwoD.length) {                     flag = false;                 } else if (numbers[m - 1] == 0) {                     numbers[m - 1] = 1;                 } else if (numbers[m - 1] == 1) {                     flag = false;                 }             }         }         return flag;     }     public static int sumLRDiag(int[][] arr) {         int sum = 0;         for (int i = 0; i < arr.length; i++) {             sum = arr[i][i] + sum;         }         return sum;     }     public static int sumRLDiag(int[][] arr) {         int sum = 0;         for (int i = 0; i < arr.length; i++) {             sum = sum + arr[i][arr.length - 1 - i];         }         return sum;     }     public static int sumRow(int[][] arr, int i) {         int sum = 0;         for (int j = 0; j < arr[i].length; j++) {             sum = sum + arr[i][j];         }         return sum;     }     public static int sumCol(int[][] arr, int j) {         int sum = 0;         for (int i = 0; i < arr[j].length; i++) {             sum = sum + arr[i][j];         }         return sum;     }     public static void main(String[] args) {         int[][] arr = {{16, 2, 3, 5}, {13, 11, 10, 8},             {9, 7, 6, 12}, {4, 14, 15, 1}};         boolean flag = allNumbers(arr);         if (flag == true) {             int value = sumLRDiag(arr);             if (sumRLDiag(arr) != value) {                 flag = false;             } else {                 for (int i = 0; i < arr.length; i++) {                     int sum = sumRow(arr, i);                     if (sum != value) {                         flag = false;                     }                 }                 if (flag == true) {                     for (int j = 0; j < arr.length; j++) {                         int sum = sumCol(arr, j);                         if (sum != value) {                             flag = false;                         }                     }                 }             }         }         if (flag == true) {             System.out.println("The matrix contains a perfect square.");         } else {             System.out.println("The matrix is not a perfect square.");         }     } }```
• 11-02-2011, 03:14 AM
Fubarable
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Quote:

Originally Posted by XxVashxX
Got most of the methods done, and just need help the main and first method because they are the only ones I am unsure about in which I am getting incorrect output.

Here are the directions for the first and main methods.

"Write a method with the header
public static boolean allNumbers(int[][] arrTwoD)
that returns true if arrTwoDr contains all integers from 1 to n2, where
n = arrTwoD.length . [Assume that the number of rows in arrTwoD equals
the number of columns in arrTwoD.]
Hint:
• Declare an int[] array called numbers. Allocate n2 elements for this
array.
• Declare a boolean variable called flag and set it to true.
• Initialize each element of numbers to 0.
• Use a nested for loop to inspect every value in arrTwoD. Suppose m
is a value stored in the two-dimensional array, arrTwoD
o If m is less than 1 or greater than n2, then set flag to false.
o Otherwise
if numbers[m - 1] = 0 then set numbers[m - 1] = 1
if numbers[m - 1] = 1 then set flag to false.
• Return the value of flag."

"Write a main method with the following features
1. Declare and initializes the two-dimensional array.
DO NOT READ IN THE VALUES FROM THE KEYBOARD.
Here is an example of Java code that initializes a 4 by 4 matrix.
int[][] arr = { {16, 3, 2, 13}, {5, 10, 11, 8}, { 9, 6, 7, 12}, {4, 15,
14, 1}}
2. Declare a boolean variable called flag.
Assign flag a value with the statement
flag = allNumbers(arr);
3. If flag is true, then declare an int variable called value. Assign the
integer returned by the sumLRDiag method to this variable.
4. Set flag to false if the value returned by sumRLDiag is not equal to
value.
5. If flag is true, then use a loop to find the sum of each row. If any
sum is not equal to value, then set flag to false.
6. If flag is true, then use a loop to find the sum of each column. If any
sum is not equal to value, then set flag to false.
7. The last line of the main method generates output that tells us if our
matrix is a majic square. If flag is true then display the message
“The matrix contains a perfect square”. If flag is false, the display,
“The matrix is not a perfect square”.
Test your program with the following data
int[][] arr = { {16, 2, 3, 13}, {5, 11, 10, 8},
{ 9, 7, 6, 12}, {4, 14, 15, 1}};
int[][] arr = {{1,1}, {2, 3}};"

Heres my current code:
Code:

```public class MagicSquare {     public static boolean allNumbers(int[][] arrTwoD) {         int[] numbers = new int[arrTwoD.length * arrTwoD.length];         boolean flag = true;         for (int i = 0; i < numbers.length; i++) {             numbers[i] = 0;         }         for (int j = 0; j < arrTwoD.length; j++) {             for (int k = 0; k < arrTwoD[j].length; k++) {                 int m = arrTwoD[j][k];                 if (m < 1 || m > arrTwoD.length * arrTwoD.length) {                     flag = false;                 } else if (numbers[m - 1] == 0) {                     numbers[m - 1] = 1;                 } else if (numbers[m - 1] == 1) {                     flag = false;                 }             }         }         return flag;     }     public static int sumLRDiag(int[][] arr) {         int sum = 0;         for (int i = 0; i < arr.length; i++) {             sum = arr[i][i] + sum;         }         return sum;     }     public static int sumRLDiag(int[][] arr) {         int sum = 0;         for (int i = 0; i < arr.length; i++) {             sum = sum + arr[i][arr.length - 1 - i];         }         return sum;     }     public static int sumRow(int[][] arr, int i) {         int sum = 0;         for (int j = 0; j < arr[i].length; j++) {             sum = sum + arr[i][j];         }         return sum;     }     public static int sumCol(int[][] arr, int j) {         int sum = 0;         for (int i = 0; i < arr[j].length; i++) {             sum = sum + arr[i][j];         }         return sum;     }     public static void main(String[] args) {         int[][] arr = {{16, 2, 3, 5}, {13, 11, 10, 8},             {9, 7, 6, 12}, {4, 14, 15, 1}};         boolean flag = allNumbers(arr);         if (flag == true) {             int value = sumLRDiag(arr);             if (sumRLDiag(arr) != value) {                 flag = false;             } else {                 for (int i = 0; i < arr.length; i++) {                     int sum = sumRow(arr, i);                     if (sum != value) {                         flag = false;                     }                 }                 if (flag == true) {                     for (int j = 0; j < arr.length; j++) {                         int sum = sumCol(arr, j);                         if (sum != value) {                             flag = false;                         }                     }                 }             }         }         if (flag == true) {             System.out.println("The matrix contains a perfect square.");         } else {             System.out.println("The matrix is not a perfect square.");         }     } }```

Please explain how it's not working, what is happening that shouldn't be happening and what isn't happening that should. Also if you have any error messages, please post the complete messages.
• 11-02-2011, 03:19 AM
XxVashxX
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Quote:

Originally Posted by Fubarable
Please explain how it's not working, what is happening that shouldn't be happening and what isn't happening that should. Also if you have any error messages, please post the complete messages.

There are no compiler/runtime errors. I tried testing the program with a matrix of int[][] arr = {1, 1}, {1, 1}, which is obviously a magic square, although it states it is not. Having no compiler/runtime errors, it is hard for me to distinguish where the problem is.
• 11-02-2011, 03:28 AM
Junky
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
You can use the poor man's debugger. Insert print statements all over the place to display the values of your variables. You can then see if they match your expectations.
• 11-02-2011, 05:30 AM
XxVashxX
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Quote:

Originally Posted by Junky
You can use the poor man's debugger. Insert print statements all over the place to display the values of your variables. You can then see if they match your expectations.

No use. I can't figure out what the matter is. Couldn't someone just actually take a look at the code/instructions?
• 11-02-2011, 06:06 AM
Fubarable
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Try again. If it doesn't work, show us what you've tried. Come on -- you've got to learn how to debug if you want to succeed at this.
• 11-02-2011, 06:42 AM
XxVashxX
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Quote:

Originally Posted by Fubarable
Try again. If it doesn't work, show us what you've tried. Come on -- you've got to learn how to debug if you want to succeed at this.

Yeah, ok thanks. I come here for help, and you tell me to keep trying on my own. =\
I'm out of ideas, which is why I'm here.
• 11-02-2011, 08:28 AM
pbrockway2
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
I agree with Fubarable's useful idea.

There are four places in main() where you assign a value to flag. Using System.out.println() to determine which of those assignments led to flag being set to false fairly rapidly makes it obvious why you get the result you get for the input you used.
• 11-02-2011, 09:07 AM
JosAH
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Quote:

Originally Posted by XxVashxX
int[][] arr = {1, 1}, {1, 1}, which is obviously a magic square, although it states it is not.

A 2x2 magic square should contain the numbers 1, 2, 3, 4 (== 2x2); your square doesn't. b.t.w. a 2x2 magic square doesn't exist; 3x3 is the smallest possible size.

kind regards,

Jos
• 11-02-2011, 01:54 PM
CHLim
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Quote:

Originally Posted by JosAH
A 2x2 magic square should contain the numbers 1, 2, 3, 4 (== 2x2); your square doesn't. b.t.w. a 2x2 magic square doesn't exist; 3x3 is the smallest possible size.

kind regards,

Jos

I've doubled checked your code both by hand and by compiling it on my computer. And the conclusion is, it works!. Though JosAh is right using {1,1},{1,1} will give you false because its not a magic square. However if you enter a correct magic square ie {{2, 7, 6}, {9, 5 ,1}, {4, 3, 8}} you can see that its correct.

Just to make it a little clearer, see this part of your code:
Code:

``` public static boolean allNumbers(int[][] arrTwoD) {         int[] numbers = new int[arrTwoD.length * arrTwoD.length];         boolean flag = true;         for (int i = 0; i < numbers.length; i++) {             numbers[i] = 0;         }         for (int j = 0; j < arrTwoD.length; j++) {             for (int k = 0; k < arrTwoD[j].length; k++) {                 int m = arrTwoD[j][k];                 if (m < 1 || m > arrTwoD.length * arrTwoD.length) {                     flag = false;                 } else if (numbers[m - 1] == 0) {                     numbers[m - 1] = 1;                 } else if (numbers[m - 1] == 1) {                     flag = false;                 }             }         }         return flag;     }```
if you are using {1,1},{1,1} when you go into the loop where j = 1 you get the flag false because number[0] = 1.

Hope that helps.

P.s it would be nice if you add in a little bit of commentary to your code to make it more readable.
• 11-02-2011, 06:33 PM
XxVashxX
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Quote:

Originally Posted by CHLim
I've doubled checked your code both by hand and by compiling it on my computer. And the conclusion is, it works!. Though JosAh is right using {1,1},{1,1} will give you false because its not a magic square. However if you enter a correct magic square ie {{2, 7, 6}, {9, 5 ,1}, {4, 3, 8}} you can see that its correct.

Just to make it a little clearer, see this part of your code:
Code:

``` public static boolean allNumbers(int[][] arrTwoD) {         int[] numbers = new int[arrTwoD.length * arrTwoD.length];         boolean flag = true;         for (int i = 0; i < numbers.length; i++) {             numbers[i] = 0;         }         for (int j = 0; j < arrTwoD.length; j++) {             for (int k = 0; k < arrTwoD[j].length; k++) {                 int m = arrTwoD[j][k];                 if (m < 1 || m > arrTwoD.length * arrTwoD.length) {                     flag = false;                 } else if (numbers[m - 1] == 0) {                     numbers[m - 1] = 1;                 } else if (numbers[m - 1] == 1) {                     flag = false;                 }             }         }         return flag;     }```
if you are using {1,1},{1,1} when you go into the loop where j = 1 you get the flag false because number[0] = 1.

Hope that helps.

P.s it would be nice if you add in a little bit of commentary to your code to make it more readable.

Aha. thanks for the help everyone. I'm truly greatful you all have the time to help out a beginner like me. Makes complete sense now.
Bring in the thread closer, boys.
• 11-02-2011, 07:38 PM
KevinWorkman
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)