Last Updated:

Dynamic arrays in Pascal

Dynamic arrays in Pascal

A static array in Pascal is specified as:

var<name of an array>: array [<first index>.. <last index>] of<type of array elements>;

The size of a static array is unchanged, is specified when it is described, and is equal to B-A+1, where B is the last index and A is the first index of the array. In a dynamic array, we do not set the size at the beginning, and we can change it as the program runs.

The advantage of a dynamic array over a static array is that we can thus save computer memory and fill it as needed.

Creating a Dynamic Array

 

An array that does not have the specified dimensions we can define as follows:

var<name of an array>: arrayof<type of array elements>;

To set the size of a dynamic array, use the Setlength function. It has two parameters. The first is the array itself, and the second is an integer that determines the size of the array.
Before use, the Setlength mass has a length of 0. The Setlength function can be used several times.You can check the length of the array using the length function.

An example of setting the length of an array:

Input:var a: array of integer;
begin
writeln(‘Length1: ‘, length(a));
Setlength(a, 2);
writeln(‘Length2: ‘, length(a));
Setlength(a, 4);
writeln(‘Length3: ‘, length(a));
end.
Conclusion:Length1:0
Length2:2
Length3:4

Clean up a dynamic array

To clear the data in a dynamic array, use the Setlength function with the second parameter of 0.

An example of clearing an array.

Input:var a: array of integer;
begin
Setlength(a, 4);
writeln(‘Length1: ‘, length(a));
Setlength(a, 0);
writeln(‘Length2: ‘, length(a));
end.
Conclusion:Length1:4
Length2:0

Multidimensional Dynamic Arrays

In addition to one-dimensional dynamic arrays in the Pascal language, there are also multidimensional ones. Dynamic arrays having a dimension greater than 1 are defined as follows:

var<the name of an array>: arrayof< element type>ofarray<type of elements>of ... (use the word array as many times as the dimension of the array)

An example of creating a two-dimensional array with a size of 5 * 3:

varc: arrayofarrayofinteger;
i: integer;
begin
SetLength(c,5);
fori := 0 to 2 do
SetLength(c[i],3);
writeln(length(c));
end.

Examples of working with dynamic arrays

Array input

From the keyboard, a dynamic array is entered in almost the same way as a static array. The first index of such an array is always 0. Therefore, the initial value of the iteration counter in the loop for input should be zero.

Example of array input:

var c: array of integer;
n, i: integer;
begin
read(n); enter the length from the keyboard
SetLength(c,n); set the length of the
array fori := 0 to n-1 do
read(c[i]); enter n elements of the
end array.

Array output

The array is displayed on the screen like a static one, but we must not forget that its first index is zero:

var c: array of integer;
i: integer;
begin
SetLength(c, 5); set the length
of fori := 0 to 4 do
c[i] := 1; fill the array with
fori units := 0 to 4 do
write(c[i], ' '); display elements through the end space
.

Array operations

Insert an item

In order to insert an element into a dynamic array, you need to act according to the following algorithm:

  1. Change the length of the array. We make it 1 element larger than the initial one.
  2. Set the value and position of the item to add.
  3. All elements up to the specified position are shifted one back.
  4. Assign the value of the element to the specified position.

Code that implements the algorithm:

const n = 5;
var c: array of integer;
i, num, pos: integer;
begin
SetLength(c, n);
fori := 0 to n-1 do
c[i] := 1; fill the array with
SetLength(c, n+1) units; increasing the length of the array by 1
num := 2; the value of the pos element
to be inserted := 3; position for inserting
fori := n — 1 downto 0 do
c[i+1] := c[i];// shifting elements
c[pos-1] := num;// directly inserting a new element into the end array
.

Delete an item

To remove an element from a dynamic array, follow the following algorithm:

  1. Select the position of the item to delete.
  2. All elements after the specified position are shifted one back.
  3. Change the length of the array. We make it 1 element less than the initial one.

An example of deleting an item:

Input:const n = 6;
var c: array of integer;
i, num, pos: integer;
begin
SetLength(c, n);
fori := 0 ton-1 do
c[i] := 1; fill the array with
units c[pos-1] := 2; for clarity, replace the element at the required position with
two pos := 3; the position of the element to delete
write ('First array: ');
fori := 0 to n-1 do
write(c[i], ‘ ‘); output the elements of the array through the writeln space
;
fori := pos-1 ton-2 do
c[i] := c[i+1]; Shifting SetLength(c, n-1) elements
reducing the length of the array by 1
write ('Second array: ');
fori := 0 to n-2 do
write(c[i], ‘ ‘); output the array again
to the end.
Withdrawal:1 1 2 1 1 1
1 1 1 1 1 1 1

A list is analogous to a dynamic array

In addition to variable-length arrays in the Pascal language, there are also structures that allow you to dynamically allocate memory. Among them are connected lists, which are a kind of analogue of dynamic arrays. They are implemented using pointers and the service word record.

An example of implementing a single-linked list:

type
PNode = ^Node;
Node = record
data: integer;
next: PNode;
end;

Also in the Delphi language, which is a natural continuation of Pascal, there is a TList class, which is a list. It has the functions of adding and deleting an item, cleaning and sorting the list itself, and others. But to use it, you need to understand the principles of object-oriented programming (OOP).