How to resolve the algorithm Spiral matrix step by step in the ooRexx programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Spiral matrix step by step in the ooRexx programming language
Table of Contents
Problem Statement
Produce a spiral array.
A spiral array is a square arrangement of the first N2 natural numbers, where the numbers increase sequentially as you go around the edges of the array spiraling inwards.
For example, given 5, produce this array:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Spiral matrix step by step in the ooRexx programming language
Source code in the oorexx programming language
call printArray generateArray(3)
say
call printArray generateArray(4)
say
call printArray generateArray(5)
::routine generateArray
use arg dimension
-- the output array
array = .array~new(dimension, dimension)
-- get the number of squares, including the center one if
-- the dimension is odd
squares = dimension % 2 + dimension // 2
-- length of a side for the current square
sidelength = dimension
current = 0
loop i = 1 to squares
-- do each side of the current square
-- top side
loop j = 0 to sidelength - 1
array[i, i + j] = current
current += 1
end
-- down the right side
loop j = 1 to sidelength - 1
array[i + j, dimension - i + 1] = current
current += 1
end
-- across the bottom
loop j = sidelength - 2 to 0 by -1
array[dimension - i + 1, i + j] = current
current += 1
end
-- and up the left side
loop j = sidelength - 2 to 1 by -1
array[i + j, i] = current
current += 1
end
-- reduce the length of the side by two rows
sidelength -= 2
end
return array
::routine printArray
use arg array
dimension = array~dimension(1)
loop i = 1 to dimension
line = "|"
loop j = 1 to dimension
line = line array[i, j]~right(2)
end
line = line "|"
say line
end
You may also check:How to resolve the algorithm Consecutive primes with ascending or descending differences step by step in the Python programming language
You may also check:How to resolve the algorithm Bernoulli numbers step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the PureBasic programming language
You may also check:How to resolve the algorithm XML/Output step by step in the MATLAB programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Aikido programming language