Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A simple magic square (Links to an external site.)Links to an external site. is

ID: 3913229 • Letter: A

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 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

Explanation / Answer

**************************************************

Modified Source Code:

Module Module1

                Sub Main()

                Do

                                Dim intInput As Integer = 0

                               

                                'Main Menu: Allow users to continuosly create squares until they want

                                Console.WriteLine("")

                                Console.WriteLine("Main Menu")

                                Console.WriteLine("==========================")

                                Console.WriteLine("1. Create magic square")

                                Console.WriteLine("2. Exit" & vbNewLine)

                                Console.Write("Enter your choice: ")

                                'Check user input: either integer or anything else

                                If Integer.TryParse(Console.ReadLine(), intInput) Then

                                                Select Case intInput

                                                                Case 1 'Create square

                                                                                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()

                                                                                'User input must be an integer

                                                                                If Not IsNumeric(strSize) Then

                                                                                                Console.WriteLine("Please enter only number instead of characters/words")

                                                                                Else

                                                                                                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

                                                                                                Console.WriteLine("")

                                                                                                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 If

                                                                Case 2 'Exit the program

                                                                                Exit Sub

                                                                Case Else 'If user enter anything else suggested from main menu

                                                                                Console.WriteLine("Please select from 1 and 2")

                                                End Select

                                Else

                                                Console.WriteLine("Please select from 1 and 2")

                                End If

                Loop

                End Sub

End Module

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote