1. Member
Join Date
Sep 2011
Location
Washington DC
Posts
51
Rep Power
0

## Magic Square

I need help.

Let's say I have 4x4 matrix like this one:

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. ## 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. Member
Join Date
Sep 2011
Location
Washington DC
Posts
51
Rep Power
0

## 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. ## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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. Member
Join Date
Sep 2011
Location
Washington DC
Posts
51
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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"...

8. Member
Join Date
Sep 2011
Location
Washington DC
Posts
51
Rep Power
0

## Re: Magic Square

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

#### Posting Permissions

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