![]() |
|
#1
|
|||
|
|||
Magic SquareHi
I am new to this forum but i knew that every body is helpful i have trouble with my assignment i need to write a program that implemnt a magic square 3x3 and 4x4 but i do not know how this algorithm work my i have any help with this i have the following algorithm JAVA Code:
Last edited by LuciWiz : 27-Feb-2006 at 14:05.
Reason: Please insert your Java code between [java] & [/java] tags
|
|
#2
|
||||
|
||||
Re: Magic SquareCan you elaborate a little more as to what this "magic square" is? (And describe further the functionality required)
Also, it helps to put the JAVA tags around your code to properly format the code and highlight key words, etc. ([java] is the tag.) __________________
If practice makes perfect and nobody's perfect... why practice? Homepage: http://www.treywhite.com Blog: http://www.treywhite.com/blog.php Web Design Company: http://www.ewebproductions.com |
|
#3
|
|||
|
|||
any help with this code Magic Square Please This code suppose to generate Magic square NxN but i could not run for 3X3 JAVA Code:
Last edited by cable_guy_67 : 02-Mar-2006 at 15:52.
Reason: USE [java] your code here [/java]
|
|
#4
|
||||
|
||||
Re: Magic SquareThere are several parts of this code that stick out to me as faulty or not needed. There are also several parts where I don't really know what you are trying to do. If you are wanting help from this board... the best thing that you could do is to comment your code really well.
Other conventions that help us read and understand the code better besides commenting well are: 1.) Better use of whitespace 2.) Name variables better. The name needs to represent it's function in the code. Particularly avoid names such as N,x1,y,i, and c. Also overly generic names such as solution and flag can often be confusing to someone who isn't familiar with the code. Some things that immediately stuck out with this code: You don't use anything from java.awt.*, so there really isn't any need to import it. Unless you are planning to make this program graphical later: JAVA Code:
This jumbling mess of global variables should be cleaned up. Although Java (and most languages) allow multiple variable initializations on a single line... it isn't really good coding necessarily. Just give each variable a line for itself. Also in your example, all of the loop counters don't need to be global. JAVA Code:
Also, I noticed that most of your arrays... although they will be holding only 9 items... you initialized them to hold 20 items? This has to do with the way you are initializing your index counter in the arrays. You are starting with a counter value of 1. '1' is actually the 2nd element in an array (with 0 being the first). You should start your counter at 0 and initialize your array as holding 9 elements. JAVA Code:
There's actually a lot of areas of this program that should be improved upon, so if you can provide comments for each area... we could take it one section at a time... and try to get this thing working. Oh and please put the [java] tags around your code __________________
If practice makes perfect and nobody's perfect... why practice? Homepage: http://www.treywhite.com Blog: http://www.treywhite.com/blog.php Web Design Company: http://www.ewebproductions.com |
|
#5
|
|||
|
|||
Re: Magic Squarethank so much for your reply i will tell what i should
i should generate magic square of NxN using Brute Force algorithm my number should be generated using Permutation but the problem i got that my Permutation method generate numbers from 1 to 8 when i want to have the matrix of 3 that way i have stick n to 3 explicty the N shluld be any number entered by the user that mean i should initialize the array according to the user input that's why you see single array initialized to 20 because i was testing for 4x4 The second problem that i could not reorder the content of the array to meet the magic square reule that they should equal (N^2+1)*N/2 this checked in the inner loop of the Permutation method the rule of Permutation states that i should start my Permutation with one i am trying to implement simple magic square ; i have taken this algorithm from this website personal.vsnl.com personal.vsnl.com personal.vsnl.com this what i am trying to do the Global variable i am trying to declear because i want to use the same value JAVA Code:
this the Code again but when i remove the Global Varuable i did not get any output you can run it if you have a Jcreator i will be so glad if you can figer what is wrong with my code i have spent 4 days before i subscibe to your forum i have given you the url to kknow what i am trying to do; so do not get upset that i might waste your time ,but may be i understand wrong but i want to implement it using this alogorithm thank you for your attantion this code driving me crazy ![]() Last edited by cable_guy_67 : 03-Mar-2006 at 07:49.
Reason: Please surround java code with [java] your code [/java]
|
|
#6
|
|||
|
|||
Re: Magic Squarehi
i have generate the code for 3x3 but when i tried for 4x4 it did not run here is the working version Try it with 3 it works perfectly but with 4 it did not work JAVA Code:
Last edited by admin : 03-Mar-2006 at 16:35.
Reason: Please insert your JAVA code between [java] & [/java] tags
|
|
#7
|
||||
|
||||
Re: Magic SquareOne of the problems with your program is organization... hopefully you understand objects in Java, because the following code implements your code using objects. I still haven't done the "magic" part of your project, but to be able to fully understand what's going on... you need to be organized first.
This Driver file asks the user for the number of rows, and then creates a Board object for that specific number of rows. It then permutates the board and prints the board: JAVA Code:
This is the Board object class. Notice how much cleaner and extendable it is. You could theoretically create a board of 3x3, 4x4, .... nxn (memory withstanding). There are no numbers that are hard coded, it is all based on the user's input and flexible to what they enter (in my example, I limit the input to between 3-10 for display purposes) JAVA Code:
It's all about organization... once you have the structure down... you can more easily complete the more complex features. Now try to get your program working using this structure if you like. If you need more halp on the permutate part, I need some more details. Correct me if I'm wrong... but you need to figure out how to insert the numbers in the squares such that the sum of the values in each row are equal... and the sum of the values in each column are equal? __________________
If practice makes perfect and nobody's perfect... why practice? Homepage: http://www.treywhite.com Blog: http://www.treywhite.com/blog.php Web Design Company: http://www.ewebproductions.com |
|
#8
|
|||
|
|||
Re: Magic Squarehello Troy
Thank you for your help in this problem . if do not mind i would like to know if you have any experince with multithreading program and coding because i am taking an operating cource and i don not understand how threads behave in term of passing signals ,wait for siganl so if you could help i will be glad for that . also i want to learn how do you learn the indent in coding any Programming language Thanx |
|
#9
|
|||
|
|||
Re: Magic Squarethis method for generate magic for any number(odd number and even number).But I 'm not sure why I can't run this code,please check method of calculate.....
[]constructor []generate() JAVA Code:
-------------------------------- thank you for your attantion this code thanks... Last edited by admin II : 30-Nov-2007 at 05:23.
Reason: Please surround your Java code with [java] your code [/java]
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| OT: nth power square root math question | earachefl | C++ Forum | 1 | 18-Feb-2006 08:45 |
| Convert the input into square yards | pjpav | Java Forum | 1 | 08-Oct-2005 08:43 |
| morrowind - magic menu problem | gino | Computer Software Forum - Games | 0 | 25-Sep-2005 11:19 |
| Need help with my programs, please help. | agentxx04 | C Programming Language | 1 | 23-Sep-2004 19:02 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The