Results 1 to 8 of 8
Thread: Magic Square
- 10-05-2011, 11:16 PM #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:
Any ideas? The above code does not work.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; } } } }
- 10-05-2011, 11:27 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 10-05-2011, 11:30 PM #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?
- 10-05-2011, 11:34 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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)
- 10-05-2011, 11:35 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Magic Square
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).The above code does not work.
-----
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.
- 10-06-2011, 12:19 AM #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...
- 10-06-2011, 12:43 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Magic Square
I don't know what "concept" it is that you're looking for.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 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"...
- 10-06-2011, 01:53 AM #8
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Similar Threads
-
Help with Magic square program
By kkGG in forum New To JavaReplies: 2Last Post: 02-16-2011, 04:10 PM -
Need Help with Magic Square
By easybe in forum New To JavaReplies: 10Last Post: 04-23-2010, 09:39 PM -
Magic square
By gandalf5166 in forum New To JavaReplies: 20Last Post: 04-15-2010, 07:18 PM -
Magic Square!!!... :D
By joms999 in forum New To JavaReplies: 4Last Post: 02-25-2010, 07:55 AM -
Problem using buttons to creat a magic square game
By goldman in forum New To JavaReplies: 5Last Post: 05-05-2008, 04:04 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks