Results 1 to 12 of 12
  1. #1
    XxVashxX is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default 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:
    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.");
            }
        }
    }
    Please & Thank You.
    Last edited by XxVashxX; 11-02-2011 at 01:59 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by XxVashxX View Post
    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:
    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.");
            }
        }
    }
    Please & Thank You.
    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.

  3. #3
    XxVashxX is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  5. #5
    XxVashxX is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

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

    Quote Originally Posted by Junky View Post
    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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    XxVashxX is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    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.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by XxVashxX View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

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

    Quote Originally Posted by JosAH View Post
    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:
    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;
        }
    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.
    Last edited by CHLim; 11-02-2011 at 02:36 PM.

  11. #11
    XxVashxX is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

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

    Quote Originally Posted by CHLim View Post
    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;
        }
    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.
    Last edited by XxVashxX; 11-02-2011 at 06:52 PM.

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. 2d Array Sums Help
    By XxVashxX in forum New To Java
    Replies: 3
    Last Post: 10-17-2011, 07:20 PM
  2. Harmonic sums - Recursive
    By überfuzz in forum New To Java
    Replies: 4
    Last Post: 03-25-2011, 11:42 AM
  3. Magic squares help
    By mjpam in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 03:24 PM
  4. Magic Squares, input confusion
    By bengiles89 in forum New To Java
    Replies: 6
    Last Post: 04-29-2010, 02:13 AM
  5. Magic squares LUX method
    By gandalf5166 in forum New To Java
    Replies: 14
    Last Post: 04-17-2010, 05:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •