How to resolve the algorithm Spiral matrix step by step in the ALGOL 68 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Spiral matrix step by step in the ALGOL 68 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 ALGOL 68 programming language
Source code in the algol programming language
INT empty=0;
PROC spiral = (INT n)[,]INT: (
INT dx:=1, dy:=0; # Starting increments #
INT x:=0, y:=0; # Starting location #
[0:n-1,0:n-1]INT my array;
FOR y FROM LWB my array TO UPB my array DO
FOR x FROM LWB my array TO UPB my array DO
my array[x,y]:=empty
OD
OD;
FOR i TO n**2 DO
my array[x,y] := i;
INT nx:=x+dx, ny:=y+dy;
IF ( 0<=nx AND nx
x:=nx; y:=ny
ELSE
INT swap:=dx; dx:=-dy; dy:=swap;
x+:=dx; y+:=dy
FI
OD;
my array
);
PROC print spiral = ([,]INT my array)VOID:(
FOR y FROM LWB my array TO UPB my array DO
FOR x FROM LWB my array TO UPB my array DO
print(whole(my array[x,y],-3))
OD;
print(new line)
OD
);
print spiral(spiral(5))
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the Phix programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the NOWUT programming language
You may also check:How to resolve the algorithm Find the missing permutation step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Determine if two triangles overlap step by step in the Perl programming language
You may also check:How to resolve the algorithm Wireworld step by step in the Mathematica/Wolfram Language programming language