I started off creating a form with a 4x4 array as textbox inputs which can add numbers in the rows and columns and show the sums of them. I want this to read arrays off of a text file which indicates the size of the array, then read the actual array of numbers, output it and state whether its magic or not. Magic square means the sums of the rows and columns add up to one single number. It should then go back to the input file and get the next array until all the arrays are processed.
I attached the text file and the form I created so far. Also, below is code given to me by cictrix suggesting how to implement the calculation for the square arrays.
Code:
1.Private Function IsMagic(ByVal arr As Integer(,)) As Boolean
2.
' We received some 2D array into the arr argument.
3.
' Let's first check if it's a square.
4.
' We need a square so the number of rows must be equal to the number of columns.
5.
' To get the upper bound of the array we use .GetUpperBound method and specify
6.
' the dimension in the parentheses (0 for rows and 1 for columns):
7.
If arr.GetUpperBound(0) <> arr.GetUpperBound(1) Then
8.
' We inform the caller of our function that the passed array is not a square:
9.
Throw New ArgumentException("The array is not a square")
10.
Return False
11.
End If
12.
13.
Dim row, col As Integer
14.
Dim testsum As Integer = 0 ' We'll store the sum on the first row here
15.
' Each subsequent sum will be compared to the previous one.
16.
Dim tempRsum As Integer = 0 ' Sums of rows
17.
Dim tempCsum As Integer = 0 ' Sums of columns
18.
Dim tempD1sum As Integer = 0 ' Sum of 1st diagonal
19.
Dim tempD2sum As Integer = 0 ' Sum of 2nd diagonal
20.
21.
For row = 0 To Arr.GetUpperBound(0) ' 0 is for rows
22.
For col = 0 To Arr.GetUpperBound(1) ' 1 is for columns
23.
tempRsum += arr(row, col)
24.
' Now, to save the processor time, we'll swap rows and cols to evaluate
25.
' columns sums as well:
26.
tempCsum += arr(col, row)
27.
Next
28.
' And diagonals (but we need to do it only once):
29.
tempD1sum += arr(row, row)
30.
tempD2sum += arr(row, arr.GetUpperBound(1) - row)
31.
32.
If testsum = 0 Then ' We do it for the first time
33.
testsum = tempRsum ' The first sum is calculated and saved
34.
Else
35.
' If at least one sum is not equal to the first one
36.
' Then it's not a magic square so we return false:
37.
If testsum <> tempRsum OrElse
38.
testsum <> tempCsum Then Return False
39.
End If
40.
' This row/column is finished and
41.
' we're ready to proceed with the next ones
42.
tempRsum = 0
43.
tempCsum = 0
44.
Next
45.
' Checking diagonals
46.
If testsum <> tempD1sum OrElse
47.
testsum <> tempD2sum Then Return False
48.
49.
' Everything's OK we have our magic square:
50.
Return True
51.
End Function
Last edited by si_the_geek; Nov 23rd, 2010 at 04:11 AM.
Reason: removed compiled files
You say that you keep getting bugs but you make no mention of what and where. Also, please post your code snippets using the Code or VBCode buttons to provide formatting to make it readable.
Also, from the size of your ZIP file I can see that you have included the binary files, which is against forum rules. Always delete the 'bin' and 'obj' folders before zipping up a project.
Thanks for telling me. I didn't know that. I posted it again. I can't get the code feature posted perfect but I tried. Sorry. As far as the bugs go, I deleted what I was trying to debug and I am starting from scratch as I type. Any help from anyone is appreciated. I will post more things here as I continue to work on this. Thanks.