Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

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

    Improving the world one idiot at a time!

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

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

    Hi Vash,

    Would you please pay attention to the following thread, and linked threads.
    http://www.javaprogrammingforums.com...rum-rules.html

    Please not that our members shouldn't be complying by this rules for you, i.e. Junky. You will see better solutions and responses from all forums if you follow the rules of all the respected forums.

    As you will see some forums do not allow cross-posting.


    Chris

Similar Threads

  1. Type magic
    By meathead in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2011, 12:12 PM
  2. [SOLVED] isPrime Boolean Method returns true for squares of primes
    By Staticity in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 29th, 2011, 11:46 PM
  3. How to make Java determine if a values is an integer or not.
    By MiniatureBeast in forum Object Oriented Programming
    Replies: 5
    Last Post: August 17th, 2011, 11:32 PM
  4. [SOLVED] Help; algorithm to determine 'range'
    By b_jones10634 in forum Algorithms & Recursion
    Replies: 13
    Last Post: August 24th, 2010, 04:57 PM
  5. Magic Squares, input confusion
    By bengiles89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 08:40 PM