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
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
Here is an example of the "00" format:
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:
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
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 = ""