Pascal. Arrays

Arrays


Contents





Answers

1. What is the array?

Arrays are the limited and ordered set of the values, which are the same type. Every single value is called the item (component) of array.

 


2. What data types can be the array items?

The array type can be any, adopted in Pascal, except for the file type. This type is a base type.

 


3. How to define the one-dimensional array?

According to Pascal syntax, you can define the array by two ways. First way – in the “var” section. The second way – by using his own type definition in the “type” section.

Way 1. Definition of one-dimensional array in the var section of variables definition.

var
  m1:array [1..100] of integer; // an array of 100 integers
  m2:array [1..50] of real; // an array of 50 real numbers
  m3:array [-20..30] of char; // an array of 51 characters
  m4:array [5..5] of boolean; // an array that contains 1 item of "boolean" type

Way 2. Definition of one-dimensional array by using the “type” section.

type
  TM1 = array [1..100] of integer; // an array of 100 integers
  TM2 = array [1..50] of real; // an array of 50 real numbers
  TM3 = array [-20..30] of char; // an array of 51 characters
  TM4 = array [5..5] of boolean; // an array that contains 1 item of boolean type

var
  m1:TM1;
  m2:TM2;
  m3:TM3;
  m4:TM4;

 


4. How get the access to the items of one-dimensional array?

Suppose we have the following description:

var
  m1:array [1..100] of integer; // an array of 100 integers
  m2:array [1..50] of real; // an array of 50 real numbers
  m3:array [-20..30] of char; // an array of 51 characters
  m4:array [5..5] of boolean; // an array, that contains 1 item of boolean type

Then, to assign the value to a concrete item of array, you need to write:

m1[15] := 289; // is entered the number 289 in the 15-th element of the array
m2[50] := -3.85; // is entered the number -3.85 in the 50-th element of array
m3[-8] := 'k';
m4[5] := true;

 


5. Example the code snippet of zeroing array called “M“, containing 100 integers.
var
  M:array [1..100] of integer;
  i:integer;

begin

  ...

  for i := 1 to 100 do
    M[i]:=0;

  ...

end;

 


6. Example of sum calculation of array Mas items, containing 50 real numbers.
var
  Mas:array [1..50] of real; // the array definition 
  i:integer;
  sum:real;

begin

  ...

  sum := 0; // zeroing of sum

  for i := 1 to 50 do // searching the sum
    sum := sum + Mas[i];

  ...

end;

 


7. Example of searching the maximum value in the array of 100 numbers.
var
  Mas:array [1..100] of integer;
  i:integer;
  max:integer; // max - maximum value

begin

  ...

  max:=Mas[1];
  for i:=2 to 100 do
    if max<Mas[i] then
      max:=Mas[i];

  ...

end;

 


8. An example of determining the presence of a given element in the array of 100 integers.

In the example below, the array is saved in the variable Mas. The item, which you need to find, is saved in the variable num.

var
  Mas:array [1..100] of integer;
  i:integer;
  num:integer; // the desired item
  f_is:boolean;

begin

  ...

  f_is:=false;

  for i:=1 to 100 do
    if num=Mas[i] then
      f_is:=true;

  ...

end;

 


9.An example of sorting of one-dimensional array of real numbers using the insertion sort.

Code snippet, that sorts the array of integers, where maximum items is equal MaxItems. The items are sorted in descending order.

const
  MaxItems = 100;

var
  M:array [1..MaxItems] of integer;
  i,j:integer;
  t:integer;

begin

  ...

  for i:=1 to MaxItems-1 do
    for j:=i downto 1 do
      if M[j]<M[j+1] then
      begin
        t:=M[j]; // the exchange of places items M [j] and M [j + 1]
        M[j]:=M[j+1];
        M[j+1]:=t;
      end;

  ...

end;

 


10. How in Pascal is performed the assignment of the value of one array to another array?

Code snippet, where the value of array M1 is assigned into array M2. The arrays must be the same type.

var
  M1, M2:array [1..MaxItems] of integer;

begin

  ...

  M2:=M1;

  ...

end;

 


11. How is realized the initialization of one-dimensional array?

The initialization of one-dimensional array is realized in the section const. In the code snippet below, two arrays are initialized:

  • array of 3 items, which are reals;
  • array of 5 items, which are integers.

 

const
  mr:array [1..3] of real = (0.45, 9.56, 3.7); // array of reals
  mi:array [1..5] of integer = (2, -30, 0, -100, 23); // array of integers

 



12. An example of definition and using of two-dimensional array in program.

There are two ways of definition of two-dimensional arrays.

Example 1. Definition of two-dimensional array in the section var.

var
  Matr1:array [1..5, 1..6] of real; // matrix of real numbers of size 5 * 6
  Matr2:array [1..20, -5..10] of real; // matrix of real numbers
  Matrix:array[1..10,1..10] of integer; // matrix of integers of size 10*10

Example 2. Definition with using the type section.

type
  TMatrReal_5_6 = array [1..5, 1..6] of real;
  TMatrReal = array [1..20, -5..10] of real;
  TMatrInt = array [1..10, 1..10] of integer;

var
  Matr1:TMatrReal_5_6; // matrix of reals of size 5*6
  Matr2:TMatrReal; // matrix of reals
  Matrix:TMatrInt; // matrix of integers of size 10*10

An example of assignment of values to the items of two-dimensional array.

Matr1[2,3]:=2.93; // in the 2nd row and 3rd column
Matr2[20,-3]:=3.23; // line with an index of 20 and a column with index -3
Matrix[2,8]:=209;

When referring to the matrix in square brackets indicate the first line number, in the second – the number of the column.

 


13. Examples of definition of many-dimensional arrays.

In the example below is defined the three-dimensional and four-dimensional arrays.

type
  TM3 = array[1..5, 1..10, 1..20] of integer; // three-dimensional array of integers
  TM4 = array[1..2, 1..3, 1..5, 1..10] of real; // four-dimensional array of reals

var
  M3:TM3; // variable of type TM3
  M4:TM4; // variable of type TM4

begin

  ...
  
  M3[2,3,6]:=23;
  M4[2,1,3,9]:=398.93;

  ...

end;

 


14. An example of initializing of two-dimensional array.

In the following example two-dimensional arrays of integers are initialized, which are described by a different ways. According to syntax of Pascal the initializing of arrays must be realized in the section const (the constants definition section).

type
  TMatr_2_2 = array [1..2, 1..2] of integer;

const
  Matr23:array[1..2,1..3] of integer = ( (33, -6, 55),
                                         (20, 18, -9) );
  Matr22:TMatr_2_2 = (( 4, -6),
                      (-3,  7));

 


15. An example of initializing of multidimensional array.

The initialization of three-dimensional array of integers:

type
  TM3 = array [1..2,1..3,1..4] of integer;

const
  Mas3 : TM3 = (
                (( 1, 2, 3, 4),
                 ( 2, 1, 6, 5),
                 ( 1, 4, 2, 3)),

                (( 1, 2, 0, 3),
                 (-1,-2, 5, 2),
                 ( 3, 1, 2, 4))
               );

 


16. An example of zeroing of two-dimensional matrix of integers of size 10*10.

In the example below the matrix named “Matr” is zeroing.

var
  Matr:array [1..10, 1..10] of integer; // matrix definition
  i, j:integer;

begin

  ...

  // zeroing of matrix
  for i:=1 to 10 do

    for j:=1 to 10 do
      Matr[i,j]:=0;

  ...

end;

 


17. An example of searching of a given item in the integers matrix of size 20*20.

In the given example num-value is the desired item, f_is – a flag that determines the presence of element in the matrix. It is considered that the matrix Matr already full of values.

var
  Matr:array [1..20, 1..20] of integer;
  f_is:boolean;
  num:integer; // desired number
  i, j:integer;

begin

  ...

  f_is:=false; // First, we believe that there is no item in the matrix

  for i:=1 to 20 do

    for j:=1 to 20 do
      if Matr[i,j]=num then
        f_is:=true;

  ...

end;

 


18. An example of determining the minimal value in the matrix of reals.

In the example, the desired minimal value is saved in the variable min. It is considered that matrix Matr already full of values.

var
  Matr:array [1..10, 1..10] of real;
  min:real; // desired minimal value
  i, j:integer;

begin

  ...

  min:=Matr[1,1]; // Assumed that min - is one of the elements of the matrix

  for i:=1 to 10 do

    for j:=1 to 10 do
      if Matr[i,j]<min then
        min:=Matr[i,j];

  ...

end;

 


19. An example of calculating the sum of items of matrix of reals.

In the example, the sum of matrix items is saved in the variable sum. It is considered that matrix is full of values.

var
  Matr:array [1..10, 1..10] of real;
  suma:real; // desired sum of items
  i, j:integer;

begin

  ...

  suma:=0;
  for i:=1 to 10 do
    for j:=1 to 10 do
      suma := suma + Matr[i,j];

  ...

end;

 


20. An example of definition and using of one-dimensional array of structures of type BOOK.

Suppose we have the following description:

type
  // The definition the structure of type BOOK
  TBOOK = record
    Title:string; // Book title
    Author:string; // Name of author
    year:integer; // The year of publishing
    price:real; // Book cost
  end;

var
  BOOKS:array [1..100] of TBOOK; // array of 100 structures of type TBOOK

Example of accessing an array BOOKS structures. In this example, in the element at index 3 of array “BOOKS” the data are saved.

...

begin

  ...

  BOOKS[3].Title := 'Title of book 3';
  BOOKS[3].Author := 'Author of book 3';
  BOOKS[3].year := 1993;
  BOOKS[3].price := 23.45;

...

end;

 


21. An example of definition and using the array of strings.
var
  MasStr:array [1..10] of string; // definition of array of strings

begin

  ...

  // Write the text into the 3rd element of the array
  MasStr[3] := 'Array of text';

  ...

end;

 


22. An example of definition and using the array of classes.

In the example the class named TMyClass is described. In the class the two variables with names x and y are declared. Access for these variables is public.

type
  // definition of class
  TMyClass = class
    public
      x:integer; // internal variables of class
      y:integer;
  end;

  // Definition of classes array
  TArrayClass = array [1..10] of TMyClass;

...

var
  MasClass:TArrayClass;

...

begin

  ...

  // fill the values of class with index 2
  MasClass[2].x:=25;
  MasClass[2].y:=30;

  ...

end;