# Spiral traversal of matrix in vb.net

Vb program for Spiral traversal of matrix. Here problem description and explanation.

``````' Include namespace system
Imports System
'  Vb.net program for
'  Spiral form of matrix
public Class SpiralView
Public Sub spiral(ByVal data As Integer(,),
ByVal startRow As Integer,
ByVal startCol As Integer,
ByVal endRow As Integer,
ByVal endCol As Integer,
ByVal element As Integer)
'  Left to right
Dim i As Integer = startCol
While i <= endCol AndAlso
element > 0

element -= 1
Console.Write("  " + data(startRow,i).ToString())
i += 1
End While
'  Top to down
i = startRow + 1
While i <= endRow AndAlso
element > 0

element -= 1
Console.Write("  " + data(i,endCol).ToString())
i += 1
End While
'  Bottom right to bottom-left
i  = endCol - 1
While i >= startCol AndAlso
element > 0

element -= 1
Console.Write("  " + data(endRow,i).ToString())
i -= 1
End While
'  Bottom left to top
i = endRow - 1
While i > startRow AndAlso
element > 0

element -= 1
Console.Write("  " + data(i,startRow).ToString())
i -= 1
End While
if (startRow + 1 <= endRow - 1 AndAlso
element > 0) Then
'  Recursive call
Me.spiral(data, startRow + 1,
startCol + 1,
endRow - 1,
endCol - 1,
element)
End If

End Sub

Public Shared Sub Main(ByVal args As String())
Dim task As SpiralView = New SpiralView()
'  Given matrix
Dim matrix As Integer(,) =
{
{1, 2, 3, 4, 5, 6},
{22, 23, 24, 25, 26, 7},
{21, 36, 37, 38, 27, 8},
{20, 35, 42, 39, 28, 9},
{19, 34, 41, 40, 29, 10},
{18, 33, 32, 31, 30, 11},
{17, 16, 15, 14, 13, 12}}
Dim row As Integer = matrix.GetLength(0)
Dim col As Integer = matrix.GetLength(1)
'  Get the number of element
Dim element As Integer = row * col
task.spiral(matrix, 0, 0, row - 1, col - 1, element)

End Sub

End Class

``````

Output

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

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.