Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Magic Square

  1. #1
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Magic Square

    I need help.

    Let's say I have 4x4 matrix like this one:
    Magic Square-msq004.gif

    I need to code a function unique(int[][] square) that -- inspects every value in square checking that each one is a unique integer in the range describes above. This does not require four nested loops...

    For a square to be unique...it has to
    - have each box with a different #, cannot repeat numbers.
    - make sure that every number from 1-16 is in the box.

    If the two above are true, then return true...

    Does anyone know what I would be able to do.. I tried doing something like this:
    Java Code:
    public static boolean unique(int[][]square)    {
            
            
            int[] array=new int[2*(square.length)];
            int count=1;
            
            for(int r=0; r<square.length;r++)
            {
                for(int c=0; c<square[r].length; c++)
                {
                    if(square[r][c]==count)
                    {
                        count++;
                        array[count]=count;
                    
                    }
                }
                    
    }
    }
    Any ideas? The above code does not work.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Magic Square

    Loop through the input and store everything in a Map<Integer, Integer>, Where the key is the number, and the value is the occurrences. If the Map has all the numbers 1-16 as keys and no values > 1, you have a unique cube.

  3. #3
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Magic Square

    What is a map? how do I store values in a "map"? I have never heard of this term. Is this some sort of method in Java? and what is a key?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Magic Square

    A map is a data structure, which is really not java specific. Most languages will have a similar data structure. It basically has a pair of values in each slot, where one is the key and the other is the value. For more information, head here: The Map Interface (The Java™ Tutorials > Collections > Interfaces)

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

    Default Re: Magic Square

    The above code does not work.
    It's a good idea to say why it does not work. Specifically, does it compile? If it does not and you cannot understand the compiler messages, post those messages and say which lines of your code they are referring to. If the code does compile but doesn't do the right thing at runtime, say what it does do and post any runtime stacktrace (runtime error message).

    -----

    Before you get that far - indeed before you even start to write code - think about how you are going to check the square array that is passed as an argument. Be able to describe (using plain language rather than code) any assumptions you make about the array. And be able to describe the steps you are going to take to check the array: your description should be both precise and comprehensive.

  6. #6
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Magic Square

    I think I basically don't have the concept in my head. I've tried numerous ways to solve this problem and I cannot understand or think of any other ways. Basically I'm stuck and I don't know what to do..

    As for maps, I looked over that site and its sounds really confusing. I don't know what any of the code means...

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

    Default Re: Magic Square

    I basically don't have the concept in my head. I've tried numerous ways to solve this problem and I cannot understand or think of any other ways.
    I don't know what "concept" it is that you're looking for.

    I stand by my comment before: this is, first of all, a problem of clear thinking and clear expression. Only then does it start to become a problem of Java code.

    The problem amounts to something like this. You are sitting alone in a room about to take a test. The test is a simple one - you will be asked a single question: "Did you hear the numbers 1 to 16 each said exactly once?". Someone enters the room, says "9", and leaves. Another enters and says "16". Then "3" and so on through all the numbers you gave in your original post. Then the examiner enters and asks "Did you hear the numbers 1 to 16 each said exactly once?"

    Can you answer the question accurately? If so how did you determine the answer?

    -----

    I don't think maps are needed.

    My memory is bad. If getting the answer were really important I might look around around for something to help me out. Luckily I have 16 coins in my pocket. Before the first person enters the room I arrange them in a row across my desk. I carefully place them so they are all "tails" up. I am on to something and I have a plan that I can begin to carry out when the first person announces "9"...
    sunde887 likes this.

  8. #8
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: Magic Square

    :O thank you, problem solved. smh......*facepalm*

Similar Threads

  1. Help with Magic square program
    By kkGG in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 05:10 PM
  2. Need Help with Magic Square
    By easybe in forum New To Java
    Replies: 10
    Last Post: 04-23-2010, 10:39 PM
  3. Magic square
    By gandalf5166 in forum New To Java
    Replies: 20
    Last Post: 04-15-2010, 08:18 PM
  4. Magic Square!!!... :D
    By joms999 in forum New To Java
    Replies: 4
    Last Post: 02-25-2010, 08:55 AM
  5. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 05:04 AM

Posting Permissions

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