• Post Reply Bookmark Topic Watch Topic
  • New Topic

2d array with a magic square  RSS feed

 
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a problem here, i cant figure out what is wrong with my code, the elements in the arrays form a magic square. But i keep getting false when i run  the program? im very new with the concept of 2d arrays.
Can i get some direction on this please.


 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile the code it gave me an error that says.
java: missing return statement
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are correct, im working on fixing it, and improving it.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to supply a working example before we can help you.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remind us what a Latin Square does. Do all the columns, all the rows, and both diagonals add up to the same total?
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allrigth i think i got it to work. so a magic square definition is :  the values in each vertical, horizontal, and diagonal row add up to the same value. in this case total is  = 34, for rows , columns and diagonals.
could someone let me know if im missing something. i've changed the values on various indices it seems to work.

 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is a square with all the same numbers a magic square? Can there be any duplicate numbers in a magic square?
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would say no, i must have looked a tens of examples, and there is no number repetition in any of them.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the wikipedia entry on Magic Squares:
... a magic square is a square grid (equal number of rows and columns) filled with distinct numbers such that the numbers in each row, and in each column, as well as the numbers in the main and secondary diagonals, all add up to the same value, called the magic constant.

Yup, so technically, your program is not yet complete.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i cant see what im missing. what am i missing?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, your program returns true for this input:

That is not correct.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol , its does the opposite of what is supposed to. Im not sure what im missing here. can i get a clue?
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i was missing the check for rows and columns , how is this for rows and columns check,

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clue #1: What makes a square like that NOT a magic square?

Clue #2: distinct numbers

Clue #3: "distinct" - not alike, different
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont follow, the numbers on my array are distinct.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you're writing a program that only works properly for one set of input? Shouldn't it work for all possible inputs?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about arrays that are not square? Does your program reject those?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you modified your program a little bit, you could use this to test your solution:

The strange looking comments are just special comments for Eclipse, so it doesn't reformat the code when I use the Ctrl+Shift+F keyboard shortcut.  A correct implementation of MagicSquareChecker should output:
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


run:
Is magic square: false
BUILD SUCCESSFUL (total time: 0 seconds)
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've done some changes and checked with all the sets of numbers you provided, it works with all sets except the one with all ones, that returns true.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose that could be good enough for purposes of a grade, unless you really want to implement that condition.

One thing you should do is reformat your code so that it's properly aligned and indented. Right now your code looks like someone with crooked teeth who has never been to a dentist / orthodontist.

In case you're curious how my code looks, here's a little part of it:

This is just the high-level code. The detailed calculations occur inside all those methods being called from here.  If you break your program down into smaller chunks, it's a lot easier to get a handle on the complex logic involved in evaluating whether you have a magic square or not.  The only thing you're missing from your solution is something that checks what my hasDistinctNumbers() method checks.

Edit: you're probably missing the logic that's in my isSquare() method, too.  Does your code gracefully handle the following input?

What about this?

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes in know my code looks as crooked as my teeth before braces, we are being forced to write our code on an editor that has no autocomplete or auto format features, its cool because it helps train my eye to formatting errors. i thought it looked good though. But i will retype, if you can see it crooked that means my professor will pull out a piece of string and measure the code alignment on the screen. and i will be looked at funny lol.
regarding the new set of numbers: they return false on both instances.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:its cool because it helps train my eye to formatting errors. i thought it looked good though.

If your actual code looks anything like what it looks like in your posts, no, it's not quite there yet. It's not horrible but it can definitely be improved. More practice.

I kind of get why an instructor might force you to use an editor that doesn't have the conveniences that modern IDEs like Eclipse and NetBeans have but I don't agree with it. That's like forcing engineering students to use slide rules for an entire course instead of scientific/engineering calculators. Sure, it can be educational to know how to do it the "old school" way but at some point you have to modernize so you don't have to deal with the tedium of the old ways of doing things. Work smarter, not harder.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:yes in know my code looks as crooked as my teeth before braces, we are being forced to write our code on an editor that has no autocomplete or auto format features, its cool because it helps train my eye to formatting errors...

It's also a good idea anyway, because it forces you to learn how the Java language actually works - eg, how to compile and jar classes, correct (and avoid) errors, and use the API docs. Lots of people here who've only ever used an IDE basically never learn that stuff, or rely on the IDE to do it for them; so kudos to your teacher.

I notice that you haven't yet tackled the "distinctive" part yet, so here's a tip for you if and when you decide to:

Rather than try and work out if a particular value is the same as any of the ones you've already used - which can be time-consuming and fiddly - Java has an interface called Set which specifically defines a collection of distinct items. This, in turn, defines a method called add(value) that you'll probably find very useful. Have a look at the link and see if anything leaps out at you.

HIH

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!