Содержание:
Theory
Lection in pdf format
Filling an array with generated sequence. lambda expressions
var a := Arr(1,3,5,7,9); // [1,3,5,7,9] var a := ArrFill(5,555); // [555,555,555,555,555] var a := Arr(55) * 4 + Arr(77) * 6; // [55,55,55,55,77,77,77,77,77,77] var a := ArrGen(10,i->i); // [0,1,2,3,4,5,6,7,8,9] var a := ArrGen(10,i->i,1); // [1,2,3,4,5,6,7,8,9,10] var a := ArrGen(10,1,x->x+2); // [1,3,5,7,9,11,13,15,17,19] var a := ArrGen(10,1,x->x*2); // [1,2,4,8,16,32,64,128,256,512] var a := ArrGen(10,1,1,(x,y)->x+y); // [1,1,2,3,5,8,13,21,34,55] |
Array input and Randomly generated array
var a:=ReadArrInteger(5); var a:=ReadArrReal(5); var a:=new integer[10]; a:=arrRandomInteger(10); // array of 10 generated integers from 0 to 99 |
Params keyword to pass a varying number of parameters
1 2 3 4 5 6 7 8 9 10 11 | function Sum(params a: array of integer): integer; begin Result := 0; foreach var x in a do Result += x; end; begin Print(Sum(1,3,5)); Print(Sum(1,4,10,22,44)); end. |
params should be the last one in the listSetLength procedure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function MakeArr (params a: array of integer): array of integer; begin var b := new integer[a.length]; var j := 0; for var i := 0 to a.length - 1 do if a[i] > 0 then begin b[j] := a[i]; j += 1; end; SetLength(b, j); // to specify a particular value of b array size result := b; end; begin var a := arrRandomInteger(5, -5, 10); // array a [-1,9,9,7,-3,0,-2,1,1,-1] println('array a', a); println('result of function, array b ', MakeArr(a)); // array b [9,9,7,1,1] end. |
Reassignment and copy function
var a: array of integer := (1,3,5,7); var b:=a; // [1,3,5,7] |
But!
b, then the array a will also change:var a: array of integer := (1,3,5,7); var b:=a; b[2]:=1; print(a); //[1,3,1,7] |
b as a copy of array a:var a: array of integer := (1,3,5,7); var b:=Copy(a); b[2]:=1; print(a); //[1,3,5,7] |
Standard functions and procedures
Reverse(a)a.IndexOf(x)a.LastIndexOf(x)a.Contains(x)
Reverses elements in a array
Finds the position (index) of x in a array
Finds the last position (index) of x in a array
Checks if the a array contains x
Labs and tasks
Filling an array with generated sequence
{0.2 points} Task 1:
To do: Generate an array of integers from -20 to 30. You should use ArrGen() function.
Expected output:
[-20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[Program name: 13task-01.pas]
{0.2 points} Task 2:
To do: Generate an array of integers from -10 to 10 in increments of 2 (step = 2). You should use ArrGen() function.
Expected output:
[-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
[Program name: 13task-02.pas]
{0.2 points} Task 3:
To do: Generate an array of 20 numbers 5. You should use ArrFill() function.
Expected output:
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
[Program name: 13task-03.pas]
{0.2 points} Task 4:
To do: Generate an array of the products of twos of numbers beginning with 1: i.e. 1 * 2, 2 * 2, 2 * 4, …). You should use ArrGen() function with two variables (x & y).
Expected output:
[1,2,2,4,8,32,256,8192]
[Program name: 13task-04.pas]
Params keyword. SetLength procedure
To do: Create a function to add any number of integers.
✍ Algorithm:
paramskeyword is used to make it able to pass a varying number of parameters.- In the case of more than one parameter,
paramsshould be the last one in the list
1 2 3 4 5 6 7 8 9 10 11 | function Sum(params a: array of integer): integer; begin Result := 0; foreach var x in a do Result += x; end; begin Print(Sum(1,3,5)); Print(Sum(1,4,10,22,44)); end. |
{0.4 points} Task 5:
To do: Create a PrintArr procedure that prints out any number of numbers, delimited by some character (the numbers must be understood as an array parameter, and there must be also a delim parameter, the value of which is a character). Inside the main program, call the procedure several times with different number of parameters.
Note: Procedure call example:
var a:=new integer[10]; a:=arrRandomInteger(10); PrintArr(';',a); PrintArr('~',1,2,3); // some more procedure call |
Extra task: * Correct the PrintArr procedure so that there is no delimeter after the last element.
Expected output:
76 ; 22 ; 71 ; 92 ; 51 ; 68 ; 70 ; 47 ; 15 ; 37 ; 1 ~ 2 ~ 3 ~ 5 / 2 / 3 / 5 /
[Program name: 13task-05.pas]
{0.4 points} Task 6:
To do: Three integer array of different sizes are generated randomly. Write a procedure EvenDouble with one parameter: an integer array of any size. Procedure must double all the elements of the array with even values and print out the resulting array.
Note: Inside the main program, call the procedure several times with the arrays of different sizes:
var a := new integer[10]; a := arrRandomInteger(5,-5,5); EvenDouble(a); var a1 := new integer[5]; ... // some more procedure calls |
Expected output:
the array before the task is done: [33,0,52,35,70,56,83,12,68,78] result: 33 0 104 35 140 112 83 24 136 156 --- the array before the task is done: [5,-1,4,1,1] result: 5 -1 8 1 1
[Program name: 13task-06.pas]
To do: create a function to fill array b with positive numbers of array a
✍ Algorithm:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function MakeOddArrFunc(params a: array of integer): array of integer; begin var b := new integer[a.length]; var j := 0; for var i := 0 to a.length - 1 do if a[i] > 0 then begin b[j] := a[i]; j += 1; end; SetLength(b, j); result := b; end; begin var a := arrRandomInteger(10, -5, 10); println('array a', a); println('result of function, array b ', MakeOddArrFunc(a)); end. |
{0.4 points} Task 7:
To do: Create a function to fill array b with local minimums of array a. The local minimum is the element that is less than each of its neighbours. The first and last element should not be considered as local minima.
Expected output:
array a [6,-2,3,-4,6] result of function, array b [-2,-4]
[Program name: 13task-07.pas]