VB.NET Program to print Matrix in a Snail Shell Way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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 :