Тренировка задания 17 ЕГЭ (17.2)

17 задание с сайта К. Полякова, № 28:

Рассматривается множество целых чисел, принадлежащих числовому отрезку [3439; 7410], которые удовлетворяют следующим условиям:

  • запись в двоичной и шестеричной системах счисления заканчивается разными цифрами;
  • кратны 9, 10 или 11.
  • Найдите количество таких чисел и максимальное из них.

    ! Для выполнения этого задания можно написать программу или воспользоваться редактором электронных таблиц

    Ответ: 683   7407

    Тренировка задания 17 ЕГЭ (17.1)

    17 задание с сайта К. Полякова, № 1:

    Рассматривается множество целых чисел, принадлежащих числовому отрезку [1012; 9638], которые делятся на 3 и не делятся на 11, 13, 17, 19. Найдите количество таких чисел и максимальное из них.
    В ответе запишите два целых числа: сначала количество, затем максимальное число.

    ! Для выполнения этого задания можно написать программу или воспользоваться редактором электронных таблиц

    Ответ: 2151 | 9630

    Тренировка задания 15 ЕГЭ (15.5)

    Решение 15 задания (Вариант 139, К. Поляков) :

    Обозначим через ДЕЛ(n, m) утверждение «натуральное число n делится без остатка на натуральное число m».

     
    Для какого наименьшего натурального числа А формула

    (ДЕЛ(x, A) ∧ (ДЕЛ(x, 21)) → ДЕЛ(x, 18)

    тождественно истинна (то есть принимает значение 1 при любом натуральном значении переменной х)?

    Ответ: 18


    Тренировка задания 15 ЕГЭ (15.7)

    Решение 15 задания (Вариант 132, К. Поляков) :

    Обозначим через ДЕЛ(n, m) утверждение «натуральное число n делится без остатка на натуральное число m».

      
    Для какого наибольшего натурального числа А формула

    ¬ДЕЛ(x, А) → (¬ДЕЛ(x, 24)  ∧ ¬ДЕЛ(x, 36)) 

    тождественно истинна (то есть принимает значение 1 при любом натуральном значении переменной х)?

    Ответ: 12


    Lesson #16. Sequence

    Theory

    Lection in pdf format

    Standard sequence generators

      Range(a,b: integer): sequence of integer

      print(Range(1,10)); // [1,2,3,4,5,6,7,8,9,10]

      Range(a,b,step: integer): sequence of integer

      print(Range(1,10,2)); // [1,3,5,7,9]

      Range(c1,c2: char): sequence of char

      print(Range('a','k')); // [a,b,c,d,e,f,g,h,i,j,k]

      Partition(a,b: real; n: integer): sequence of real

      print(Partition(0.0, 6.0, 4)); // делим поровну на 4 части [0, 1.5, 3, 4.5, 6]

      SeqRandomInteger(n: integer[; a,b: integer]): sequence of integer;

      var q:=SeqRandomInteger(5,10,20);
      print(q); // [12,18,16,14,16]

      SeqRandomReal(n: integer[; a,b: real]): sequence of real;

      var q:=SeqRandomReal(3, 1.0, 5.0);
      print(q); // [4.98996168374548,2.22339218166815,2.81110574389394]

      Seq(params a: array of T): sequence of T;

      foreach var x in Seq (1,3,8) do
          print(x*x); // 1 9 64

      SeqFill(count: integer; x: T): sequence of T;

      begin
      var q:=SeqFill(7,5);
      print(q); // [5,5,5,5,5,5,5] 
      end.

    Sequence generators with lambda-expressions

    The following functions are used to generate sequences using lambda expressions:

    • SeqGen(count: integer; f: integer -> T): sequence of T;
    • begin
      var sq:=SeqGen(5,x->x+1);
      sq.println; // 1 2 3 4 5
      end.
    • SeqGen(count: integer; first: T; next: T -> T): sequence of T;
    • begin
      var sq:=SeqGen(5, 4, x->x+1);
      sq.println; // 4 5 6 7 8
      end.
    • SeqGen(count: integer; first,second: T; next: (T,T) -> T): sequence of T;
    • begin
      var sq:=SeqGen(5, 1,3, (x,y)->x+y);
      sq.println; // 1 3 4 7 11
      end.
    • SeqWhile(first: T; next: T -> T; pred: T -> boolean): sequence of T;
    • Condition is added:

      begin
      var sq:=seqWhile(2,x->x*2,x->x<=1024);
      sq.println; // 2 4 8 16 32 64 128 256 512 1024
      end.
    • SeqWhile(first,second: T; next: (T,T) -> T; pred: T -> boolean): sequence of T;
    • SeqGen(count: integer; f: integer -> T): sequence of T;

    Infinite sequence generators

    • Cycle()
    • Repeating a sequence block

      Seq(1,2,10,5).Cycle().Take(15).Println; // 1 2 10 5 1 2 10 5 1 2 10 5 1 2 10

      Take is used to restrict

    • Repeat
    • Infinite sequence of numbers

      var q:=55.Repeat.Take(10).Println; // 55 55 55 55 55 55 55 55 55 55
    • Step
    • Generating an infinite sequence with a step

      var q:=5.Step(2).Take(10).Println; // 5 7 9 11 13 15 17 19 21 23
    • Iterate
    • Generating an infinite sequence using a lambda-function

      var q:=10.Iterate(x->x-2).Take(10).Println; // 10 8 6 4 2 0 -2 -4 -6 -8

    Labs and tasks

    Lab 1.

    To do: Ask user to input 10 integer elements of the sequence. Calculate a sum of the seq elements.

    The result examples:

    1
    2
    3
    4
    1
    6
    7
    2
    5
    6
    sum = 37 
    

    ✍ Algorithm:

      begin
      var q:=ReadSeqReal(10); 
      var s:=0.0;
      foreach var x in q do
        s+=x;
      print('sum = ', s)
      end.
    {0.3 points} Task 1:

    To do: Create the function to calculate the product (multiplication) of 5 entered sequence elements.

    Note: the signature of the function:

    function Product(q: sequence of real):real;

    The resulting example:

    Please, enter 5 real numbers:
    1.2
    3.1
    6.7
    2.8
    3.4
    product:  237.27648 
    

    [Program name: 16task-01.pas]

    {0.2 points} Task 2:

    To do: Create sequence of integers from -20 to 30 with a step of 3 (Range function).

    The resulting example:

    [-20,-17,-14,-11,-8,-5,-2,1,4,7,10,13,16,19,22,25,28] 
    

    [Program name: 16task-02.pas]

    {0.2 points} Task 3:

    To do: Create N random numbers of a sequence from -50 to 100 (SeqRandom function).

    The resulting example:

    enter n 
    >>15
    [-23,13,-27,2,46,-26,10,92,60,-9,75,28,85,7,18] 
    

    [Program name: 16task-03.pas]

    Lab 2: Methods

    To do: Create a function that searches in a sequence for the quantity of maximum elements.

    The resulting example:

    1 5 2 10 1 10
    number of max elements is 2 
    

    ✍ Algorithm:

      function findCountMax(a: sequence  of integer):integer;
      begin
      var k:=a.Max();
        foreach var i in a do
          if i=k then result+=1;
      end;
      begin
        var c:=Seq(1,5,2,10,1,10);
        c.Println();
        println('number of max elements is ',findCountMax(c));
      end.
    {0.4 points} Task 4:

    To do: Create a function that calculates a sum of odd elements in a sequence. You should use IsOdd method within the foreach loop.

    The resulting example:

    1 5 2 10 1 10
    sum of odd elements  7 
    

    [Program name: 16task-04.pas]

    Lab 3.

    To do: Define a sequence using Seq method. Calculate a quantity of entered number within the sequence. You should use count (condition) method.

    The result examples:

    -1 2 3 -5 2 -7 8 2 11
    enter x: 2
    there is 2 within the seq 3 times 
    

    ✍ Algorithm:

      begin
        var c:=Seq(-1,2,3,-5,2,-7,8,2,11);
        c.Println();
        var x:=readinteger('enter x:');
        var n:=c.count(c->c=x);
        println('number of negative elements is ',n);
      end.
    {0.3 points} Task 5:

    To do: Define a sequence using Seq method. Calculate a number of negative elements in a sequence. You should use count (condition) method.

    The resulting example:

    -1 2 3 -5 6 -7 8 9 11
    number of negative elements is 3
    

    [Program name: 16task-05.pas]

    Sequence generators with lambda-expressions

    {0.2 points} Task 6:

    To do: Using lambda expression, create a sequence of N even numbers starting at 10 (SeqGen should be used)

    The resulting example:

    Enter n 
    >> 7
    10 12 14 16 18 20 22
    

    [Program name: 16task-06.pas]

    {0.2 points} Task 7:

    To do: Create the following sequence: 1 3 9 27 81 243 729 2187 6561 19683 (SeqGen should be used)

    The resulting example:

    3 9 27 81 243 729 2187 6561 19683
    

    [Program name: 16task-07.pas]

    {0.3 points} Task 8:

    To do: Create a sequence of 15 numbers: 2 1 0.5 0.25 0.125 0.0625 0.03125 0.015625 ... (specifying the first element and the function to get the next one) (SeqGen should be used)

    The resulting example:

    2  1  0.5  0.25  0.125  0.0625  0.03125  0.015625 0.0078125 0.00390625 
    0.001953125 0.0009765625 0.00048828125 0.000244140625 0.0001220703125 
    

    [Program name: 16task-08.pas]

    {0.2 points} Task 9:

    To do: Create the following sequence: 2017 2012 2007 2002 1997 1992 1987 1982 1977 1972 (SeqGen should be used)

    The resulting example:

    2017 2012 2007 2002 1997 1992 1987 1982 1977 1972
    

    [Program name: 16task-09.pas]

    Lab:

    To do: Create a sequence of N Fibonacci numbers

    The resulting example:

    Enter n 
    >>8
    1 1 2 3 5 8 13 21
    

    ✍ Algorithm:

      begin
      var n:=readInteger('Enter n');
      var sq:=SeqGen(n,1,1,(x,y)->x+y);
      sq.println();
      end.
    Lab:

    To do: Create a sequence of N numbers generated by the iterative process: а1=2, аk=(аk-1+1)*аk-1, k = 2,3,… (SeqGen should be used)

    The resulting example:

    Enter n 7
    2 6 42 1806 3263442 -1461943274 -757910022
    

    ✍ Algorithm:

      begin
      var n:=readInteger('Enter n');
      var sq:=SeqGen(n,2,x->(x+1)*x);
      sq.println();
      end.
    {0.4 points} Task 10:

    To do: Create a sequence of N numbers generated by the iterative process: a1 = 5, ak = ak-1 / 2-1, k = 2,3, … (SeqGen should be used)

    The resulting example:

    Enter n 
    >> 9
    5  1.5  -0.25  -1.125  -1.5625 -1.78125 -1.890625 -1.9453125 -1.97265625
    

    [Program name: 16task-10.pas]

    {0.4 points} Task 11:

    To do: Create a sequence of N numbers generated by the iterative process: а1=1, а2=2, аkk-1+2аk-2, k=3,… (SeqGen should be used)

    The resulting example:

    Enter n 
    >>7
    1 2 4 8 16 32 64
    

    [Program name: 16task-11.pas]

    {0.4 points} Task 12:

    To do: Create a sequence of N numbers generated by the iterative process: а1=1, а2=2, аk=(аk-1k-2)/2, k=3,… (SeqGen should be used)

    The resulting example:

    Enter n 
    >> 7
    1 2 1.5 1.75 1.625 1.6875 1.65625
    

    [Program name: 16task-12.pas]

    Lesson #15. Matrices

    Theory: Two-dimensional arrays

    Lection in pdf format

    • Definition
    • A matrix or two-dimensional array is a rectangular table of numbers (or other elements of the same type). Each element of the matrix has two indices (row number and column number).

    • Matrix declaration
    • var a2: array [,] of integer;
      a2 := new integer[3,4];
      // or:
      var a := new integer[3,4];
    • Loop over the elements and pronting out:
    • for var i:=0 to a2.RowCount-1 do
        	for var j:=0 to a2.ColCount-1 do
          		a2[i,j] := i + j;  
       
      a2.Println;
      Println(a2); // [[0,1,2,3],[1,2,3,4],[2,3,4,5]]

    Matrix filling

      begin
        var a := Matr(3,4,1,2,3,4,5,6,7,8,9,10,11,12);  
        a.Println(3);
       
        var a1 := MatrGen(3,4,(i,j)->i+j+0.5); // using arrow operator
        a1.Println(5,1);
      end.
    • Filling with random numbers
    • begin
        var a := MatrRandomInteger(3,4);  
        a.Println(4);
       
        var a1 := MatrRandomReal(3,4,1,9);  
        a1.Println(6,2);
      end.

    Operations by rows and columns

    begin
      var a := MatrRandomInteger(3,4);  
      a.Println;
     
      a.Row(0).Sum.Println;
      a.Row(1).Average.Println;
      a.Row(2).Product.Println;
     
      a.Col(0).Min.Println;
      a.Col(1).Max.Println;
    end.

    Labs and tasks

    Lab 0. Two-dimensional array

    To do: Initialize a two-dimensional array named arr2d with given integer values, they are [[1, 2], [3, 4], [5, 6]].
    1) First, output all the elements of the array.
    2) After, output the following elements to the Output window (the elements to output are marked in red color):

    (1, 2)
    (3, 4) 
    (5, 6)
    

    The result examples:

    The array:
    1  2
    3  4
    5  6
    1-st element = 3, 2-nd element = 5
    

    ✍ Algorithm:

      begin
        var a := new integer[3,2];
       
        for var i:=0 to a.RowCount-1 do
        	for var j:=0 to a.ColCount-1 do
                a[i,j]:=readinteger;  
       
        println('The array:');
        a.Println();
        Println(a[1,0], a[2,0]); 
      end.
    {0.6 points} Task 1:

    To do: The air temperature values for 4 days are given, they are taken from three weather stations located in different regions of the country:

    Station number 1-st day 2-nd day 3-d day 4-th day
    1 -8 -14 -19 -18
    2 25 28 26 20
    3 11 18 20 25

    That is, in a two-dimensional array it would look like this:

    t[0,0]=-8; t[0,1]=-14; t[0,2]=-19; t[0,3]=-18;
    t[1,0]=25; t[1,1]=28; t[1,2]=26; t[1,3]=20;
    t[2,0]=11; t[2,1]=18; t[2,2]=20; t[2,3]=25;

    or it is better to use matr function:

    var t := matr(3,4,-8,-14,-19,-18,25,28,26,20,11,18,20,25);

    To do:

    1. Output value of the temperature at the 2nd weather station during the 4th day and at the 3rd weather station during the 1st day (your result must be 20 and 11).
    2. Output the values of the temperature of all weather stations during the 2nd day (for loop is needed).
    3. Output the values of the temperature of all weather stations during all days.
    4. Calculate the average temperature at the 3rd weather station (for loop is needed).
    5. Output the days and the weather stations’ numbers where the temperature was in the range of 2426 degrees Celsius.

    The resulting example:

    1-st task: 20  and  11
    2-nd task: -14 28 18
    3-d task:
    -8  -14  -19  -18
    25   28   26   20
    11   18   20   25
    
    4-th task: 18.5
    5-th task:
    station number 1  day 0
    station number 1  day 2
    station number 2  day  
    

    [Program name: 15task-01.pas]

    {0.2 points} Task 2:

    To do: Fill a two-dimensional array 4-by-3 ([4,3]) with random integer values. Count positive elements within the array.

    The resulting example:

    The array:
    2   5  -1
    6   7   8
    1   8   6
    -3  1  -6
    number of positive = 9
    

    [Program name: 15task-02.pas]

    {0.5 points} Task 3:

    To do: Fill a two-dimensional array 4-by-5 ([4,5]) with random integer values. Create a function to find a minimum and maximum elements of the array (FindMinMaxArr).

    Note: the signature (header) of the function should be as follows:

    function FindMinMaxArr(a: array [,] of integer): (integer, integer);

    The resulting example:

    The array:
    10   2   9  3  4
    -3 -10 -14 -4  2
     2  -9  11  3 -10
    -1 -13  -5 -2  3
    min and max:
    -14  11
    

    [Program name: 15task-03.pas]

    Lab 1

    To do: Fill a two-dimensional array 3-by-4 ([3,4]) using the rule:
    each element equals to i-th row * j-th column + 0.5
    The result examples:

    0.5  1.5  2.5  3.5
    1.5  2.5  3.5  4.5
    2.5  3.5  4.5  5.5
    

    ✍ Algorithm:

      begin
        var a1 := MatrGen(3,4,(i,j)->i+j+0.5);
        a1.Println(5,1);
      end.
    {0.2 points} Task 4:

    To do: Fill a two-dimensional array 4-by-5 ([4,5]) using the rule: all elements of the i-th row have the value 10 ∗ i.

    The resulting example:

        0    0    0    0    0
       10   10   10   10   10
       20   20   20   20   20
       30   30   30   30   30
    

    [Program name: 15task-04.pas]

    Lab 2

    To do: Calculate sum in each row.

    The result examples:

      15  21   9  78
      54  61   2  19
      19  28  50  61
    123 136 158
    

    ✍ Algorithm:

      begin
        var a := MatrRandomInteger(3,4);  
        a.Println;
       
        var Sums := ArrGen(a.RowCount,r -> a.Row(r).Sum);
        Sums.Println;
      end.
    {0.2 points} Task 5:

    To do: Fill a two-dimensional array 4-by-5 ([4,5]) with random integer values. Calculate maximum element in each row.

    The resulting example:

      91   9  82  71  99
      59  30  36  35  26
      56  66  76   7  23
      25  46  92  39  77
    99 59 76 92
    

    [Program name: 15task-05.pas]

    {0.3 points} Task 6:

    To do: Fill a two-dimensional array 4-by-5 ([4,5]) with random integer values in the range [0,10]. Create a function to return the array of the products (multiplications) in each column.

    The resulting example:

       6   4   6   8   0
       6   4   5   3   7
       0   3   5   2   8
       6   4   6   3   8
    result:  [0,192,900,144,0] 
    

    [Program name: 15task-06.pas]

    Lab 3

    To do: Calculate a number of evens in each row.

    The result examples:

      49  98  78  27
      44  65  50  74
      68   1  75  59
    2 1 2 1
    

    ✍ Algorithm:

      begin
        var a := MatrRandomInteger(3,4);  
        a.Println;
       
        var EvensCount := ArrGen(a.ColCount,c -> a.Col(c).Count(x->x.IsEven));
        EvensCount.Println;
      end.
    {0.3 points} Task 7:

    To do: Fill a two-dimensional array 4-by-5 ([4,5]) with random integer values. Create a procedure to print out the array of the quantities of the matrix elements in the range [0,10] by rows.

    Note: the signature of the procedure must be as follows:

    procedure arrCount(a:array[,] of integer);

    The resulting example:

      59   6  34  70
      11  28  10  31
       8   0   9  80
    1 1 3
    

    [Program name: 15task-07.pas]

    Lab 4

    To do: The matrix is given. Does the matrix contain the element equaled to x? x is entered.

    The result examples:

       3   8   5   7
       1   7   6   1
       1   1   5   9
    enter x, please: 
    >> 6
    True 
    

    ✍ Algorithm:

      function Contains<T>(a: array [,] of T; x: T): boolean;
      begin
        Result := False;
        for var i:=0 to a.RowCount-1 do
         for var j:=0 to a.ColCount-1 do
           if a[i,j]=x then
           begin 
            Result := True;      
            exit;
          end;
      end;
       
      begin
        var a := MatrRandomInteger(3,4,1,10);  
        a.Println;
       
        var x:=readinteger('enter x, please:');
        var found := Contains(a,x);
        Println(found);
      end.
    {0.3 points} Task 8:

    To do: Fill a two-dimensional array 3-by-4 ([3,4]) with random integer values. Create a function to print out the indices (row and column) of the array element equaled to entered x value. If x is not found, the function must return -1,-1.

    Note: the signature of the function must be as follows:

    function indicesArr<T>(a: array [,] of T; x: T): (integer,integer);

    The resulting example:

       1   2   2  10
       3   4   7   5
       3   5   5  10
    enter x, please: 6
    (-1,-1) 
    ---
       3   6   7   1
       7   1   8   5
      10   2   4   6
    enter x, please: 7
    (0,2) 
    

    [Program name: 15task-08.pas]

    {0.8 points} Task 9:

    To do: M-by-N integer matrix is given (it is filled with generated random numbers). Find the number of the first of its rows containing an equal quantity of positive and negative elements (zero matrix elements are not taken into account). If there are no such rows, then output 0.

    Note: you can do the task using one of the following ways: 1) iterating over the elements of the array; 2) using mass operations by rows and columns (.Row)

    The resulting example:

    N: 
    >>3
    M: 
    >>4
      -5  -5   5  -1
       1  -1   0   0
       3  -3  -4   1
     
    result: 2
    

    [Program name: 15task-09.pas]

    Lesson # 9. Classes

    Theory

    • Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.
    • An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
    • Classes are defined using either keyword class or keyword struct, with the following syntax:
    • class class_name {
        access_specifier_1:
          member1;
        access_specifier_2:
          member2;
        ...
      } object_names;
      

      Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, which can either be data or function declarations, and optionally access specifiers.

    • C++ classes encapsulate data and associated functionality into an object:
    • // C++ class: 
      class Cube {
       public:
        double getVolume();
         // ...
       private:
        double length_;
      };
    • Thus, Classes have the same format as plain data structures, except that they can also include functions and have these new things called access specifiers. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights for the members that follow them.
    • Encapsulation encloses data and functionality into a single unit (called a class)
    • In C++, data and functionality are separated into two separate protections: public and private.
    • The protection level determines the access that «client code» has to the member data or functionality:

    • Public members can be accessed by client code. So, public members are accessible from anywhere where the object is visible. protected members are accessible from other members of the same class (or from their «friends»), but also from members of their derived classes.
    • Private members cannot be accessed by client code (only used within the class itself). So, private members of a class are accessible only from within other members of the same class (or from their «friends»).
    • Example of header-file code (Cube.h):

      #pragma once
      class Cube {
       public:
         double getVolume();
         double getSurfaceArea();
         void setLength(double length);
       private:
         double length_;
      };

      Example of implementation-file code (Cube.cpp):

      #include "Cube.h"
      double Cube::getVolume() {
        return length_ * length_ * length_;
      }
      double Cube::getSurfaceArea() {
         return 6 * length_ * length_;
      }
      void Cube::setLength (double length) {
        length_ = length;
      }

      Example of main-file code (main.cpp):

      #include "Cube.h"
      int main() {
       Cube c;
       c.setLength(3.48);
       double volume = c.getVolume();
       std::cout << "Volume: " << volume << std::endl;
       return 0;
      }

      Arguments passed by …

        Identical to storage, arguments can be passed to functions in three different ways:

      • Pass by value (default)
      • Pass by pointer (modified with *)
      • Pass by reference (modified with &, acts as an alias)

      Values returned by …

      Similarly, values can be returned all three ways as well:

    • Return by value (default)
    • Return by pointer (modified with *)
    • Return by reference (modified with &, acts as an alias)
    • Class destructor

      • When an instance of a class is cleaned up, the class destructor is the last call in a class’s lifecycle.
      • An automatic default destructor is added to your class if no other destructor is defined.
      • The only action of the automatic default destructor is to call the default destructor of all member objects.
      • An destructor should never be called directly. Instead, it is automatically called when the object’s memory is being reclaimed by the system:
      • If the object is on the stack, when the function returns
      • If the object is on the heap, when delete is used
      • To add custom behavior to the end-of-life of the function, a custom destructor can be defined as:
      • A custom destructor is a member function.
      • The function’s destructor is the name of the class, preceded by a tilde ~.
      • All destructors have zero arguments and no return type.
      • Cube::~Cube(); // Custom destructor

      Labs and tasks

      Lab 1:
      To do: Create a Rectangle class and an object (i.e., a variable) of this class, called rect.
      1) The class should contain four members:
      — two data members of type int (member width and member height) with private access,
      — and two member functions with public access: the functions set_values and area.

      2) Define two member functions: set_values function should set the variables width and height to the values; are function should return width*height.

      3) Create a Custom default constructor to have the initial values for width and height (set them to 5).

      4) Set the width and height values for the rect object and print out the information about this object.

      Expected output:

      Lab 1:
      please, enter width:
      >> 20
      please, enter height:
      >> 2
      rect area: 40
      

      ✍ Algorithm:

      • To do the lab create empty console application with a name Lesson9. Add files: mainLab1.cpp, ImpLab1.cpp, HeaderLab1.h.
      • Include all the needed libraries and files.
      • 1.Create a Rectangle class:

      • The class must be defined inside a header file. So, open the header file and add the code to define the class with two private data members, they are width and height; and two member functions with public access, they are set_values and area.
      • class Rectangle {
        private:
        	int width, height;
        public:
        	void set_values(int, int);
        	int area(void);
        } ;
      • Here Rectangle is the class name (i.e., the type).
      • The functions set_values and area have a public access, it means that they can be accessed from inside the main function by simply inserting a dot (.) between object name and member name (e.g. rect.set_values…).
      • width and height members cannot be accessed from outside the class, since they have private access and they can only be referred to from within other members of that same class.
      • 2. Define two member functions:

      • Since members width and height have private access, access to them from outside the class is not allowed. So, we should define a member function to set values for those members within the object: the member function set_values.
      • Let’s create the definition of set_values member function. We’re going to have a member of a class outside the class itself, so, we have to use scope operator (::, two colons). You should create the definition of the function inside the implementation file:
      • void Rectangle::set_values(int x, int y) {
        	width = x;
        	height = y;
        }
        The scope operator (::) specifies the class to which the member being defined belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition. For example, the function set_values has access to the variables width and height, which are private members of class Rectangle, and thus only accessible from other members of the class, such as this.
      • The second member function, that is area function, we’re going to have inside the class itself, just to try different ways of definition. So, return to the header file and add the definition inside the class next to int area(void) statement:
      • int area() {return width*height;}
        The function is automatically considered an inline member function by the compiler.

        3) Create a Custom default constructor to have the initial values for width and height (set them to 5).

      • Add the declaration of a Custom default constructor inside the public access of the class (header file):
      • public: // you had this code
        	Rectangle(); 
        	void set_values(int, int); // you had this code
        //...
      • Open your implementation file to add the definition of that constructor:
      •  Rectangle::Rectangle() {
        	width = 5;
        	height = 5;
        }

        4) Set the width and height values for the rect object and print out the information about this object:

      • Open a main file in the editor window. You should create an object (i.e., a variable) of the Rectangle class, called rect. And after this, ask user to input the width and height values. Call the set_values function and output the result:
      • int main() {
        	Rectangle rect;
        	int w, h;
        	cout << "please, enter width:\n";
        	cin >> w;
        	cout << "please, enter height:\n";
        	cin >> h;
        	rect.set_values(w, h);
        	cout << "rect area: " << rect.area() << endl;
        	system("pause");
        	return 0;
        }
      • Run the program and check the output.
      Task 1:

      To do: Create a LessonDate class to output the date of a lesson.

      1) The class should contain the following members:
      — three data members of type int with private access, they are day, month and year;
      — and two member functions with public access:
      void setDate(int, int, int); to set the date for the next lesson and
      void getDate(); to print out the date of the lesson.

      2) Create a three argument constructor to have the initial values for date; in the constructor body call the setDate function to set the date.

      3) Inside the main function create an object (i.e., a variable) of this class, called objLesson. Set the values of three parameters — day, month and year. Print out the information about this object.

      Expected output:

      Task 1:
      Lesson date: 11.11.2021
      Please, enter day, then month and year of the next lesson
      28
      12
      2020
      Lesson date: 28.12.2020
      

      [Solution and Project name: Lesson_9task1, file name L9Task1main.cpp, L9Task1imp.cpp, L9Task1.h]

      Lab 2:
      To do: Create a Cube class.
      1) The class should contain four members:
      length_ data member of type double with private access,
      — three member functions with public access: the functions double getVolume(); , double getSurfaceArea(); and void setLength(double length);.

      2) Give the definitions (implementations) of those functions:
      getVolume() function have to calculate a volume of the cube: length_ * length_ * length_;
      getSurfaceArea() function have to calculate a Surface Area of the cube: 6 * length_ * length_;;
      setLength(double length) function have to set the value for the cube length.

      3) Create a Custom default constructor to have the initial values for length_ (set it to 1).

      4) Inside the main cpp file create double cube_on_stack() function to create an object (i.e., a variable) of this class and get volume of it. This object will be in a stack memory. Also, inside the main function create a new cube of length 10 which is going to be in heap memory. Set the values. Print out the information about those objects.

      5) Create a custom destructor to delete an information about the cube.

      Expected output:

      Lab 2:
      Volume of c cube:
      Destructor called for cube with length 2
      Volume of c cube in the heap memory: 27
      Destructor called for cube with length 3
      

      ✍ Algorithm:

      • To do the lab create an empty console application with a name Lesson9Lab2. Create new files: mainLab2.cpp, ImpLab2.cpp, HeaderLab2.h.
      • Include all the needed libraries and files.
      • 1.The class should contain four members:

      • The interface of the class must be inside a header file. So, open the header file and add the code to define the class with one private data member, it is length_ of a cube; and three member functions with public access, they are getVolume(), getSurfaceArea() and setLength(double length).
      • class Cube
        {
        public:
        	double getVolume();
        	double getSurfaceArea();
        	void setLength(double length);
        private:
        	double length_;
        };
      • Here Cube is the class name (i.e., the type.
      • The functions getVolume(), getSurfaceArea() and setLength(double length) have a public access, it means that they can be accessed from anywhere where the object is visible.
      • length_ member cannot be accessed from outside the class, since it has a private access and it can only be accesses from within the class itself.
      • 2. Give the definitions (implementations) of the functions:

      • Let’s create the definition of setLength member function. We’re going to have a member of a class outside the class itself, so, we have to use scope operator (::, two colons). You should create the definition of the function inside the implementation cpp file:
      • void Cube::setLength(double length) {
        	length_ = length;
        }
        The scope operator (::) specifies the class to which the member being defined belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition. For example, the function setLength has access to the variable length_, which is a private member of the class Cube, and thus only accessible from other members of the class, such as this.
      • The second and th third member functions are getVolume and getSurfaceArea function, we’re going to have them inside the implementation file too:
      • double Cube::getVolume() {
        	return length_ * length_ * length_;
        }
        double Cube::getSurfaceArea() {
        	return 6 * length_ * length_;
        }
        The getVolume function will return the volume of that cube, for which this function will be called.
        The getSurfaceArea function will return the surface area of that cube, for which this function will be called.

        3) Create a custom default constructor to have the initial values for length_ (set it to 1).

      • Add the declaration of a custom default constructor inside the public access of the class (header file):
      • public: // you had this code
        	Cube(); 
                //...
      • Open your implementation file to add the definition of that constructor:
      • Cube::Cube() {
        	length_ = 1;
        }

        4) Inside the main cpp file create double cube_on_stack() function to create an object (i.e., a variable) of this class and get volume of it:

      • Open a main file in the editor window. Before the main function add the code to create cube_on_stack() which will create the object of Cube class and return the volume of this object:
      • double cube_on_stack() {
        	Cube c;
        	c.setLength(2);
        	return c.getVolume();
        }
        int main() {
         // ...
        }
      • Inside the main function ptint out the message and call that function:
      • int main() {
          cout << "Volume of first cube: " << endl;
          cube_on_stack();
        }

        Also, inside the main function create a new cube of length 10 which is going to be in heap memory. Set the values. Print out the information about those objects.

      • Then, inside the main function you should create a new cube of length 10 which is going to be in heap memory:
      •   Cube * pcube = new Cube;
          pcube->setLength(3);
          cout << "Volume of c cube in the heap memory: " << pcube->getVolume() << endl;
        To have an object to be stored in the heap memory you should use star (*) sign. Such objects or instances to the objects can be used together with arrow operator to access their members.

        5) Create a custom destructor to delete an information about the cube.

      • Open header file to declare a class destructor, which will be called to clean up the instance of the class. Add it right after the class constructor to the public protection level:
      • ~Cube();
      • Add the implementation for that destructor inside implementation file:
      • Cube::~Cube(){
        	cout << "Destroyed cube with length " << length_;
        }
      • Return to the main file and add the delete keyword to clean up the memory of the pcube object:
      • // ...
        delete pcube;
        If the object is on the heap, the destructor is only called when that delete keyword is used, to reclaim that memory that the object was using.
      • Run the program and check the output.
      Task 2:

      To do: Create a Student class to output information about the students.

      1) The class should contain the following members:
      — two data members of type string with private access, they are name and surname, and one data member of type intage;
      — and two member functions with public access:
      void set_info(string, string, int); to set the information about the student
      and void get_info(void); to print out the information.

      2) Create a three argument constructor to have the initial values for student.

      3) Inside the main cpp file create void student_on_stack() function to create an object (i.e., a variable) of this class and get volume of it. This object will be in a stack memory. Also, inside the main function create a new student with some info about, which is going to be in a heap memory. Print out the information about those objects.

      4) create a custom destructor to delete an information about student.

      Expected output:

      Task 2:
      info about student1: name: Johnsurname: Ivanovage: 20
      Destructor called for Student Ivanov
      info about student2: name: Petersurname: Panage: 17
      Destructor called for Student Pan
      

      [Solution and Project name: Lesson_9task2, file name L9Task2main.cpp, L9Task2imp.cpp, L9Task2.h]

      Task 3:

      To do: Create a BookShop class to store and output an information about the selling books.

      1) The class should contain the following members:
      data members with private access:
      title_ (a title of a book) of type string;
      author_ (an author of a book) of type string;
      _price (a price of a book) of type double;
      _discount (a discount for a price of a book) of type int.
      data members with public access:
      void getInfo() function to print out the information about the book;
      void set_info(string title, string author, double price, int discount) function to set the information about the book;
      double getTotalPrice() function calculate the price of a book considering the discount (price — (price * discount)).

      2) Create a four argument constructor to have the initial values for books.

      3) Create two objects and print out the information about those objects. Print out the info about the price considering the discount.

      Expected output:

      Task 3:
      // book 1 info:
      Dostoevsky Demons 205 roubles, discount 0.05, total price 194,75 roubles
      // book 2 info:
      Kuprin Duel 125 roubles, discount 0.1, total price 112,5 roubles
      

      [Solution and Project name: Lesson_9task3, file name L9Task3main.cpp, L9Task3imp.cpp, L9Task3.h]

    Lesson # 10. Lists

    Theory

    Forward_list or singly-linked lists

    • Forward lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence.
    • template < class T, class Alloc = allocator > class forward_list;

      T — Type of the elements.
      Aliased as member type forward_list::value_type.
      Alloc — Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent. Aliased as member type forward_list::allocator_type.

    • Forward lists are implemented as singly-linked lists; Singly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept by the association to each element of a link to the next element in the sequence.
    • Forward list keeps internally only a link to the next element.
    • Links lack direct access to the elements by their position; For example, to access the sixth element in a forward_list one has to iterate from the beginning to that position, which takes linear time in the distance between these. They also consume some extra memory to keep the linking information associated to each element
    • Sequence

    • Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.
    • Linked list

    • Each element keeps information on how to locate the next element, allowing constant time insert and erase operations after a specific element (even of entire ranges), but no direct random access.
    • Construct forward_list object
      // forward_list constructors
      #include <iostream>
      #include <forward_list>
       
      int main ()
      {
       
        std::forward_list<int> first;                      // (1) default: empty
        std::forward_list<int> second (3,77);              // (2) fill: 3 seventy-sevens
        std::forward_list<int> third (second.begin(), second.end()); // (3) range initialization
        std::forward_list<int> fourth (third);            // (4) copy constructor
        std::forward_list<int> fifth (std::move(fourth));  // (5) move ctor. (fourth wasted)
        std::forward_list<int> sixth = {3, 52, 25, 90};    // (6) initializer_list constructor
       
        std::cout << "first:" ; for (int& x: first)  std::cout << " " << x; std::cout << '\n';
        std::cout << "second:"; for (int& x: second) std::cout << " " << x; std::cout << '\n';
        std::cout << "third:";  for (int& x: third)  std::cout << " " << x; std::cout << '\n';
        std::cout << "fourth:"; for (int& x: fourth) std::cout << " " << x; std::cout << '\n';
        std::cout << "fifth:";  for (int& x: fifth)  std::cout << " " << x; std::cout << '\n';
        std::cout << "sixth:";  for (int& x: sixth)  std::cout << " " << x; std::cout << '\n';
       
        return 0;
      }

      Expected output:

      first:
      second: 77 77 77
      third: 77 77 77
      fourth:
      fifth: 77 77 77
      sixth: 3 52 25 90

      (1) — default — Constructs an empty container, with no elements.
      (2) — fill constructor — Constructs a container with n elements. Each element is a copy of val (if provided).
      (3) — range constructor — Constructs a container with as many elements as the range [first,last), with each element emplace-constructed from its corresponding element in that range, in the same order.
      (4) — copy constructor (and copying with allocator) — Constructs a container with a copy of each of the elements in fwdlst, in the same order.
      (5) — move constructor (and moving with allocator) — Constructs a container that acquires the elements of fwdlst. If alloc is specified and is different from fwdlst‘s allocator, the elements are moved. Otherwise, no elements are constructed (their ownership is directly transferred). fwdlst is left in an unspecified but valid state.
      (6) — initializer list constructor — Constructs a container with a copy of each of the elements in il, in the same order.

    Forward_list::assign

    Syntax:
    range (1)

    template 
    void assign (InputIterator first, InputIterator last);

    fill (2)

    void assign (size_type n, const value_type& val);

    initializer list (3)

    void assign (initializer_list il);
  • In the range version (1), the new contents are elements constructed from each of the elements in the range between first and last, in the same order.
  • In the fill version (2), the new contents are n elements, each initialized to a copy of val.
  • In the initializer list version (3), the new contents are copies of the values passed as initializer list, in the same order.
  • Any elements held in the container before the call are destroyed and replaced by newly constructed elements (no assignments of elements take place).
    Parameters:
    first, last
    Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
    The function template argument InputIterator shall be an input iterator type that points to elements of a type from which value_type objects can be constructed.
    n
    New size for the container.
    Member type size_type is an unsigned integral type.
    val
    Value to fill the container with. Each of the n elements in the container will be initialized to a copy of this value.
    Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).
    il
    An initializer_list object. The compiler will automatically construct such objects from initializer list declarators. Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).
    Example:

    #include <iostream>
    #include <forward_list>
     
    int main ()
    {
      std::forward_list<int> first;
      std::forward_list<int> second;
     
      first.assign (4,15);                           // 15 15 15 15
     
      second.assign (first.begin(),first.end());     // 15 15 15 15
     
      first.assign ( {77, 2, 16} );                  // 77 2 16
     
      std::cout << "first contains: ";
      for (int& x : first) std::cout << ' ' << x;
      std::cout << '\n';
     
      std::cout << "second contains: ";
      for (int& x : second) std::cout << ' ' << x;
      std::cout << '\n';
     
      return 0;
    }

    Output:

    first contains: 77 2 16
    second contains: 15 15 15 15
    Assignment operator = with forward_list

    Syntax:
    copy (1)

    forward_list& operator= (const forward_list& fwdlst);

    move (2)

    forward_list& operator= (forward_list&& fwdlst);

    initializer list(3)

    forward_list& operator= (initializer_list il);

    Assigns new contents to the container, replacing its current contents.

  • The copy assignment (1) copies all the elements from fwdlst into the container (with fwdlst preserving its contents).
  • The move assignment (2) moves the elements of fwdlst into the container (x is left in an unspecified but valid state).
  • The initializer list assignment (3) copies the elements of il into the container.
  • The container preserves its current allocator, except if the allocator traits indicate fwdlst’s allocator should propagate. This allocator is used (through its traits) to allocate or deallocate if there are changes in storage requirements, and to construct or destroy elements, if needed.
    Parameters:
    fwdlst
    A forward_list object of the same type (i.e., with the same template parameters, T and Alloc).
    il
    An initializer_list object. The compiler will automatically construct such objects from initializer list declarators. Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).
    Example:

    #include <iostream>
    #include <forward_list>
     
    template<class Container>
    Container by_two (const Container& x) {
      Container temp(x); 
      for (auto& x:temp) 
          x*=2;
      return temp;
    }
     
    int main ()
    {
      std::forward_list<int> first (4);      // 4 ints
      std::forward_list<int> second (3,5);   // 3 ints with value 5
     
      first = second;                        // copy assignment
      second = by_two(first);                // move assignment
     
      std::cout << "first: ";
      for (int& x : first) std::cout << ' ' << x;
      std::cout << '\n';
     
      std::cout << "second: ";
      for (int& x : second) std::cout << ' ' << x;
      std::cout << '\n';
     
      return 0;
    }

    In the first assignment, second is an lvalue: the copy assignment function is called.
    In the second assignment, the value returned by by_two(first) is an rvalue: the move assignment function is called.
    Output:

    first: 5 5 5
    second: 10 10 10
    resize () function
    void resize (size_type n);
    void resize (size_type n, const value_type& val);

    Resizes the container to contain n elements.
    If n is smaller than the current number of elements in the container, the content is trimmed to contain only its first n elements, removing those beyonf (and destroying them).
    If n is greater than the current number of elements in the container, the content is expanded by inserting at the end as many elements as needed to reach a size of n elements. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.
    Example

    // resizing forward_list
    #include <iostream>
    #include <forward_list>
     
    int main ()
    {
      std::forward_list<int> mylist = {10, 20, 30, 40, 50};
                                    // 10 20 30 40 50
      mylist.resize(3);             // 10 20 30
      mylist.resize(5,100);         // 10 20 30 100 100
     
      std::cout << "mylist contains:";
      for (int& x: mylist) std::cout << ' ' << x;
      std::cout << '\n';
     
      return 0;
    }
    mylist contains: 10 20 30 100 100
    insert_after function

    Example

    // forward_list::insert_after
    #include <iostream>
    #include <array>
    #include <forward_list>
     
    int main ()
    {
      std::array<int,3> myarray = { 11, 22, 33 };
      std::forward_list<int> mylist;
      std::forward_list<int>::iterator it;
     
      it = mylist.insert_after ( mylist.before_begin(), 10 );          // 10
                                                                       //  ^  <- it
      it = mylist.insert_after ( it, 2, 20 );                          // 10 20 20
                                                                       //        ^
      it = mylist.insert_after ( it, myarray.begin(), myarray.end() ); // 10 20 20 11 22 33
                                                                       //                 ^
      it = mylist.begin();                                             //  ^
      it = mylist.insert_after ( it, {1,2,3} );                        // 10 1 2 3 20 20 11 22 33
                                                                       //        ^
     
      std::cout << "mylist contains:";
      for (int& x: mylist) std::cout << ' ' << x;
      std::cout << '\n';
      return 0;
    }

    Output:

    mylist contains: 10 1 2 3 20 20 11 22 33

    Doubly linked lists

    • Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions.
    • template < class T, class Alloc = allocator > class list;
    • List containers are implemented as doubly-linked lists.
    • Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept internally by the association to each element of a link to the element preceding it and a link to the element following it.
    • Doubly linked list keeps two links per element: one pointing to the next element and one to the preceding one, allowing efficient iteration in both directions, but consuming additional storage per element and with a slight higher time overhead inserting and removing elements.
    • Compared to other base standard sequence containers (array, vector and deque), forward_list perform generally better in inserting, extracting and moving elements in any position within the container, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

    * Information has taken from: http://www.cplusplus.com/reference/forward_list/forward_list/.

    Labs and tasks

    Lesson #14. Arrays (slices, Sorting algorithms, Lists)

    Theory: slices, Sorting algorithms

    Lection in pdf format

    + and * operations for arrays
  • a + b – concatenation of two arrays into result array
  • a * N – concatenation of N copies of a into result array
  • Slices

    • Array slice is a subarray of original array
    • Slices are read-only and cannot be assigned values.
    • The slice type is the same as the array.
    • Slices work with arrays, strings, and lists.
    • It has one of two forms: a[x:y] or a[x:y:step]. Expressions x and y can be omitted.
    • Examples:

      begin
      var a := Arr(0,1,2,3,4,5,6,7,8,9);
      println(a[:4]); // 0 1 2 3
      println(a[4:]); // 4 5 6 7 8 9
      println(a[:a.Length-1]); // 0 1 2 3 4 5 6 7 8
      println(a[:]);      // 0 1 2 3 4 5 6 7 8 9  (copy of a)
      println(a[::2]);   // 0 2 4 6 8
      println(a[1::2]);  // 1 3 5 7 9
      println(a[4:1:-1]); // 4 3 2
      println(a[::-1]);   // 9 8 7 6 5 4 3 2 1 0  (reverse of a)
      end.

    Array sorting algorithms

      Selection sort
    • this algorithm iterates over the array over and over, moving one value to the correct position
    • it selects the smallest unsorted value
    • so, at the next iteration, we will find the minimum in the array after the current element and change it with it, if necessary. Thus, after the i-th iteration, the first i elements will stay in their places.
    • the sorted portion of the array is at the beginning
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      
      procedure SelectionSort(a: array of integer);
      begin
        for var i := 0 to a.High-1 do
        begin
          var (min,imin) := (a[i],i);
          for var j := i + 1 to a.High do
            if a[j] < min then
              (min,imin) := (a[j],j);
          Swap(a[imin],a[i]);
        end;
      end;

      Bubble sort
    • Let’s iterate over the array from left to right.
    • If the current element is greater than the next one, we swap them.
    • We do this until the array is sorted.
    • Note that after the first iteration, the largest element will be at the end of the array, in the correct place.
    • After two iterations, the two largest items will be in the correct place, and so on.
    • 1
      2
      3
      4
      5
      6
      7
      
      procedure BubbleSort(a: array of integer);
      begin
        for var i := 0 to a.High-1 do
          for var j := a.High downto i+1 do
            if a[j] < a[j-1] then
              Swap(a[j], a[j-1]);
      end;
      Insertion sort

      The Insertion sort algorithm iterates through the elements of the array one at a time, and places each new taken element in a suitable place among the previously ordered elements.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      
      procedure SortByInsert(a: array of integer);
      begin
        for var i:=1 to a.High do
        begin
          var x := a[i];
          var j := i - 1;
          while (j >= 0) and (x < a[j]) do
          begin
            a[j+1] := a[j];
            j -= 1;
          end;
          a[j+1] := x;
        end;
      end;
      Standard sort
      Sort(a);
      SortByDescending(a);

    Labs and tasks

    Slices

    Lab 1:
    To do: an array A of size N and an integer K (1 <= K <= N) are given. Print its elements with ordinal numbers (i.e. indexes) that are multiples of K (i.e. devisable by K):

    Ak, A2 * k, A3 * k ...

    Do not use the if statement.

    ✍ Algorithm:

      begin
      var n:=ReadInteger('how many elements');
      var a:=ReadArrReal(n);
      var k:=ReadInteger('K=');
      a[k-1 : : k].Print;
      end.
    {0.2 points} Task 1:

    To do: An array of size N is given. Print its elements in reverse order.

    The resulting example:

    how many elements 
    >> 10
    array:
    25 5 68 42 48 32 100 77 50 47
    result:
    47 50 77 100 32 48 42 68 5 25
    

    [Program name: 14task-01.pas]

    {0.3 points} Task 2:

    To do: An array A of size N is given (N is an even number). Display its elements with even ordinal numbers in ascending order of ordinal numbers:

    а2, а4, а6, ... аn

    Do not use the conditional operator.

    The resulting example:

    >>10
    64 64 21 72 22 82 62 50 30 25
    64 72 82 50 25
    

    [Program name: 14task-02.pas]

    {0.3 points} Task 3:

    To do: An array A of size N is given (N is an odd number). Display its elements with odd ordinal numbers, i.e. in descending order of ordinal numbers:

    аn, аn-2, аn-4, ... а1

    Do not use the conditional operator.

    The resulting example:

    >>9
    array: 89 67 32 43 49 67 93 75 31
    result: 31 93 49 32 89
    

    [Program name: 14task-03.pas]

    Lab 2:

    To do: An array A of size N is given. First, output its elements with even ordinal numbers (in ascending order of ordinal numbers), and then — elements with odd ordinal numbers (also in ascending order of ordinal numbers):

    a2, a4, a6, ... a1, a3, a5 ...

    Do not use conditional operator.

    The resulting example:

    how many elements 
    >> 9
    4 96 8 94 14 80 93 49 6
    96 94 80 49 4 8 14 93 6
    

    ✍ Algorithm:

      begin
      var n:=ReadInteger('how many elements');
      var a:=arrRandomInteger(n);
      a.Println;
      var slice:=a[1::2]+a[::2];
      slice.Print
      end.
    {0.3 points} Task 4:

    To do: An array A of size N is given (N is odd number). First, output its elements with odd indexes (in ascending order of their ordinal numbers), and then — elements with even indexes (in descending order of ordinal numbers). Create a slice to store a concatenation of those two slices:

    a2, a4, a6, ..., a7, a5, a3, a1

    Do not use conditional operator.

    The resulting example:

    >>9
    array: 80 81 71 37 55 78 26 33 53
    result: 81 37 78 33 53 26 55 71 80
    

    [Program name: 14task-04.pas]

    Lab 3:

    To do: An array of size N and integers K and L (1 <= K <= L <= N) are given. Find the arithmetic mean (average) of the elements of an array with numbers from K to L inclusive.

    The resulting example:

    >> 10
    59 87 0 37 57 69 79 19 100 5
    K=  >> 2
    L=  >> 4
    41.3333333333333 
    

    ✍ Algorithm:

      begin
      var n:=ReadInteger;
      var a:=arrrandominteger(n);
      a.Println;
      var k:=ReadInteger('K= ');
      var l:=ReadInteger('L= ');
      var slice:=a[k-1:l].Average;
      slice.Print;
      end.
    Lab 4:

    To do: An array of size N is given. Find the minimum element of its even-numbered elements:

    a2, a4, a6, .....

    The resulting example:

    >> 10
    96 79 71 87 61 21 51 74 67 89
    slice: [79,87,21,74,89] 
    21 
    

    ✍ Algorithm:

      begin
      var n:=ReadInteger;
      var a:=arrRandomInteger(n);
      a.Println;
      println('slice: ',a[1::2]);
      print(a[1::2].min);
      end.
    {0.3 points} Task 5:

    To do: An array of size N and integers K and L (1 <= K <= L <= N) are given. Calculate the sum of array elements except for elements with indexes from K to L inclusive.

    Note: you should use here the sum method:

    print(a[?:?:?].sum);
    // or
    print(slice.sum);

    The resulting example:

    >> 10
    35 26 82 63 54 47 37 95 26 88
    K =  >> 4
    L =  >> 8
    257 
    

    [Program name: 14task-05.pas]

    {0.4 points} Task 6:

    To do: An array of size N (N is even) is given. Change the first half in it with the second (assume that the length of the array is an even number). Do not change the order of the elements in the halves.

    The resulting example:

    >> 10
    68 57 63 91 52 56 78 51 33 83
    result: [56,78,51,33,83,68,57,63,91,52] 
    

    [Program name: 14task-06.pas]

    Lab 5: Insertion and deletion in an array

    To do 1: An array of N integers is given. It’s necessary to insert an element x on k-th index, k<=N.

    To do 2: An array of N integers is given. It’s necessary to delete an element with index k, k<N.

    The resulting example:

    // to do 1:
    array: [5, 12, 1, 3, 11, 19]
    enter a number to insert 2
    enter an order number 3
    result:  [5,12,1,2,3,11,19] 
    
    // to do 2:
    array: [5, 12, 1, 3, 11, 19]
    enter an order number 
    >> 2
    result: [5,12,3,11,19] 
    

    ✍ Algorithm:

      // to do 1:
      begin
        var a := arr(5, 12, 1, 3, 11, 19);
        var x := ReadInteger ('enter a number to insert');
        var k := ReadInteger ('enter an order number');
        a := a[:k] + Arr(x) + a[k:]; 
        print('result: ', a)
      end.
      // to do 2:
      begin
        var a := arr(5, 12, 1, 3, 11, 19);
        var k := ReadInteger ('enter an order number');
        a := a[:k] + a[k+1:]; 
        print('result: ', a)
      end.
    {0.4 points} Task 7:

    To do: An array of integers is given. Find maximum element of the array and delete it. You should use slice to implement the task.

    Note: To find an index of the maximum element it is better to use a.IndexMax method.

    The resulting example:

    array:  [66,46,26,64,73,62,37,57,46,9] 
    result:  [66,46,26,64,62,37,57,46,9]  
    

    [Program name: 14task-07.pas]

    {0.4 points} Task 8:

    To do: An array of integers and N number are given (N is entered). Find minimum element of the array and insert N number before this element. You should use slice to implement the task.

    Note: To find an index of the maximum element it is better to use a.IndexMin method.

    The resulting example:

    array:  [20,3,18,33,93,58,30,56,15,3] 
    enter N number please:  
    >> 20
    result:  [20,20,3,18,33,93,58,30,56,15,3]  
    

    [Program name: 14task-08.pas]

    Array sorting algorithms

    Lab 6:

    To do: calculate a time to execute the algorithm of Bubble sort

    ✍ Algorithm:

      procedure MySort(a: array of integer);
        begin
      // bubble sort
          for var i := 1 to arr.High - 1 do
          for var j := 0 to arr.High - i do
            if arr[j] > arr[j + 1] then 
              Swap(arr[j], arr[j + 1]);
      end;
      begin
      var a:=arrRandomInteger(20000);
      // note the time
      var t:=System.DateTime.Now;
      // run the algorithm
      MySort(a);
      // note the time of algorithm end
      var t1:=System.DateTime.Now;
      println('The time to execute the algorithm: ',t1-t);
      //t:=System.DateTime.Now;
      end.
    {0.5 points} Task 9:

    To do: Calculate a time to execute the algorithm of Selection sort and Insertion sort. Compare the time and output the result

    Note: You should create two procedures with sorting algorithms (copy those algorithms from the Theory materials). And you’ll need to note a current time four times (before starting each algorithm and before finishing it).

    The resulting example:

    The time to execute the Insertion algorithm:  00:00:00.5804751 
    The time to execute the selection algorithm:  00:00:00.9035547 
    

    [Program name: 14task-09.pas]

    Merging of two sorted arrays

    Lab 7:

    To do: Two sorted arrays a and b are given. Merge them into third sorted array.

    Resulting example:

    array 1: [1,3,11,19] 
    array 2: [1,13,21] 
    result: [1,1,3,11,13,19,21] 
    

    ✍ Algorithm:

      function Merge(a, b: array of integer; n, m: integer): array of real;
      begin
        Assert((0 < n) and (n < a.Length));
        Assert((0 < m) and (m < b.Length));
        a[n] := integer.MaxValue; // barrier
        b[m] := integer.MaxValue; // barrier
        SetLength(Result, m + n);
        var (ia, ib) := (0, 0);
        for var:= 0 to n + m - 1 do
          if a[ia] < b[ib] then
          begin
            Result[] := a[ia]; 
            ia += 1;
          end
          else
          begin
            Result[] := b[ib]; 
            ib += 1;
          end;
      end;
      begin
        var a := arr(1, 3, 11, 19);
        var b := arr(1, 13, 21);
        println('array 1:',a);
        println('array 2:',b);
        setLength(a, 5); // for extra element
        setLength(b, 4); // for extra element
        print('result: ', Merge(a, b, 4, 3)) // [1,1,3,11,13,19,21] 
      end.
    {0.5 points} Task 10:

    To do: Calculate a time to execute the algorithm of the lab before this task (with the function to Merge two arrays). Compare the time with a time, needed to complete the merging using standard sort algorithm:

    var c:=a+b;
    sort(c);

    The resulting example:

    array 1: [1,3,11,19] 
    array 2: [1,13,21] 
    result array c: [1,1,3,11,13,19,21] 
    the time with standard sort method 00:00:00.0009962 
    result:  [1,1,3,11,13,19,21] 
    the time with function to merge 00:00:00.0009984  
    

    [Program name: 14task-10.pas]

    Lab 8:

    To do: An array of integers and n number is given (n is entered). You should output the index of the n number in the array, or ‘there is no n’ message if n is not in the array.

    The resulting example:

    array:  [3,3,24,41,57,59,84,88,89,95] 
    enter n to find it:  
    >> 41
    index of n is:  3

    ✍ Algorithm:

      begin
        var a := arrrandominteger(10);
        sort(a);
        println('array: ', a);
        var n := readinteger('enter n to find it: ');
        var index := a.BinarySearch(n);
        if (n > 0) then
          print('index of n is: ', index)
        else
         print('there is no n ')
      end.
    {0.5 points} Task 11:

    To do: An array of integers and n number are given (n is entered). You should use a.BinarySearch(n) standard method to delete that n from the array. Also, you should use slices to do it.

    Note: to use BinarySearch() method you need the array to be sorted first. So, use Sort() method before searching.

    The resulting example:

    array:  [6,71,85,33,41,75,75,84,38,57] 
    sorted array:  [6,33,38,41,57,71,75,75,84,85] 
    enter n to delete it:  
    >> 38
    result:  [6,33,41,57,71,75,75,84,85]  
    

    [Program name: 14task-11.pas]

    Lists

    Lab 9:

    To do: An array of N integers is given. Insert all even elements of the array into L1, and all odd elements into L2.

    The resulting example:

    array: [17,25,8,17,21,9,19,22,19,24] 
    L1: 8 22 24
    L2: 17 25 17 21 9 19 19
    

    ✍ Algorithm:

      begin
        var a := arrrandominteger(10, 5, 25);
        println(a);
        var L1 := new List<integer>;
        var L2 := new List<integer>;
       
        foreach var x in a do
          if x.IsEven then
            L1 += x
          else L2 += x;
        L1.println;
        L2.println;
      end.
    {0.2 points} Task 12:

    To do: An array of integers is given (fill it with random generated numbers in the range [-5,20]). Insert all positive elements of the array into L1 list, and all negative elements into L2 list.

    The resulting example:

    array:  [17,16,15,5,-2,2,-3,-5,16,-5] 
    L1: 17 16 15 5 2 16
    L2: -2 -3 -5 -5
    

    [Program name: 14task-12.pas]

    {0.4 points} Task 13:

    To do: A list of integers is given. Find maximum element of the list and delete it. You should use L.RemoveAt(k); standard list method.

    Note: To find an index of the maximum element it is better to use L.IndexMax method.

    The resulting example:

    List: 0 93 71 88 99 44 50 36 72 1
    result:  [0,93,71,88,44,50,36,72,1]   
    

    [Program name: 14task-13.pas]

    {0.4 points} Task 14:

    To do: A list of integers and N number are given (N is entered). Find minimum element of the list and insert N number before this element. You should use L.Insert(ind,n); standard method.

    Note: To find an index of the minimum element it is better to use L.IndexMin method.

    The resulting example:

    76 45 84 47 85 27 12 74 21 47
    enter n:  
    >> 20
    result:  [76,45,84,47,85,27,20,12,74,21,47]   
    

    [Program name: 14task-14.pas]

    Lesson #13. Arrays (continuation)

    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.
  • In the case of more than one parameter, params should be the last one in the list
  • SetLength procedure

  • Standard SetLength function allocates the necessary memory to contain the array elements
  • 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!

  • If we now reassign the values of the elements of the array 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]
  • To avoid this situation, you need to create array 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)
    • Reverses elements in a array

    • a.IndexOf(x)
    • Finds the position (index) of x in a array

    • a.LastIndexOf(x)
    • Finds the last position (index) of x in a array

    • a.Contains(x)
    • 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.

    The resulting example:

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

    The resulting example:

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

    The resulting example:

    [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).

    The resulting example:

    [1,2,2,4,8,32,256,8192] 
    

    [Program name: 13task-04.pas]

    Params keyword. SetLength procedure

    Lab 1: Params keyword

    To do: Create a function to add any number of integers.

    ✍ Algorithm:

      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 keyword is used to make it able to pass a varying number of parameters.
    • In the case of more than one parameter, params should be the last one in the list
    {0.4 points} Task 5:

    To do: Create a PrintArr procedure that prints out any number of parameters, delimited by a semicolon (there must be two parameters: array and delim). Use the default value for delim = '; '. 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.

    The resulting example:

    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: An integer array is given. 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 number of such elements.

    Note: Inside the main program, call the procedure several times with different number of parameters. Generate random array of different number of elements:

    var a := new integer[10];
    a := arrRandomInteger(5,-5,5);
    EvenDouble(a);
    // some more procedure calls

    The resulting example:

    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]

    Lab 2: SetLength

    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 neighbors. The first and last element should not be considered as local minima.

    The resulting example:

    array a [6,-2,3,-4,6] 
    result of function, array b  [-2,-4] 
    

    [Program name: 13task-07.pas]


    Standard functions and procedures and Transformation of an array elements

    Lab 3:

    To do: Create the function to search x in the array. The function returns -1 if it is not found and index of the element otherwise.

    ✍ Algorithm:

      function IndexOfW<T>(a: array of T; x: T): integer;
      begin
        var n := a.Length;
        var i := 0;
        while (i < n) and (a[i] <> x) do
          i += 1;
        Result := i = n ? -1 : i;
      end;
       
      begin
        var a := new integer[10];
        a := arrRandomInteger(10, 0, 10);
        print(a);
        print(indexOfW(a, 2))
      end.
    Lab 4:

    To do: Print out how many there odd elements are in the array.

    The resulting example:

    [18,10,91,47,35] 
    3 
    

    ✍ Algorithm:

      begin
        var a := new integer[5];
        a := arrRandomInteger(5); // [18,10,91,47,35] 
        print(a);
        print(a.Count(a->odd(a))) // 3 
      end.
    Lab 5:

    To do: Transform the array elements using the rule: if element is even then substruct it by 1, if it is odd then add 1 to it.

    ✍ Algorithm:

        begin
          var a := new integer[5];
          a := arrRandomInteger(5); // [4,36,93,36,29] 
          a.Transform(a,a -> a mod 2 = 0 ? a-1 : a + 1);
          print(a) // [3,35,94,35,30] 
        end.
    {0.5 points} Task 8:

    To do: Create a randomly generated array. Use the standard functions and procedures to implement the following goals. Print out:

    1. reversed array (Reverse())
    2. the position of x number (x is entered)
    3. the last position of x number (x is entered) (LastIndexOf())
    4. true if x exists in the array, false if it doesn’t (Contains())
    5. index of the first even element (IndexOf())
    6. the number of positive elements in the array (Count()). You should create your own function that returns true if the checked number is positive.
    7. maximum element and it’s index (Max(), IndexMax()).
    8. transformed array with a rule: if element is positive then square it, if it is negative then set it to 0 (Transform()).

    The resulting example:

    
    

    [Program name: 13task-08.pas]

    Lab 6, Loop over some indices:

    To do: Create an array of integer elements. Pint out the array values in the interval [10,19] and increase by one it’s positive values with even indexes.

    The resulting example:

    array: [41,90,100,12,16,7,69,23,59,37] 
    array values in the interval [10,19]: 12 16 
    

    ✍ Algorithm:

      begin
        var a:=arrRandomInteger(10);
        print(a);
        // values in the interval [10,19]
        foreach var i in a.Indices(x -> x.InRange(10,20)) do 
          print(a[i]);
       
        // positive values with even indexes
        foreach var i in a.Indices((x,i) -> (i mod 2 = 0) and (x > 0)) do 
          a[i] += 1;
      end.
    {0.4 points} Task 9:

    To do: Create a randomly generated array. Use loop over some indices to print out:

    • The values of the array with odd indexes.
    • The second half of the array elements.

    The resulting example:

    array: [8,41,56,82,8,93,44,70,6,54] 
    The second half of the array elements: 8 41 56 82 8 
    The values of the array with odd indexes: 41 82 93 70 54 
    

    [Program name: 13task-09.pas]

    Lab 7:

    To do: Create the procedure to shift circularly the elements to the left

    ✍ Algorithm:

      procedure CircularShiftLeft<T>(a: array of T);
      begin
        var v := a[0];
        for var i:=0 to a.Length-2 do
          a[i] := a[i+1];
        a[a.Length-1] := v;
      end;
       
      begin
        var a := new integer[5];
        a := arrRandomInteger(5); // [56,28,33,57,25] 
        CircularShiftLeft(a);
        print(a)// [28,33,57,25,56] 
      end.
    Lab 8:

    To do: Create the procedure to shift circularly the elements to the right

    ✍ Algorithm:

      procedure CircularShiftRight<T>(a: array of T);
      begin
        var v := a[a.Length-1];
        for var i:=a.Length-1 downto 1 do
          a[i] := a[i-1];
        a[0] := v;  
      end;
       
      begin
        var a := new integer[5];
        a := arrRandomInteger(5); // [25,23,74,17,31] 
        CircularShiftRight(a);
        print(a)// [31,25,23,74,17] 
      end.
    {0.3 points} Task 10:

    To do: Create a randomly generated array. At first, transform its elements using the rule: a-1, and after, reverse it’s elements and after, make a circular shift it’s elements to the right and to the left.

    The resulting example:

    array:  [93,25,52,74,76,40,54,13,29,65] 
    transformed:  [92,24,51,73,75,39,53,12,28,64] 
    Shift left:  [24,51,73,75,39,53,12,28,64,92] 
    Shift right:  [92,24,51,73,75,39,53,12,28,64] 
    

    [Program name: 13task-10.pas]

    Вставить формулу как
    Блок
    Строка
    Дополнительные настройки
    Цвет формулы
    Цвет текста
    #333333
    Используйте LaTeX для набора формулы
    Предпросмотр
    \({}\)
    Формула не набрана
    Вставить