Results 1 to 3 of 3
Thread: Help with Magic square program
- 02-16-2011, 02:31 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Help with Magic square program
I'm writing a program that will take in a user-inputed sequence and determine if what they entered is a magic square. So I need to test: 1) did the user enter n^2 numbers, 2) there are no repeated numbers, and 3) row, column, and diagonal sums are all the same. I am having trouble coming up with a way to do 1 and 3.
I am completely stumped on 1) but here is what I have for 3):
I check row sums first, taking the complete sum of all the rows and if that is not divisible by the number of rows, it means that not all the sums were the same so it returns false (not a magic square). I attempted to do the same sort of procedure with the columns but I can't control the indices the same because they jump by row (don't increase by one each time). How should I deal with that? And how should I go about checking the diagonals?Java Code:int nrows, ncolumns, k = 0, rsum = 0, csum = 0; nrows = (int)Math.ceil(Math.sqrt((double) square.size())); ncolumns = nrows; for (int r = 0; r < nrows; r++) { while (k < ncolumns) { rsum += square.get(k); k++; } } if (rsum%nrows != 0) return false; k = 0; }
Thanks. Any help appreciated.
- 02-16-2011, 03:14 AM #2
- 02-16-2011, 04:10 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Magic squares help
By mjpam in forum New To JavaReplies: 3Last Post: 06-30-2010, 02:24 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


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks