A
A
Alertoso2021-02-20 16:15:38
Delphi
Alertoso, 2021-02-20 16:15:38

How to make dynamic array in given code?

const
SizeArray = 11;
type
MyArrayType = array [1..SizeArray, 1..SizeArray] of string[1];
DirectionType = (dRight, dDown, dLeft, dUp);

procedure Spiral(var M : MyArrayType; SpirWrite : boolean; var S : string);
var
CurStep, Step, PosX, PosY : integer;
Direction : DirectionType; {1 to 4 - 1 right, 2 down, 3 left, 4 up}
i : integer; {banal counter by line}
begin
i := 1;
PosX := 6; {round(SizeArray/2)}
PosY := 6;
CurStep := 1;
step := 1; {after how many steps to turn}
Direction := dRight; {in fact, this is where to twist the spiral in the first step}
if Not SpirWrite {means reading from array} then S := '';
while (PosX>0) and (PosX<=SizeArray) and (PosY>0) and (PosY<=SizeArray) do
begin
if SpirWrite then M[PosX, PosY] := S[i]
else S := S + M [PosX,PosY];

{here, in fact, the movement along the matrix in a spiral!}
case Direction of
dRight: begin PosY := PosY + 1;
if CurStep = Step then
begin
CurStep :=1; Direction := dDown;
end
else
CurStep := CurStep + 1;
end;
dDown : begin PosX := PosX + 1;
if CurStep = Step then
begin
CurStep :=1; Direction := dLeft;
Step := Step + 1;
end
else
CurStep := CurStep + 1;
end;
dLeft : begin PosY := PosY - 1;
if CurStep = Step then
begin
CurStep :=1; Direction := dUp;
end
else
CurStep := CurStep + 1;
end;
dUp : begin PosX := PosX - 1;
if CurStep = Step then
begin
CurStep :=1; Direction := dRight;
Step := Step + 1;
end
else
CurStep := CurStep + 1;
end;
end;

inc(i);
end;


end;

procedure DoCheck_and_Fix_String( var S : string; NeedLength : integer);
begin
if (NeedLength<1) or (NeedLength>255) then Exit;
if Length(S) > NeedLength then
S := Copy(S,1, NeedLength)
else
while length(S) end;

var
Matrix : MyArrayType;
i, j : integer;
myS : string;
f : text;

begin
myS := '1234567890 This is just my favorite tesing string - you need very long string!';

i:= length( myS );
WriteLn('lenS =',i);

DoCheck_and_Fix_String( myS, 121);

i:= length( myS );
WriteLn('len (fixed)=',i);

Spiral(Matrix, true, myS );

{output the matrix with the text placed there into a text file}
Assign(f, 'SpirOut.txt');
rewrite(f);
for i:=1 to 11 do
begin
for j:=1 to 11 do
Write(f, Matrix[i,j]);
WriteLn(f);
end;
close(f);
end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hemul GM, 2021-02-20
@Alertoso

type MyArrayType = array [1..SizeArray, 1..SizeArray] of string[1];

.. And why string[1] - if it's just Char
type MyArrayType = array of array of Char;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question