**Arrays**

## Contents

- 1. What is the array?
- 2. What data types can be the array items?
- 3. How to define the one-dimensional array?
- 4. How get the access to the items of one-dimensional array?
- 5. Example the code snippet of zeroing array called “M“, containing 100 integers.
- 6. Example of sum calculation of array Mas items, containing 50 real numbers.
- 7. Example of searching the maximum value in the array of 100 numbers.
- 8. An example of determining the presence of a given element in the array of 100 integers.
- 9.An example of sorting of one-dimensional array of real numbers by the bubble method.
- 10. How in Pascal is performed the assignment of the value of one array to another array?
- 11. How is realized the initialization of one-dimensional array?
- 12. An example of definition and using of two-dimensional array in program.
- 13. Examples of definition of many-dimensional arrays.
- 14. An example of initializing of two-dimensional array.
- 15. An example of initializing of multidimensional array.
- 16. An example of zeroing of two-dimensional matrix of integers of size 10*10.
- 17. An example of searching of a given item in the integers matrix of size 20*20.
- 18. An example of determining the minimal value in the matrix of reals.
- 19. An example of calculating the sum of items of matrix of reals.
- 20. An example of definition and using of one-dimensional array of structures of type BOOK.
- 21. An example of definition and using the array of strings.
- 22. An example of definition and using the array of classes.

## 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.

varm1:array[1..100]ofinteger; //an array of 100 integersm2:array[1..50]ofreal; //an array of 50 real numbersm3:array[-20..30]ofchar; //an array of 51 charactersm4:array[5..5]ofboolean; //an array that contains 1 item of "boolean" type

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

typeTM1 =array[1..100]ofinteger; //an array of 100 integersTM2 =array[1..50]ofreal; //an array of 50 real numbersTM3 =array[-20..30]ofchar; //an array of 51 charactersTM4 =array[5..5]ofboolean; //an array that contains 1 item of boolean typevarm1: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:

varm1:array[1..100]ofinteger; //an array of 100 integersm2:array[1..50]ofreal; //an array of 50 real numbersm3:array[-20..30]ofchar; //an array of 51 charactersm4:array[5..5]ofboolean; //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 arraym2[50] := -3.85; //is entered the number -3.85 in the 50-th element of arraym3[-8] := 'k'; m4[5] := true;

### ⇑

**5. Example the code snippet of zeroing array called “M“, containing 100 integers.**

varM:array[1..100]ofinteger; i:integer;begin...fori := 1to100doM[i]:=0; ...end;

### ⇑

**6. Example of sum calculation of array Mas items, containing 50 real numbers.**

varMas:array[1..50]ofreal; //the array definitioni:integer; sum:real;begin... sum := 0; //zeroing of sumfori := 1to50do//searching the sumsum := sum + Mas[i]; ...end;

### ⇑

##### 7. **Example of searching the maximum value in the array of 100 numbers.**

varMas:array [1..100]ofinteger; i:integer; max:integer; //max - maximum valuebegin... max:=Mas[1];fori:=2to100doifmax<Mas[i]thenmax:=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.

varMas:array[1..100]ofinteger; i:integer; num:integer; //the desired itemf_is:boolean;begin... f_is:=false;fori:=1to100doifnum=Mas[i]thenf_is:=true; ...end;

### ⇑

##### 9.**An example of sorting of one-dimensional array of real numbers by the bubble method.**

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

constMaxItems = 100;varM:array [1..MaxItems] of integer; i,j:integer; t:integer;begin...fori:=1toMaxItems-1doforj:=idownto1doifM[j]<M[j+1]thenbegint:=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.

varM1, M2:array[1..MaxItems]ofinteger;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.

constmr:array[1..3]ofreal = (0.45, 9.56, 3.7); // array of reals mi:array[1..5]ofinteger = (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**.

varMatr1:array[1..5, 1..6]ofreal; //matrix of real numbers of size 5 * 6Matr2:array[1..20, -5..10]ofreal; //matrix of real numbersMatrix:array[1..10,1..10]ofinteger; //matrix of integers of size 10*10

**Example 2.** Definition with using the **type** section.

typeTMatrReal_5_6 =array[1..5, 1..6]ofreal; TMatrReal =array[1..20, -5..10]ofreal; TMatrInt =array[1..10, 1..10]ofinteger;varMatr1:TMatrReal_5_6; //matrix of reals of size 5*6Matr2:TMatrReal; //matrix of realsMatrix: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 columnMatr2[20,-3]:=3.23; //line with an index of 20 and a column with index -3Matrix[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.

typeTM3 =array[1..5, 1..10, 1..20]ofinteger; // three-dimensional array of integers TM4 =array[1..2, 1..3, 1..5, 1..10]ofreal; // four-dimensional array of realsvarM3:TM3; //variable of type TM3M4:TM4; //variable of type TM4begin... 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).

typeTMatr_2_2 =array[1..2, 1..2]ofinteger;constMatr23:array[1..2,1..3]ofinteger = ( (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:

typeTM3 =array[1..2,1..3,1..4]ofinteger;constMas3 : 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.

varMatr:array[1..10, 1..10]ofinteger; //matrix definitioni, j:integer;begin... // zeroing of matrixfori:=1to10doforj:=1to10doMatr[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.

varMatr:array[1..20, 1..20]ofinteger; f_is:boolean; num:integer; //desired numberi, j:integer;begin... f_is:=false; //First, we believe that there is no item in the matrixfori:=1to20doforj:=1to20doifMatr[i,j]=numthenf_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.

varMatr:array[1..10, 1..10]ofreal; min:real; //desired minimal valuei, j:integer;begin... min:=Matr[1,1]; //Assumed that min - is one of the elements of the matrixfori:=1to10doforj:=1to10doifMatr[i,j]<minthenmin:=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.

varMatr:array[1..10, 1..10]ofreal; suma:real; //desired sum of itemsi, 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 BOOKTBOOK =recordTitle:string; //Book titleAuthor:string; //Name of authoryear:integer; //The year of publishingprice:real; //Book costend;varBOOKS:array[1..100]ofTBOOK; //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.**

varMasStr:array[1..10]ofstring; //definition of array of stringsbegin... //Write the text into the 3rd element of the arrayMasStr[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

**are declared. Access for these variables is public.**

*y*type//definition of classTMyClass =classpublicx:integer; //internal variables of classy:integer;end; //Definition of classes arrayTArrayClass =array[1..10]ofTMyClass; ...varMasClass:TArrayClass; ...begin... //fill the values of class with index 2MasClass[2].x:=25; MasClass[2].y:=30; ...end;