This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

help with magic square

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic