aspose file tools*
The moose likes Beginning Java and the fly likes help with magic square Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with magic square" Watch "help with magic square" New topic
Author

help with magic square

Mike Currier
Greenhorn

Joined: Apr 02, 2012
Posts: 1
Hi everyone,
I was working on this and ran into a problem. When i try to find the sum of the columns and rows it goes and adds all of the numbers in the array instead of one column/row at a time. Is there a way I can modify my code to make it so it will return the sum of a single row/column to determine if its magic. Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Mike Currier wrote:When i try to find the sum of the columns and rows it goes and adds all of the numbers in the array instead of one column/row at a time.


Yes, that's right, it does.

Is there a way I can modify my code to make it so it will return the sum of a single row/column to determine if its magic.


Sure. But are you sure that's what you want to do? I thought the rule was that each row and each column had to add up to the magic number. So just choosing one row and one column wouldn't be an adequate test, would it?

Seems to me that you want to return all of the row sums and all of the column sums. In that case returning a single number for rowSum and a single number for columnSum is the wrong design; you want to return an array of numbers for each of those things.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7801
    
  21

Mike Currier wrote:Is there a way I can modify my code to make it so it will return the sum of a single row/column to determine if its magic.

Not an answer, but a general tip: create methods to do what you want, eg:
public int rowSum(int row, int[][] array) { ...
and then call them from your isMagic() method.

Creating methods helps to isolate logic, which hopefully makes things clearer for you (and also makes things easier to test).

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with magic square