Results 1 to 3 of 3
  1. #1
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default 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):

    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;
    }
    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?

    Thanks. Any help appreciated.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by kkGG View Post
    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).
    1 2 3
    4 5 6
    7 8 9

    1+2+3 = 6
    4+5+6 = 15
    7+8+9 = 24

    6+15+24 = 45

    45 / 3 = 15

    According to your logic this is a magic square.

  3. #3
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    1 2 3
    4 5 6
    7 8 9

    1+2+3 = 6
    4+5+6 = 15
    7+8+9 = 24

    6+15+24 = 45

    45 / 3 = 15

    According to your logic this is a magic square.
    Well, I'm not done with the program. I know I still need to sheck the columns and diaganols...that is what I am asking for help with...any idea for how to go about doing that?

Similar Threads

  1. Magic squares help
    By mjpam in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 02:24 PM
  2. Need Help with Magic Square
    By easybe in forum New To Java
    Replies: 10
    Last Post: 04-23-2010, 09:39 PM
  3. Magic square
    By gandalf5166 in forum New To Java
    Replies: 20
    Last Post: 04-15-2010, 07:18 PM
  4. Magic Square!!!... :D
    By joms999 in forum New To Java
    Replies: 4
    Last Post: 02-25-2010, 07: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, 04: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
  •