CIS 220 - Program 8
Magic Square Clues

You may need to use dynamic arrays.  We run into a situation where you do not know in advance how large your array needs to be, since the size is determined by the "n" which is entered by the user.  You can either declare the array to be 9 x 9 (the maximum size that the specs require) and only use part of the array, or you can use dynamic arrays.  If you use dynamic arrays, you first declare the array without specifying a size, such as 

        Dim magicSquare  ( , ) as Integer

and then after you read n from the textbox you can set the size by redimensioning the array with a statement like

        ReDim dynamicArray(n,n)

Take your pick. 

Whether you decide to use a static array or a dynamic array, you can clear the array contents with the Erase command, such as

        Erase magicSquare

Here is an example of the "00" format:

output &= Format(MagicSquare(rowCtr, colCtr), "00")

Please note that step 2 is repeated until a multiple of n numbers are in place. So if n is 3, then step 2 repeats until 1, 2, and 3 have been placed, and then step 3 is executed.

For those of you having problems with the magic square, break the problem into parts: 

  • First, be sure that you know how to process a two-dimensional array. Look at the examples in the notes, and remember that since you have two indexes you must use a loop in a loop (with the exception of the diagonal). Practice with some made-up arrays until you know what you are doing and how it works.
  • Second, figure out the magic square algorithm. Make sure that you can do the 3x3 example by hand. In addition to the loop in a loop, I also used variables for row and col since you have to move around the array somewhat randomly. The wrap-around shouldn't be too hard--when you are in row 0 and need to move up, then reset the row to n-1, etc.
  • Third, once you have the array populated correctly, display it in a multi-line text box. 
  • When you are done with that (whew!) you still aren't finished. You need to go back and write yet another routine to check the magic square. Total all the rows, columns, and the diagonal (I used separate loops for each test) and if they are all the same then return the value so it can be displayed on the form.

Notice that the magic square assignment tells you to check the magic square by calculating the sum of EVERY row, column and diagonal. Don't overlook this. I'd make it a separate method. It's not particularly hard, but it's not easy either. Sum each individual row, each individual column, and each diagonal and then compare the answers. If they are identical, then return the sum (pick any of them, since they're all the same) to be displayed on the form. Note the word EVERY!

This is a relatively easy problem and you need to learn to improvise and figure out unfamiliar problems on your own. That's an integral part of your training for future projects, both in the classroom and on the job.

Here is the code that I used to resize my text box.  The parameter numElements is the dimension of the magic square.  If you use this, it may require some adjustments based on the font that you use.

' Resize the text box depending on the size of the magic square. The
' multiplier was determined through trial and error. Also clears the text
' box.
Private Sub setScreen(ByVal numElements As Integer)
     txtOut.Height = numElements * 22
     txtOut.Width = numElements * 30
     txtOut.Left =
CInt((Me.Width - txtOut.Width) / 2)
     txtOut.Top =
CInt(94 + (198 - txtOut.Height) / 2)
     txtOut.Text = ""