• Post Reply Bookmark Topic Watch Topic
  • New Topic

Magic Square, array/recursion  RSS feed

 
S. Freeman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Magic square - n × n(4 x 4 in this case) matrix that is filled with the numbers 1, 2, 3, . . ., n2
Conditions: 1.Are the numbers 1, 2, ..., 16 the input.
2. And is the sums of the rows, columns, and diagonals equal to each other?




Question/request for assistance one: i wonder if there is a more optimal way of comparing elements of two arrays. I predefined an array at line 67 expectedArr, and then removed elements from that array that fall into the right-input category. So basically i am wondering how can checking if one array contains all the elements of another arrays be done better, an easier way?

And at line 120. i first ran with return equalSumRowsColumns (arr, a++) && sumOfRow(arr[a]) == sumOfColumn(arr, a); which lead to the following error -> java.lang.StackOverflowError. But when i replaced it with a + 1 there is no error. I thought that maybe a++ 'instantly' changes the value of a so on next uses it is already updated when it shouldn't be, so i changed it to a + 1, but isnt a++ same as a + 1, confused...


Edit1: example of a magic square

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi S. Freeman,

Consider that you are iterating over expectedArr multiple times for every value of arr, and on top of that you're also copying almost the entire array for each element of arr. The answer is yes, there's a more efficient way.

What you really need to do is check whether each element in arr is unique, and none are smaller than 1 or greater than 16. An easy way of checking whether elements are unique, is by putting them all in a set and then seeing if the set is the same size as the original sample group. HashMap<Integer> HashSet<Integer> would be very useful for this purpose. Then, as you're passing every element to the set, you only have to check whether they're within the specified range.

As for your second question, given a=0, a++ does *not* equal 1. Look up "post-increment-operator".
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:HashMap<Integer> would be very useful for this purpose.

You meant HashSet<Integer>, right?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errr yes XD
 
S. Freeman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post increment: a = 0, a++ -> basically i am sending zero over-and over again because its a post increment. Brain-dead cause of confusion there...

HashSet<Integer> is very useful for the problem at hand, thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!