# Static and dynamic arrays

· Statistical (traditional) arrays , those arrays are called when declaring which, in an explicit or indirect form, specific boundaries for changing each index are indicated.

Example:

Const

A=3

K=2

type

Mat = array[1..a,1..k]

Var

x: Array[3…10]

Y: mat

Dynamic arrays do not contain indications about the boundaries of changing indices

Example:

SetLenght(da1,100) – this call is equivalent to static

type description x: Array [0…99] of integer

Question number 24.

The length and size of the array.

The length and size of an array is the amount of RAM in bytes occupied by the elements of the array. To determine this characteristic, one usually resorts to the SizeOf function.
Each array element is 8 bytes. Since 13 elements are declared in the array, the compiler will allocate 8×13=104 bytes to store their values.
The size of a one-dimensional static array is the same as the number of its elements. And to determine this characteristic, they usually resort to the Length function: Length (sa1) = 13
Pascal provides two more functions – Low and High for determining the minimum and maximum values of the index of a one-dimensional and static array. For example:
Low (sa1) = 3
High (sa1) = 15

The result of the SizeOf(da1) function is sometimes 4. In fact, this is the amount of memory occupied by the da1 pointer to the corresponding data. The Length function returns an actual value equal to the current number of elements of the dynamic array, and while the SetLength procedure has not yet been called, this characteristic is 0.
For a two-dimensional array q of size nxm, the compiler gets n pointers. The first one has for q0 the same as the name of the array. It is a pointer to the beginning of the array and at the same time to the first row of the array. If the minimum value of the first index is equal to k, then the pointer q[k] also “looks” at the first row of the array (i.e. q and q[k] are two names of the same pointer). The next pointer named q[k+1] “looks” at the second row of the array, and so on.

Question number 25.

When solving complex problems, it is advisable to break them down into simpler subtasks. In the Pascal language, tools are provided that allow you to design an auxiliary algorithm as a subroutine. The use of subprograms makes the main program more visual, understandable, shorter and more efficient. Registration of procedures. PROCEDURE name (formal parameters); declaration section BEGIN statement section END; Procedure called by name: name (actual parameters);

Question number 26.

Algorithm

Pascal programming algorithm is a sequence of Pascal operators, with the help of which we came to solve the problem. She looks like this:

Program … ; { program title }

uses … ; { Connecting modules } – if any

label … ; { Label declaration section } – if any

Const … ; { Constant declaration section } – if any

type … ; { New type declaration section } – if any

Var … ; { Variable declaration section } – if any

Procedure … Procedure ; { Description of own procedures } – if any

function … ; { Description of its functions } – if any

Begin { start of main program }

…;

{ Operators }

…;

end.

The obligatory part is only the body of the program, which begins with the word begin and ends with the word end with a dot. Operators in Pascal are separated by semicolons. The program title is an optional but desirable element and consists of the reserved word program and an identifier – the name of the program, followed by a semicolon. The order of declarations and descriptions is not regulated.

Question number 27 .

Follow-up – action follows each other

Question number 28.

Choice

Pascal’s choice statement allows you to select one of several possible continuations of a program. The choice parameter is the choice key, which is an expression of any ordinal type.

The structure of the select statement in Pascal is as follows:

Case of

[else ] end

Here case, of, else, end are reserved words (case, from, otherwise, end);

· – expression of ordinal type;

· – one or more constructions of the form:

o : ;

– a constant of the same type as the expression

o ;

– arbitrary Pascal operators.

The Pascal selection operator works as follows. First, the value of the expression is evaluated, and then in the sequence, a constant equal to the calculated value is found. The statement that follows the found constant is executed, after which the select statement exits. If no constant is found in the select list that matches the computed value of the select key, control is transferred to the statements following the else word. The else part can be omitted, then if the required constant is not in the selection list, no action will be performed, and the selection operator will simply complete its work.

For example, let’s write a program that, by the number of the day of the week, displays its name on the screen:

It should be remembered that all constants from the selection list must be different.

Any of the select list statements can be preceded by more than one select constant, separated by commas. For example, the following program, when entering one of the characters ‘ y’ or ‘ Y’, will display “Yes”, and when entering ‘ n’ or ‘ N’ – the word “No”.

Question number 29.

Cycle

Cycle is the organization of repeated execution of a set of instructions (operations). Also, a cycle can be called any repeatedly executed sequence of operations, organized in any way.

This sequence is just the body of any cycle.

Each time any loop is executed, its variables are initialized at the beginning, then the exit condition is checked, the body of the loop is executed, and then the variable is updated on each new round of loop execution.

What are the cycles? Now we will consider all possible variants of cycles in order.

1) Unconditional (endless) cycles.

Such loops are created using constructs designed to create regular (or conditional) loops. To ensure infinite repetition, the condition check in such a loop is either absent or replaced by a constant value (while true do …).

2) Loop with precondition.

A loop with a precondition is a loop that is executed while a certain condition is true. This condition is checked before the loop body is executed, so the body may not be executed at all. Such a cycle is implemented by the while statement. In Pascal, a loop with a precondition looks like this:

while do

begin

end;

3) Loop with postcondition.

A loop with a postcondition is a loop in which the condition is checked after the execution of the loop body. It follows that the body is always executed at least once. The loop implements the repeat..until operator.

repeat

until

That is, the loop ends when its condition is met.

4) A cycle with an exit from the middle.

A cycle with an exit from the middle is formed using three constructions: the beginning of the cycle, the end of the cycle, and the command to exit the cycle. Inside the body, there must be an exit command from the loop, upon execution of which the loop ends and control is transferred to the operator following the loop end construct. For the loop to be executed more than once, the exit command must not be called unconditionally, but only when the loop exit condition is met.

But in the pascal language, such constructions are not provided, so the goto operator is used to exit the middle of the loop. It allows you to change the order in which commands are executed.

Goto n;

1:write(x);

n:write(y);

Where n is a label that determines which part of the program should go under certain conditions.

5) Loop with counter.

A loop with a counter is a loop in which some variable changes its value from a given initial value to a final value with some step, and for each value of this variable, the body of the loop is executed once. Implemented by the for statement, which specifies the counter (the so-called “loop variable”), the required number of passes (or the limit value of the counter), and possibly the step by which the counter changes.

n := 100;

for i := 1

to n do

begin

… loop body

end;

A loop with a counter can always be written as a conditional loop, before the beginning of which the counter is assigned an initial value, and the exit condition is that the counter reaches the final value.

That is, in the for construct, first, an arbitrary sentence of loop initialization is written, then a continuation condition, and, finally, some operation performed after each loop body.

6) Nested loops.

Sometimes it is necessary to place one loop within another. Such a cycle is called nested. Inside a nested loop, another loop can be nested in turn, forming the next level of nesting, and so on. The number of nesting levels, as a rule, is not limited.

begin

If K then D

Else

Begin

If R then M

Else P;

end;

end;

Here, another conditional statement is nested inside the conditional statement, which creates a nested loop.

With the above cycles, you can perform certain operations to improve their functionality.

There are situations when it is necessary to exit the loop early for certain reasons (an error in the loop body, etc.). To solve such problems, there is a break command, and its action is similar to the action of the unconditional jump (goto) command on the command immediately following the loop in which this command is located. This statement interrupts the work of the loop in which it is directly located.

If S then V

Else

break;

Question number 30.

FP Graphic Tools

Almost repeats the set of graphics tools Turbo Pascal, Borland Pascal. An additional feature of FP is the allocation of two windows by the application. The interaction between the user and the application is implemented in the main window (input / output for operations: read / readln, write / writeln, readkey / keypressed) In the additional window, figures are built and explanatory records are displayed using BCI procedures.

Question number 31.

coordinate system

The working field of the graphics window has a coordinate system whose origin is in the upper left corner. The x-axis is to the right and the y-axis is down.

The units of measurement are pixels.

Question number 32.

Graphic cursor.

The working field of the graphics window, located under the title of the window, is provided with a coordinate system, the origin of which is in the upper left corner. In this case, the y-axis is directed down, and the x-axis is directed to the right. Screen pixels are taken as units of measurement. To store the color code of each pixel in the video memory is allocated up to 24 bits.

A number of graphical procedures, along with absolute coordinates, also use relative coordinates, which are specified as offsets (dx, dy) relative to the position of the current CP point (Current Point). When creating a graphics window, the current point is moved to the origin. Its subsequent movements depend on the graphics operations being performed. For example, when constructing a straight line segment, the current point is transferred to the end of the displayed segment. When drawing a circle, the position of the current point does not change. The current point is usually called a graphic cursor, which, unlike a constantly blinking text cursor, is not displayed in the graphics window so as not to distort the displayed image. The program can interrogate the coordinates of the current point using the GetX and GetY functions.

Question number 33.