# ERROR CODE VISUAL BASIC 2008 • ### Pregunta

• FIRST OF ALL, SORRY FOR MY ENGLISH THE IDEA OF THIS CODE IS FOR EXMAPLE TO MAKE A MAGIC SQUARE LIKE THIS:

8-1-6

3-5-7

4-9-2

IN A 3x3 SQUARE, USING A MULTIDIMENSIONAL ARRAY, BUT IT DOESN'T WORK :(

WHAT THIS CODE MAKES IS:

THE FIRST NUMBER(1) GOES IN THE LINE 0 COLUMN 1(BECAUSE IN A 3x3 SQUARE IT HAS LINE 0,1 AND 2), THEN  IF THE NEXT NUMBER MOD THE SIZE OF THE SQUARE <>0  GOES IN THE LINE BEFORE AND IN THE AFTER COLUMN. WITH THE EXCEPTION THAT IF THE NUMBER IS THE NEXT OF A NUMBER THAT MOD THE SIZE..... = 0, THE NUMBER GOES IN THE SAME COLUMN BUT IN THE NEXT LINE.

I HOPE YOU UNDERSTAND THIS SO YOU COULD HELP ME, BECAUSE THIS WAY OF MAKING THE MAGIC SQUARE WORKS LIKE IN THE EXAMPLE I GAVE YOU.

Public Class Form1

Public Sub MAGIC_SQUARE(ByRef MAT As Array, ByRef N As Integer)
Dim I, J, NUM As Integer
I = 0
J = N Mod 2
NUM = 1
MAT(0, N \ 2) = NUM
NUM = 2
While NUM <= N * N

If NUM - 1 Mod N <> 0 Then
I = I - 1
J = J + 1
If I < 0 Then
I = N - 1
MAT(I, J) = NUM
NUM = NUM + 1
Else
If J > N - 1 Then
J = 0
MAT(I, J) = NUM
NUM = NUM + 1
Else
MAT(I, J) = NUM
NUM = NUM + 1

End If
End If
End If
If NUM - 1 Mod N = 0 Then
I = I + 1
If I > N - 1 Then
I = 0
MAT(I, J) = NUM
NUM = NUM + 1
Else
MAT(I, J) = NUM
NUM = NUM + 1
End If
End If

End While

For I = 0 To N - 1
J = 0
For J = 0 To N - 1
MsgBox(MAT(I, J))
Next

Next

End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim N As Integer

N = InputBox("INSERT THE DIMENSION OF THE SQUARE, EXAMPLE: 3 FOR A 3x3 SQUARE")
Dim MAT(,) As Integer
ReDim MAT(0 To N - 1, 0 To N - 1)
MAGIC_SQUARE(MAT, N)

End Sub
End Class
sábado, 12 de junio de 2010 3:41