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

• November 1st, 2011, 08:14 PM
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."); } } }```

• November 1st, 2011, 08:57 PM
Junky
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
• November 2nd, 2011, 05:22 AM
Freaky Chris
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Hi Vash,