Results 1 to 5 of 5

Thread: Magic square in VB.NET

  1. #1

    Thread Starter
    Junior Member hunter2's Avatar
    Join Date
    Jun 2003
    Posts
    19

    Magic square in VB.NET

    I did this project, but it wont work..any thing wrong in my code..could any one check for me..urgent Plz..

    my project:
    1 text box
    1 listview
    1 button

    Private Sub btnCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCmd.Click
    Dim Size, I, J, II, JJ, count As Short

    Size = txtSize.Text
    Dim Magic(Size, Size) As Short

    I = 0
    J = Size / 2
    Magic(I, J) = 1
    For count = 1 To (Size ^ 2)
    II = I
    JJ = J
    I = I - 1
    If (I < 0) Then
    I = Size - 1
    End If
    J = J - 1
    If (J < 0) Then
    J = Size - 1
    End If
    If (Magic(I, J) > 0) Then
    I = (II + 1)
    If (I > (Size - 1)) Then
    I = 0
    End If
    End If
    count += 1
    Magic(I, J) = count
    Next

    lvwDisplay.View = View.Details
    Dim col As New ListView.ColumnHeaderCollection(lvwDisplay)
    col.Clear()
    J = 0
    For I = 0 To (Size - 1)
    col.Add("", 20, HorizontalAlignment.Right)
    Next
    Dim Row As ListViewItem
    For I = 0 To (Size - 1)
    Row = lvwDisplay.Items.Add(Magic(I, J))
    For J = 1 To (Size - 1)
    Row.SubItems.Add(Magic(I, J))
    Next
    Next




    End Sub
    Plz..help.

  2. #2

    Thread Starter
    Junior Member hunter2's Avatar
    Join Date
    Jun 2003
    Posts
    19
    no one help...

  3. #3
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    http://mathforum.org/alejandre/magic...er/adler4.html

    I may be wrong but it looks to me like ur formual needs a little tuning up
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Also, next time, could you please use [Highlight=VB] tags?

  5. #5
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    well i got bored, i pulled a all nighter, and read the explaination on magic squares. Then i played started trying to make vb do it and after about 30minutes of playing around heres what i got...

    VB Code:
    1. Private Sub btnCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCmd.Click
    2.         CreateMagicSquare(txtSize.Text, lvwDisplay)
    3.     End Sub
    4.  
    5.     Private Function CreateMagicSquare(ByVal sSize As Short, ByRef lvwList As ListView)
    6.  
    7.  
    8.         If IsEven(sSize) = True Then
    9.             MsgBox("Error: This Function Does NOT Support Even Numbers!!!")
    10.             Exit Function
    11.         End If
    12.  
    13.         Dim asMagicSquare(sSize, sSize) As Short ' Create The Array to hold the square
    14.  
    15.         Dim sCount As Short ' Hold counting place
    16.         Dim sCurX As Short ' Holds The Vertical Position in array(magic Square)
    17.         Dim sCurY As Short ' Holds the Horizontal Position in array
    18.         Dim sOldX As Short ' Holds old sCurX (Needed because of overwrite)
    19.         Dim sOldY As Short ' Holds old sCurY (Needed because of overwrite)
    20.  
    21.         'Must Start In The MIDDLE of the First row so:
    22.         sCurX = 1 ' This is give does not change
    23.         sCurY = ((sSize - 1) / 2) + 1 ' Find Middle
    24.         'Write Frist Number
    25.         asMagicSquare(sCurX - 1, sCurY - 1) = 1 ' Always 1
    26.  
    27.         For sCount = 2 To (sSize * sSize)
    28.  
    29.             sOldX = sCurX
    30.             sOldY = sCurY
    31.  
    32.             If sCurX = 1 Then
    33.                 'We are In The Top Row and cant Move Up anymore
    34.                 sCurX = sSize 'move to bottom row
    35.             Else
    36.                 sCurX = sCurX - 1
    37.             End If
    38.  
    39.             If sCurY < sSize Then
    40.                 sCurY = sCurY + 1
    41.             Else
    42.                 sCurY = 1
    43.             End If
    44.  
    45.             'Check If we are going to overwrite
    46.             If asMagicSquare(sCurX - 1, sCurY - 1) <> Nothing Then
    47.                 'We are going to overwrite
    48.                 'so we must go below the last number we wrote
    49.                 sCurX = sOldX + 1
    50.                 sCurY = sOldY
    51.                 If asMagicSquare(sCurX - 1, sCurY - 1) <> Nothing Then
    52.                     MsgBox("Error Overwrite!! AGAIN!")
    53.                 End If
    54.             End If
    55.  
    56.             'Write sCount to position
    57.             asMagicSquare(sCurX - 1, sCurY - 1) = sCount
    58.  
    59.         Next
    60.  
    61.         DisplayArray(asMagicSquare, lvwList)
    62.  
    63.     End Function
    64.  
    65.     Private Function IsEven(ByVal lngNumber As Long) As Boolean
    66.         If lngNumber = 0 Then
    67.             Return False
    68.         End If
    69.         If (lngNumber Mod 2) = 0 Then
    70.             Return True
    71.         End If
    72.     End Function
    73.  
    74.     Private Function DisplayArray(ByVal asArray(,) As Short, ByRef lvwList As ListView)
    75.         Dim x As Short
    76.         Dim y As Short
    77.  
    78.         'Setup listview
    79.  
    80.         lvwList.Clear() ' Just Incase there is data in the list already
    81.  
    82.         lvwList.View = View.Details
    83.         Dim col As New ListView.ColumnHeaderCollection(lvwList)
    84.  
    85.         col.Clear()
    86.  
    87.         For x = 0 To (UBound(asArray) - 1)
    88.             'Add a Colum to the listview
    89.             col.Add("", 40, HorizontalAlignment.Center)
    90.  
    91.             lvwList.Items.Add(asArray(x, 0))
    92.             For y = 1 To (UBound(asArray) - 1)
    93.                 lvwList.Items(x).SubItems.Add(asArray(x, y))
    94.             Next
    95.         Next
    96.     End Function
    97. End Class

    This code sample does not work on even numbers and has not been tested extensivly but i tried a few and all of them checked out.

    If you find a method for finding even magic squares and you need help. i will try to help you.

    btw: if my coding isnt perfect its prolly because i am a vb 6 programmer that just started playing with VB .NET
    Last edited by <ABX; Aug 2nd, 2003 at 04:16 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width