VB icon

a - Odd Magic Square

Email
Submitted on: 1/16/2015 5:06:00 PM
By: K. O. Thaha Hussain (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: VB 5.0, VB 6.0
Views: 3404
 
     Magic square is a set of numbers arranged in a particular order whose column-wise, row-wise and diagonal-wise sums are all the same. This program explains the mathematics behind an odd magic square. Magic, Fun, Maths, Interesting, Easy even for beginners and excellent!!

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: a- Odd Magic Square
' Description:Magic square is a set of numbers arranged in a particular order whose column-wise, row-wise and diagonal-wise sums are all the same. This program explains the mathematics behind an odd magic square.
Magic, Fun, Maths, Interesting, Easy even for beginners and excellent!!
' By: K. O. Thaha Hussain (from psc cd)
'
' Inputs:Place an MS Flex Grid Control. Name it Grid.
'**************************************

'(C) K. O. Thaha Hussain. All rights reserved
'Analyst Programmer
'Company: http://www.induswareonline.com
'URL: http://www.bcity.com/thahahussain
'Note: Adjust the DataTypes to make room for
'large numbers..
'
'The Behind Scene Mathematics is simple!
'Step1. Begin 1 at the middle of the first row
'Step2. Next number should be one row up
' one column right
'Step3. If the present row < the first then
'make it last
'Step4. If the present column > the last then
'make it first
'Step5. The rule for the number which follows
' the multiple of the
'order of magic square, is one row down
'Finished!!
Option Explicit
Dim N As Integer
Private Sub Form_Load()
Do While N Mod 2 = 0
 N = Val(InputBox("Enter an Odd Number (Ex: 3, 5, 7 etc.)", _
 "Order of Magic Square", 5))
Loop
 Grid.BackColor = 
 Grid.FixedCols = 0
 Grid.FixedRows = 0
 Grid.Left = 0
 Grid.Top = 0
 Grid.Rows = N
 Grid.Cols = N
 Me.Caption = "Odd Magic Sqaure By K.O. Thaha Hussain " _
 & " Order : " & Str(N)
 Call MagicSquare
 
End Sub
Private Sub Form_Resize()
 Grid.Width = Me.ScaleWidth
 Grid.Height = Me.ScaleHeight
End Sub
Private Sub MagicSquare()
Dim Row As Integer, Column As Integer, I As Integer, Number As Integer
 Dim Magic(100, 100) As Integer
 Number = 1
 Row = 0
 Column = (N + 1) / 2 - 1
 Magic(Row, Column) = Number
 
 For I = 2 To N * N
 If Number Mod N <> 0 Then
Row = Row - 1
Column = Column + 1
 Else
Row = Row + 1
 End If
 If Row < 0 Then Row = N - 1
 If Column > N - 1 Then Column = 0
 Number = Number + 1
 Magic(Row, Column) = Number
 Next I
'Loops to put the values into grid
For Row = 0 To N - 1
 For Column = 0 To N - 1
Grid.Row = Row
Grid.Col = Column
Grid.Text = Format(Magic(Row, Column), "#####")
 Next Column
Next Row
End Sub


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.