RosettaCodeData/Task/Spiral-matrix/FreeBASIC/spiral-matrix.basic
2023-07-01 13:44:08 -04:00

76 lines
1.3 KiB
Text

' FB 1.05.0 Win64
Enum Direction
across
down
back
up
End Enum
Dim As Integer n
Do
Input "Enter size of matrix "; n
Loop Until n > 0
Dim spiral(1 To n, 1 To n) As Integer '' all zero by default
' enter the numbers 0 to (n^2 - 1) spirally in the matrix
Dim As Integer row = 1, col = 1, lowRow = 1, highRow = n, lowCol = 1, highCol = n
Dim d As Direction = across
For i As Integer = 0 To (n * n - 1)
spiral(row, col) = i
Select Case d
Case across
col += 1
If col > highCol Then
col = highCol
row += 1
d = down
End if
Case down
row += 1
If row > highRow Then
row = highRow
col -= 1
d = back
End if
Case back
col -= 1
If col < lowCol Then
col = lowCol
row -= 1
d = up
lowRow += 1
End If
Case up
row -= 1
If row < lowRow Then
row = lowRow
col += 1
d = across
highRow -= 1
lowCol += 1
highCol -= 1
End If
End Select
Next
' print spiral matrix if n < 20
Print
If n < 20 Then
For i As Integer = 1 To n
For j As Integer = 1 To n
Print Using "####"; spiral(i, j);
Next j
Print
Next i
Else
Print "Matrix is too big to display on 80 column console"
End If
Print
Print "Press any key to quit"
Sleep