Question: A simple magic square (Links to an external site.)Links to an external site. is numeric a square, such that the numbers in all rows and

A simple magic square (Links to an external site.)Links to an external site. is numeric a square, such that the numbers in all rows and all columns sum to the same constant. Each entry z in our magic square is calculated using the following formula Z = ((x - (2 * (-y))) Mod Size) where x and y values are based on the size of the square. Magic squares created should be odd numbered and greater than zero.

Often when writing commercial code, the customer brings in an application with a complaint or concern that they wish you to address. Many times, it is up to us to examine the application and come up with a proposal to satisfy the customer. They may not be computer scientists, so the hire us to find solutions to sometimes FUZZY complaints. Lets look at the following.

The following code is an attempt to create a user friendly program that examines the user input Size and attempts to prevent processing erroneous user input while not crashing the application in the process. When running the program, the customer finds the results confusing when they mistakenly enter characters instead of numbers. Also, the user wants to be allowed to create as many squares as he or she wants. See what you can do to improve this example using what we have learned throughout the course. We might consider a user friendly way to inform the user of invalid entries. The program output should be neat and well blocked. Comment your code changes. Use your imagination to create your custom solution. Use Visual Basic.

HINT: It was notices that when the word "Cat" for example is entered, the number converts to a zero. The president of the company noticed when the value "3blind mice" was entered, a square of size three was created.

Module Module1

Sub Main()

Dim x As Integer

Dim y As Integer

Dim Z As Integer

Dim strSize As String

Dim Size As Integer

Console.BackgroundColor = ConsoleColor.White

Console.ForegroundColor = ConsoleColor.DarkBlue

Console.Clear()

Console.SetCursorPosition(25, 2)

Console.WriteLine("CS_6 Magic Square Spactacular!")

Console.SetCursorPosition(21, 4)

Console.Write("Number of Chars Wide (Use Odd Nums): ")

'Read as a string to prevent program crash when entering letters

strSize = Console.ReadLine()

Size = Val(strSize) 'Convert string to numberic value

Size = Math.Abs(Size) 'Prevent negative values

If (Size Mod 2) = 0 Then 'Force Size to be odd

Size = Size + 1 'Add 1 to even numbers

End If

'Calculate the square and write to screen

For x = 0 To Size - 1

Console.SetCursorPosition(35, x + 6)

For y = 0 To Size - 1

Z = ((x - (2 * (-y))) Mod Size)

Console.Write(Z & " ")

Next y

Console.WriteLine()

Next x

Console.ReadKey()

End Sub

End Module

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!