This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

7 Sep 2013

VB.NET Program to print Matrix in a Snail Shell Way

Module Module1

    Sub Main()

        Dim size As Integer
        Dim a As Integer = 0
        Dim i As Integer, j As Integer, k As Integer, count As Integer = 0, flag As Integer = 0

        Do
            Try
                Console.WriteLine("www.ProgrammingPosts.blogspot.com")
                Console.WriteLine(vbLf & ">>>VB.NET PROGRAM To PRINT A MULTI-DIMENSIONAL ARRAY IN A SNAIL SHELL WAY <<<")
                Console.Write(vbLf & " Enter the Size of a square Matrix(between 2 to 10):")
                size = Convert.ToInt16(Console.ReadLine())
                If size < 2 OrElse size > 10 Then
                    'limiting the size of matrix
                    Console.BackgroundColor = ConsoleColor.DarkRed
                    'changing background color to red
                    Console.WriteLine("The Size Of Matrix should Be in between  2 and 10.")
                    'System.Console.ResetColor(); 'resetting color
                    Console.ReadKey()
                    Return
                Else



                    flag = 1
                    Dim matrix As Integer(,) = New Integer(size - 1, size - 1) {}

                    k = 1
                    While k < size
                        i = a
                        j = a
                        While j < size - k
                            'lef
                            matrix(i, j) = System.Threading.Interlocked.Increment(count) ' this is equal to count++
                            j += 1
                        End While
                        i = a
                        j = size - k
                        While i < size - k
                            matrix(i, j) = System.Threading.Interlocked.Increment(count)
                            i += 1
                        End While
                        i = size - k
                        j = size - k
                        While j >= a
                            matrix(i, j) = System.Threading.Interlocked.Increment(count)
                            j -= 1
                        End While
                        i = size - (k + 1)
                        j = a
                        While i >= k
                            matrix(i, j) = System.Threading.Interlocked.Increment(count)
                            i -= 1
                        End While
                        k += 1
                        a += 1
                    End While


                    Console.Write(vbLf & vbLf & vbTab)
                    For i = 0 To size - 1

                        For j = 0 To size - 1
                            If matrix(i, j) < 10 Then
                                Console.Write("  0" & Convert.ToString(matrix(i, j)))
                            Else
                                Console.Write("  " & Convert.ToString(matrix(i, j)))
                            End If

                            If j = size - 1 Then
                                Console.Write(vbLf & vbTab)

                            End If
                        Next
                    Next
                End If
            Catch
                'to catch exceptions,suppose string entered as a size of matrix
                Console.BackgroundColor = ConsoleColor.DarkRed
                Console.WriteLine("WARNING:only Number between (2 and 10) is allowed")
                Console.ResetColor()
            End Try


            Console.WriteLine(vbLf & vbLf & vbTab & " Press any key to exit....")
        Loop While flag = 0

        Console.ReadKey()

    End Sub

End Module
.............
Sample Output :