Arrays
Content

1. What is the array?

2. What data types can be the array items?

3. How to define the onedimensional array?

4. How get the access to the items of onedimensional 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 onedimensional 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 onedimensional array?

12. An example of definition and using of twodimensional array in program.

13. Examples of definition of manydimensional arrays.

14. An example of initializing of twodimensional array.

15. An example of initializing of multidimensional array.

16. An example of zeroing of twodimensional 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 onedimensional 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 onedimensional 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 onedimensional 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 onedimensional 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 onedimensional 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 15th element of the array m2[50] := 3.85; // is entered the number 3.85 in the 50th 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 onedimensional 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.
const MaxItems = 100; var M:array [1..MaxItems] of integer; i,j:integer; t:integer; begin ... for i:=1 to MaxItems1 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 onedimensional array?
The initialization of onedimensional 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 twodimensional array in program.
There are two ways of definition of twodimensional arrays.
Example 1. Definition of twodimensional 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 twodimensional 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 manydimensional arrays.
In the example below is defined the threedimensional and fourdimensional arrays.
type TM3 = array[1..5, 1..10, 1..20] of integer; // threedimensional array of integers TM4 = array[1..2, 1..3, 1..5, 1..10] of real; // fourdimensional 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 twodimensional array.
In the following example twodimensional 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 threedimensional 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 twodimensional 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 numvalue 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 onedimensional 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;