Advertisement
Guest User

Untitled

a guest
Feb 10th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Base 1
  2. Sub spiral()
  3.  
  4.  
  5.     Const rows As Integer = 4
  6.     Const cols As Integer = 5
  7.      
  8.     Dim matrix(rows, cols) As Integer
  9.    
  10.     For i = 1 To rows
  11.         For j = 1 To cols
  12.             matrix(i, j) = InputBox("Vvedite element (" & i & ", " & j & ")")
  13.             Next j
  14.         Next i
  15.        
  16.    
  17.     Dim top, bottom, left, right, index, direction As Integer
  18.     top = 1
  19.     bottom = rows
  20.     left = 1
  21.     right = cols
  22.     index = 1
  23.     direction = 0
  24.    
  25.     Do While (top <= bottom) And (left <= right)
  26.         Select Case (direction Mod 4)
  27.             Case 0
  28.                 For col = left To right
  29.                     matrix(top, col) = index
  30.                     index = index + 1
  31.                 Next col
  32.                 top = top + 1
  33.                 direction = direction + 1
  34.             Case 1
  35.                 For Row = top To bottom
  36.                     matrix(Row, right) = index
  37.                     index = index + 1
  38.                 Next Row
  39.                 right = right - 1
  40.                 direction = direction + 1
  41.             Case 2
  42.                 For col = right To left Step -1
  43.                     matrix(bottom, col) = index
  44.                     index = index + 1
  45.                 Next col
  46.                 bottom = bottom - 1
  47.                 direction = direction + 1
  48.             Case 3
  49.                 For Row = bottom To top Step -1
  50.                     matrix(Row, left) = index
  51.                     index = index + 1
  52.                 Next Row
  53.                 left = left + 1
  54.                 direction = direction + 1
  55.         End Select
  56.     Loop
  57.    
  58.     s = ""
  59.     For i = 1 To rows
  60.         For j = 1 To cols
  61.             s = s & matrix(i, j) & Chr(9)
  62.         Next j
  63.         s = s & Chr(10)
  64.     Next i
  65.     MsgBox (s)
  66.  
  67. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement