Results 1 to 12 of 12
 11022011, 01:50 AM #1Member
 Join Date
 Oct 2011
 Posts
 7
 Rep Power
 0
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 twodimensional 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 twodimensional 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:
Java 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."); } } }
Last edited by XxVashxX; 11022011 at 01:59 AM.

Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
 11022011, 03:19 AM #3Member
 Join Date
 Oct 2011
 Posts
 7
 Rep Power
 0
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
 11022011, 03:28 AM #4
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.
 11022011, 05:30 AM #5Member
 Join Date
 Oct 2011
 Posts
 7
 Rep Power
 0

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.
 11022011, 06:42 AM #7Member
 Join Date
 Oct 2011
 Posts
 7
 Rep Power
 0
 11022011, 08:28 AM #8Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
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.
 11022011, 09:07 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,316
 Blog Entries
 7
 Rep Power
 25
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
The only person who got everything done by Friday was Robinson Crusoe.
 11022011, 01:54 PM #10Member
 Join Date
 Nov 2011
 Posts
 24
 Rep Power
 0
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
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:
Java 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; }
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.Last edited by CHLim; 11022011 at 02:36 PM.
 11022011, 06:33 PM #11Member
 Join Date
 Oct 2011
 Posts
 7
 Rep Power
 0
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
Last edited by XxVashxX; 11022011 at 06:52 PM.
 11022011, 07:38 PM #12
Re: Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same)
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
Similar Threads

2d Array Sums Help
By XxVashxX in forum New To JavaReplies: 3Last Post: 10172011, 06:20 PM 
Harmonic sums  Recursive
By überfuzz in forum New To JavaReplies: 4Last Post: 03252011, 11:42 AM 
Magic squares help
By mjpam in forum New To JavaReplies: 3Last Post: 06302010, 02:24 PM 
Magic Squares, input confusion
By bengiles89 in forum New To JavaReplies: 6Last Post: 04292010, 01:13 AM 
Magic squares LUX method
By gandalf5166 in forum New To JavaReplies: 14Last Post: 04172010, 04:30 PM
Bookmarks