Содержание:
Theory: Enumeration type
Lection in pdf format
- An enumeration type, or enum, is a structure that enables you to create a variable with a fixed set of possible values. The most common example is to use an enum to define the day of the week. There are only seven possible values for days of the week, and you can be reasonably certain that these values will never change.
- To create an enum, you declare it in your code before the main program with the following syntax, which demonstrates creating an enum called
DayOfWeek
, that contains short names of the days of the week: - We can use
for
loop andcase
statement with an Enumeration type: - It’s more correct to use the variable of the created Enumeration type:
- An enumerated constant can be accessed directly by name, or you can use the
typeName.value
notation. - Enumerated values can be compared (
<
) : - For values of an enumerated type you can use the functions
Ord
,Pred
andSucc
, and the proceduresInc
andDec
. The functionOrd
returns the ordinal value in the list of constants corresponding enumerated type, the numbering starts at the zero. - An instance function
ToString
is defined for an enumerated type, which returns a string representation of an enumerated variable. - Array is a set of elements of the same type sequentially located in memory. Each element has its own index.
- We will use so called dynamic arrays. This means that they allocate memory dynamically - during program execution
- The memory allocation and the size of such an array are set already during the program:
- To return memory allocated by array we can assign a special nil value to an array variable:
- To iterate through the array elements by their indexes we can use
for
loop: - For read-only access (without changing the values) we can use
foreach
loop:
Creating and Using Enums
type DayOfWeek = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); begin //... end. |
for var d:= Mon to Fri do case d of Mon: Print('Monday'); Tue: Print('Tuesday'); Wed: Print('Wednesday'); Thu: Print('Thursday'); Fri: Print('Friday'); end; |
type Season = (Spring, Summer, Autumn, Winter); begin var d: Season; // the variable of enum type d := Season.Spring; // Set an enum variable using the typeName.value notation print(d); // Spring d:=Autumn; // Set an enum variable by name if d = Season.Summer then // false print(d) end. |
DayOfWeek.Wed < DayOfWeek.Sat |
type Season = (Winter,Spring,Summer,Autumn); var s: Season; begin s := Summer; writeln(s.ToString); // Summer writeln(s); // Summer end. |
Theory: Arrays
begin var a: array of integer; a := new integer[3]; a[0] := 5; a[1] := 2; a[2] := 3; end. |
or inside the same line of code:
begin var a: array of integer; a := new integer[3](5,2,3); print(a) end. |
1.
var a: array of integer; var n:=readInteger; a:=new integer[n]; |
2.
var a: array of integer; a:=new integer[readInteger]; |
3.
var a: array of integer; var n:=readInteger; SetLength(a,n); // set the size |
var a := nil; |
for var i:=0 to a.Length-1 do a[i] += 1; |
foreach var x in a do Print(x) |
Labs and tasks
Enumerations
{0.5 points}
Task 1:
To do: Request user to input a number - a mark ('1'
, '2'
, '3'
, '4'
, '5'
). Check the inputted number to print out the explanation of the mark (very_bad - '1', bad — '2', satisfactory - '3', good - '4', excellent - '5'). Provide also an answer for incorrect input. You should use an enumeration type.
Note 1: entered mark must be stored at the variable of char
type. Also, you should use a variable of enum type to output the explanation.
Note 2: it is better to use case statement to check the entered value.
Expected output:
enter your mark, please (1..5) >>>2 The explanation for 2 is bad --- enter your mark, please (1..5) >>>6 The explanation for 6 is noSuchMark
[Program name: 12task-01.pas
]
To do: Create a generic IsitDigit (D)
function that returns true
if entered integer D
is a digit (a one-digit number, that is, D
is in the range [0-9]). In the main program, output the value of this function for N
(N ≥ 0) given numbers.
Resulting example:
enter how many numbers 2 enter number > 0 45 False enter number > 0 8 True
✍ Algorithm:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function IsitDigit<T>(d: T): boolean; begin var ch := [0..9]; if d in ch then result := true else result := false; end; begin var k := readInteger('enter how many numbers'); assert(k > 0, 'entered number must be > 0'); loop k do begin var n := readInteger('enter number > 0'); assert(n > 0, 'entered number must be > 0'); print(IsitDigit(n)); end; end. |
Arrays
{0.3 points}
Task 2:
To do: Create two arrays. One is with integer elements, the other is for working with real numbers. Initialize each array with three values. Create procedure to display array elements. Call this procedure for both arrays.
Note 1: To make the procedure generic, <T>
is required (<T>
means any type).
procedure printArray<T>(a: array of T); |
Expected output:
first array: 5 2 3 second array: 5.1 1.2 3
[Program name: 12task-02.pas
]
To do: search for a specified value in an array (display the index of an element).
✍ Algorithm:
-
1. Without using generic function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function IndexOf(a:array of integer;x:integer):integer; begin result:=-1; for var i:=0 to High(a) do if a[i]=x then begin result:=i; break end end; begin var a:=Arr(1,2,3,4,5); print(IndexOf(a,5)) end. |
2. Using generic function:
1 2 3 4 5 6 7 8 | function IndexOf<T>(a:array of T;x:T):integer; begin // the same as in the previous example end; begin var a:=Arr(1,2,3,4,5); print(IndexOf(a,5)) end. |
When calling a generic function, compilation will be in two stages:
1. Auto deduction of type T
, comparison with real numbers, etc. numbers are integers, then T
is defined as an integer.
2. The body of the function is taken and replaced by T
with integer (instantiation of a function with a specific type).
{0.3 points}
Task 3:
To do: Create an array of 10 randomly generated integer elements. Create a procedure to output the elements of the array with even indexes.
Note: The signature of a procedure must be as follows:
procedure printEven(a:array of integer); |
Expected output:
Array: 2 6 4 1 8 6 5 3 7 8 Elements with even indexes: 2 4 8 5 7
[Program name: 12task-03.pas
]
{0.4 points}
Task 4:
To do: Create a procedure to generate n
elements of array, the values should be generated in the interval[minBound
,maxBound
]. The array must be output parameter. You should call the procedure in the main program twice with different arrays, after each calling print the array.
Note 1: The random function must be as follows (minBound
and maxBound
are entered numbers, minBound < maxBound ):
a[i]:=random(minBound, maxBound); |
Note 2: The signature of a procedure must be as follows:
procedure generateArray(n: integer;var a:array of integer;minBound,maxBound:integer); |
Note 3: Don't forget to make input validation (n
>0), use assert()
function.
Expected output:
Array 1: [-2,3,5,-3,-1,-1,0,1,-3,2] Array 2: [8,4,9,8,9]
[Program name: 12task-04.pas
]
{0.3 points}
Task 5:
To do: Create a procedure to find minimum and maximum of 10 real numbers of an array (10 element of the array should be generated and printed inside the main protgram). To search the minimum and maximum foreach
loop must be used.
Note 1: The signature of a procedure must be as follows:
procedure printMinMax (a: array of real); |
Note 2: To display the reals it is better to use writelnFormat
function, with specified number of digits after floating point, e.g.:
writeFormat('{0:f2}', a); // f2 means 2 digits after floating point |
Expected output:
array: 3.05 3.47 -0.53 3.84 -0.51 2.67 4.07 4.48 2.21 3.64 min = -0.53, max = 4.48
[Program name: 12task-05.pas
]
{0.2 points}
Task 6_1:
To do: Create a ArraySum
function that returns the sum of the elements of a dynamic array. Pay attention to checking the input array.
Note: Demonstrate the use of ArraySum
function in the main program: create an array, print its length and sum of its elements.
Expected output:
array: -94 -4 -30 -79 15 42 -83 -44 -74 0 sum = -351
[Program name: 12task-06.pas
]
{0.3 points}
Task 6_2:
To do: Create a ArraySum
function that returns the sum of the elements of a dynamic array. Pay attention to checking the input array.
Note 1: Create the TestArraySum
procedure to test the correctness of the ArraySum
fuction. Add some tests there. Make sure all tests from TestArraySum
works well.
function ArraySum(a: array of integer): integer; begin // TODO calculate the sum end; procedure TestArraySum; begin // array of one element: var a := new integer[1](0); Assert(ArraySum(a) = 0, 'arr-sum: [0] must be 0'); a := new integer[1](-5); Assert(ArraySum(a) = -5, 'arr-sum: [-5] must be -5'); // array of some elements: // TODO: type your tests here end; // --- main program --- begin TestArraySum; // all the rest code end. |
Note 2: Demonstrate the use of ArraySum
function in the main program: create an array, print its length and sum of its elements.
Expected output:
array: -94 -4 -30 -79 15 42 -83 -44 -74 0 sum = -351
[Program name: 12task-06_2.pas
]
Multi-file layout
{0.5 points}
Task 7:
To do: Create a multi-file layout for the task #2 of current lesson.
[Program name: 12task-07.pas
]
✍ Algorithm:
DynArrs.pas
. Add the code to give a name of the unit (unit DynArrs;
) and add two sections - the interface section and implementation.
unit DynArrs; // ======================================== interface section interface // ======================================== implementation section implementation end.
2. Find your solution of the task #2. Copy the header of the printArray
procedure and paste it inside the interface section of your unit-file. Add the summary before the pasted code.
...
interface
/// <summary>
/// prints array of any type
/// </summary>
procedure printArray<T>(a: array of T);
...
3. Copy the body of the printArray
procedure and paste it inside the implementation section of your unit-file. Add the comments before the pasted code.
... implementation // prints array of any type procedure printArray<T>(a: array of T); begin for var i := 0 to a.Length - 1 do begin print(a[i]); end; println end; ...
4.Create a new main file with a name 12task-07.pas
. Add the code to include the unit. In the main program create an array and call the printArray
procedure.
uses DynArrs;
begin
var a := new integer[3](1, 2, 3);
printArray(a);
end.
{0.3 points}
Task 8_1:
To do: Create a PrintArrDelim
procedure to display the array elements, separating them with some simbol (delim
is a parameter of string
type).
Note: ThePrintArrDelim
procedure must accept an array and a separator string. Use the default value for delim
which is '; '
.
procedure PrintArrDelim(a: array of integer; delim: string := '; '); |
Expected output:
The array with , as a separator: 1, 2, 3, The array with ~ as a separator: 1~ 2~ 3~
[Program name: 12task-08.pas
]
{0.5 points}
Task 8_2:
To do: Create a multi-file layout for the following task: create a PrintArrDelim
procedure to display the array elements, separating them with some simbol (delim
is a parameter of string
type). To do the task, open the DynArrs.pas
unit-file (of task #7) and add there all needed code. In the main file call the procedure several times with different separators.
Note 1. ThePrintArrDelim
procedure must accept an array and a separator string. Use the default value for delim
which is '; '
.
procedure PrintArrDelim(a: array of integer; delim: string := '; '); |
Note 2. When you add the implementation code, you don't need to have a default value for the delim:
procedure PrintArrDelim(a: array of integer; delim: string); |
Expected output:
1, 2, 3, 1~ 2~ 3~
[Program name: 12task-08_2.pas
]
{0.5 points}
Task 9:
To do: Create a multi-file layout for the Task #4 of the current lesson. To do the task, open the DynArrs.pas
unit-file and add there all needed code (the code of generateArray
procedure).
Expected output:
Array 1: [-2,3,5,-3,-1,-1,0,1,-3,2] Array 2: [8,4,9,8,9]
[Program name: 12task-09.pas
]
{0.5 point}
Task 10_1:
To do: Print the number of positive elements of given array. You need to write two different subroutines for this problem:
1) Create a positiveArr
function with the array as a parameter, returning a number of positives.
2) Create a printPositiveArr
procedure with two parameters: the input parameter that is an array of integers and output counter of the positives.
Expected output:
array: 1 -5 2 -9 8 result with function: 3 result with procedure: 3
[Program name: 12task-10.pas
]
{1.0 point}
Task 10_2:
To do: Create a multi-file layout for the following task: Print the number of positive numbers of the given array. You need to write two different solutions to this problem:
1) positiveArr
function with the array as a parameter, returning a number of positives.
2) printPositiveArr
procedure with two parameters: the input parameter that is an array of integers and output counter of the positives.
To do the task, open the DynArrs.pas
unit-file and add there all needed code. In the main file create an array and call the procedure and function.
Note. Create test procedures for positiveArr
function. Add the code to the unit-file.
Expected output:
array: 1 -5 2 -9 8 result with function: 3 result with procedure: 3
[Program name: 12task-10_2.pas
]