Hardware and software setup

Programming of cyclic computing processes. Manual tabulating and programming functions

Laboratory work № 2

"Programming of cyclic computing processes"

Objective

Learning loop operators in C++.

Theoretical part

Cycle- a kind of control structure in high-level programming languages, designed to organize multiple execution of a set of instructions. Also, a cycle can be called any repeatedly executed sequence of instructions, organized in any way.

A set of instructions designed to be executed multiple times is called loop body . Executing the loop body once is called iteration . An expression that determines whether iteration will be performed again, or the loop will end, is called exit condition or terminating the cycle. The variable that stores the current iteration number is called iteration counter loop or just counter cycle. The loop does not necessarily contain a counter, there may be more than one counter - the condition for exiting the loop may depend on several variables that are changed in the loop.

The execution of any loop includes the initial initialization of the loop variables, checking the exit condition, executing the loop body, and updating the loop variable at each iteration. In addition, most programming languages ​​provide means for early control of the loop, for example, loop termination statements, that is, exit from the loop regardless of the truth of the exit condition (in C language - break) and iteration skip operators (in C language - continue).

There are the following types of cycles.


1. Unconditional loops. Cycles , exit from which is not provided by the logic of the program. Due to their atypicality, programming languages ​​do not provide special syntactic means for creating infinite loops, therefore such loops are created using constructions designed to create ordinary (or conditional) cycles. For example, the C language uses a loop for(;;) with blank sections.

2. Conditional Loops. These are loops whose execution is controlled by some condition that is checked at the beginning or end of the loop body. Accordingly, conditional loops come with a precondition and a postcondition. A loop with a precondition is a loop that is executed while some condition specified before its start is true. This condition is checked before execution of the loop body, so the body may not be executed even once (if the condition is false from the very beginning). In most procedural programming languages, it is implemented by the operator while, hence its second name - while-loop. In C++, such a loop looks like:

while(<условие>)

<тело цикла>

Loop with postcondition - loop in which the condition is checked after execution of the loop body. It follows that the loop is always executed at least once. In Pascal, this loop is implemented by the operator repeat..until, in C - do…while, For example:

<тело цикла>

while(<условие продолжения цикла>);

In different languages, there are some differences in the use of a loop condition with a postcondition. In Pascal and the languages ​​descended from it, the condition of such a cycle is treated as exit condition The loop terminates when the condition is true. In C - how continuation condition(the loop terminates when the condition is false; such loops are sometimes called "while loops").

Loop out of the middle- the most general form of a conditional loop. Syntactically, such a cycle is formed using three constructions: the beginning of the cycle, the end of the cycle, and the command to exit the cycle. The start construct defines the point in the program at which the loop body begins, the end construct defines the point where the body ends. 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. Naturally, in order for the loop to be executed more than once, the exit command should not be called unconditionally, but only when the condition for exiting the loop is met.

The main feature of the cycle of this type is that the part of the loop body located after the start of the loop and before the exit command is always executed (even if the exit condition from the loop is true at the first iteration), and the part of the loop body located after the exit command is not executed at the last iteration. With a middle exit loop, you can easily model both a precondition loop (by placing an exit statement at the beginning of the loop body) and a postcondition loop (by placing an exit statement at the end of the loop body).

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. In most procedural programming languages, it is implemented by the operator for, 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.

In some languages, such as C and others derived from it, the loop for, despite the syntactic form of a loop with a counter, is actually a loop with a precondition. That is, in C, the loop construct:


for (i = 0; i< 10; ++i)

Loop body

actually represents another form of the construction notation:

while (i< 10)

Loop body

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 (this does not have to be a counter change; it can be editing a pointer or some completely extraneous operation). For languages ​​of this kind, the problem described above is solved very simply: the counter variable behaves completely predictably and, at the end of the loop, retains its last value.

Looping in C++

1. The for loop.

A loop using the for statement is convenient to use when the required number of iterations is known in advance. The syntax of this operator is:

for (action before the start of the loop;

condition for continuing the cycle;

actions at the end of each loop iteration) (

loop instruction;

loop instruction 2;

cycle instruction N;

There is a special case of this entry (with a counter):

for (counter = value; counter< значение; шаг цикла)

cycle body;

First, the initial value is assigned to the counter, after which a semicolon is placed. Then the end value of the loop counter is set. Once the counter value reaches the specified limit, the loop will end. Then the loop step is set - the value by which the loop counter will increase or decrease with each pass.

Example: Calculate the sum of all numbers from 1 to 1000.

#include

using namespace std;

int i; // loop counter

int sum = 0; // sum of numbers from 1 to 1000.

setlocale(0, "");

for (i = 1; i<= 1000; i++) // задаем начальное значение 1, конечное 1000 и задаем шаг цикла - 1.

cout<< "Сумма чисел от 1 до 1000 = " << sum << endl;

2. The while loop.

Organization of cycles while or do...whileconvenient to use when the required number of iterations is not known in advance. Loop Syntax while in C++ looks like this.

while (Condition) (

Loop body;

This loop will be executed as long as the condition specified in parentheses is true. An example of the implementation of summing numbers from 1 to 1000 using a while loop:

#include

using namespace std;

setlocale(0, "");

while (i< 1000)

cout<< "Сумма чисел от 1 до 1000 = " << sum << endl;

3. do while loop

Cycle do while differs from while the fact that when it executes the loop, one pass of the loop will be executed regardless of the condition. A program for solving the problem of finding the sum of numbers from 1 to 1000 using a loop do while:

#include

using namespace std;

setlocale(0, "");

int i = 0; // initialize the loop counter.

int sum = 0; // initialize the sum counter.

do (// loop through.

) while (i< 1000); // пока выполняется условие.

cout<< "Сумма чисел от 1 до 1000 = " << sum << endl;

1. Develop a program in C++ to solve the problem corresponding to the selected option.

2. Enter, debug and compile the program. Check if it works correctly.

1. Block diagram of the program algorithm.

2. Program listing with comments.

3. Results of control runs.

Control questions

1. Define a loop, a loop body, an iteration.

2. What is the loop exit condition, iteration counter?

3. What are the steps involved in any cycle?

4. How does the postcondition loop work?

5. How does a loop with a precondition work?

6. How does the counter loop work?

7. How the cycle is implemented using for statement?

8. How is the loop implemented using the while statement?

9. How is a loop implemented using the do while statement?

1. Display the following sequence of characters

2. A sequence of integers is entered (0 is the end of the sequence), find the difference between the smallest among positive and the largest among negative numbers.

3. Enter a sequence of N integers, find the difference between the product of odd numbers and the largest among negative numbers.

4. A sequence of integers is entered, 0 is the end of the sequence. Determine if the sequence contains at least two integers that are multiples of 3, 5, and 7.

5. A sequence of N integers is entered. Determine the largest number among multiples of 11.

6. A sequence of integers is entered, 0 is the end of the sequence. Determine if the sequence contains at least three negative even numbers.

7. A sequence of N real numbers is entered. Determine the smallest number among numbers greater than 20.

8. A sequence of integers is entered, 0 is the end of the sequence. Calculate the number of positive numbers that are multiples of 7 and not multiples of 5, and the sum of the negative elements of the sequence.

9. A sequence of N real numbers is entered. Determine the arithmetic mean among the elements of a sequence that are multiples of 7.

10. A sequence of numbers is entered, 0 is the end of the sequence. Determine if the sequence is strictly decreasing.

12. A sequence of integers is entered, 0 is the end of the sequence. Determine if the sequence contains at least two adjacent positive integers.

13. A sequence of N real numbers is entered. Determine if a sequence is character-alternating.

14. A sequence of N real numbers is entered. Determine the difference between the minimum positive and maximum negative elements of the sequence.

15. Find the last three multiples of four in the range from 1 to N. Calculate the sum of these numbers.

16. Display the following character sequence

18. Calculate the product of the last three numbers not divisible by 5 in the range from N1 to N2.

19. Calculate the arithmetic mean of even numbers that are not multiples of four in the range from 1 to K.

Task number 2

Programming branching computing processes

Objective

Get programming skills for branching computational processes. Learn conditional statements, boolean variables, logical relationships, and operations.

Let the area D be given on the x, y plane. It is required to determine whether the point with coordinates (x, y) “falls” into the area D or not. If it hits, then it is necessary to calculate the function and the variable M, the value of which is a sign of "hit" or "missing", assign the value M=1. Otherwise calculate and put M=2.

Progress

Algorithm for solving the problem:

1. Write out the region D and write out the functions and

2. Write down the equations that determine the boundaries of the area

3. Prepare 7 points so that some of the points are inside D, and some are outside it.

4. Write a program to perform the task.

5. For the selected points, get and record the results of the count.

f 1= tg(x)/e y f 2 =

(x>0 and y>0 and X**2+Y**2<=9) или (x<0 и y>0 and y<=x+4) или (x<0 и y<0 и x>(-4) and y>(-2))

then the point belongs to the region D

1)(1;1)
2)(3;3)
3)(-1;2)
4)(-5;2)
5)(-2;-1)
6)(-5;-3)

Fortran MathCAD
x y z m c z m c
0.1573E+01 T 1.573
0.0751E+01 F 0.751
-1 -0.1211E+01 T -1.211
-5 0.083E+01 F 0.83
-2 -1 0.394E+01 T 3.94
-5 -3 0.0282E+01 F 0.282
-3 0.0271E+01 F 0.271

PRINT *,"X=?,Y=?";READ *,X,Y

C=(x>0..and.y>0..and.X**2+Y**2<=9).or.(x<0..and.y>0..and.y<=x+4).or.(x<0..and.y<0..and.x>(-4).and.y>(-2))

PRINT2,X,Y,Z,C,M

2 FORMAT("X=",F7.3,3X,"Y=",F7.3,3X,"Z=",E11.4,3X,"C=",L2,3X,"M=", I2/)

END PROGRAM LAB2

FUNCTION F1(X,Y)

F1=tan(x)/exp(x)**y

FUNCTION F2(X,Y)

F2= (sqrt(sin(2*x+3*y)**2)**1./3.)

print*,"a,b,c=?";read "(2F7.3)",a,b,c

q=a>b.and.a

a c

w=b>a.and.b

b c

e=c>a.and.c

c b

28.,558.,345. 345.0000


Task number 3

Programming of cyclic computing processes

Objective

Get skills in programming cyclic computing processes. Learn step loops and conditional loops.

1. Write down the calculation formulas for calculating the term and the final amount

2. Compile an algorithm for calculating with a given accuracy the sum of a finite number of terms in the specified range of the parameter x with a given step. Divide the range of x by 10 to get the step.

3. Make a program for solving the problem. In the program, for each x value, provide for the calculation of both the approximate and the exact sum of the corresponding y series according to the formula given in the table.

the exact value of the sum of the series, as well as the number of summed terms.

Progress

1. Set x0-cycle start and xl-cycle end

2. Set the step h according to the formula

4. Write a program for solving problems in a recurrent way

5. Build a table of received data

Row formula:

Formula for Exact Sum Calculation: π/4

1. x0= π/10 xk=9 π/10

3.


real:: a,b,c,eps,h,s,x,x0,xk,y,p

b=(sin*(x)*(2*n)-1+sin*((2*n-1)*x))/4*n-2

if(abs(a)

print 2,x,s,y,n; write(1,2)x,s,y,n

end do 2 format("x=",f6.3,3x,"s=",e10.3,3x,"y=",e10.3,3x,"n=",i5)

end program lab3


x S(x) y
0.314 0.324 0.785
0.565 0.622 0.785
0.817 0.979 0.785
1.068 1.404 0.785
1.319 1.43 0.785
1.57 -19.257 0.785
1.821 -533.961 0.785
2.072 -8.624E+3 0.785
2.324 -9.986E+4 0.785
2.575 -8.928E+5 0.785
2.826 -6.477E+6 0.785

“Programming of cyclic computing processes”

Objective: mastering methods for compiling algorithms for cyclic computational processes and organizing cyclic programs of complex structure.

Theoretical part

4.1.1. Cyclic algorithms.

A loop is a sequence of actions that can be performed more than once.

A cyclic algorithm is an algorithm that contains one or more cycles.

There are 3 types of cycles:

Loop with precondition;

Loop with postcondition;

Cycle with a counter (counting cycle).

If the execution of the loop is associated with some logical condition, then loops with a precondition or a postcondition are used.

Counting loops represent a class in which the execution of the loop body must be repeated a predetermined number of times.

Block diagrams of cyclic algorithms look like this:

1. Loop with a counter.

2. Loop with precondition. 3. Loop with postcondition.

4.1.2 Loop statements in the C++ programming language.

In C++, there is a corresponding operator for each kind of loop:

while loop (with precondition);

do...while loop (with postcondition);

For loop (countable).

1. Loop operator like while

Recording form:

while (condition) statement;

where: (condition) – logical expression;

statement - the statement or body of the loop that is executed in the loop.

If the body of the loop is a compound statement, then it must be enclosed in operator brackets (...):

while (condition)

operator group

The scheme of how such a cycle works: while the condition is true (true), the body of the cycle is executed and the condition is checked again, etc. When the condition becomes false, the loop terminates.

2. Loop operator like do…while

Recording form:

operator;

while (condition);

The scheme of operation of such a cycle: first, the operator is executed, then the condition is checked, if the condition is true, the operator is executed and the condition is checked again, etc. When the condition becomes false, the loop terminates.

If the loop body is a compound statement, then, as for a loop with a precondition, it must be enclosed in operator brackets (...):



operator group

while (condition);

3. Loop operator like for

Recording form:

operator;

A is an initial expression that sets initial values ​​for the loop parameter and, if necessary, initial values ​​for other parameters. For instance:

i=0, x=0.5, p=1, s=0

B is a conditional expression that checks the condition for continuing the loop. For instance:

C is an increment expression that increments the loop parameter and, if necessary, other parameters, then they are written as a list. For example: x+=0.1, i++

4.1.3 An example of compiling an algorithm and a program in C++ for a cyclic computational process.

Calculate the value of an expression:

b- the initial value, its value is entered from the keyboard and does not change;

a– changes in the range with step 1;

y– result, its values ​​are displayed on the screen.

Based on the assignment condition, the variable a is an integer, so it can be used as a counter in the counting cycle.

The block diagram of the algorithm for solving this problem using the counting cycle is as follows:

#include

#include

#include

printf("Enter b: ");

scanf(“%f”,&b);

printf("a y\n");

for (a=0;a<=10;a++)

printf("%3d",a);

printf("%8.2f\n",y);

y=(a-b)/sqrt(a);

printf("%8.2f\n",y);

The block diagram of the algorithm for solving this problem using a loop with a precondition is as follows:

The text of the C++ program corresponding to this algorithm is as follows:

#include

#include

#include

printf("Enter b: ");

scanf(“%f”,&b);

printf("a y\n");

printf("%3d",a);

printf("%8.2f\n",y);

y=(a-b)/sqrt(a);

printf("%8.2f\n",y);

else printf(" y does not exist\n");

The block diagram of the algorithm for solving this problem using a loop with a postcondition is as follows:

The text of the C++ program corresponding to this algorithm is as follows:

#include

#include

#include

printf("Enter b: ");

scanf(“%f”,&b);

printf("a y\n");

printf("%3d",a);

printf("%8.2f\n",y);

y=(a-b)/sqrt(a);

printf("%8.2f\n",y);

else printf(" y does not exist\n");

while(a<=10);

Practical part

4.2.1 Requirements for the performance of work:

Complete the task from laboratory work No. 3 for a range of values ​​of one of the variables. The variable variable, its range of change and the step are indicated in Table 4. Draw up flowcharts of algorithms and programs for two types of cycles indicated in the individual task (Table 4).

Arrange the output of the results in such a way that the values ​​of the variable parameter are clearly distinguished and, for each of its specific values, the values ​​of the result (three variables from column 2 of table 3) are displayed in the form of a table.

The order of the work.

1. Perform task analysis, formulate a problem statement.

2. Make block diagrams of algorithms.

3. Write a program in C++. Provide input of initial data from the keyboard and output of results to the screen.

4. Check the performance of the program on various initial data.

5. Perform an analysis of the results.

Variants of individual tasks.

Variants of individual tasks are selected from table 4 in accordance with the student's number in the group list in the teacher's journal.

Table 4. Variants of individual tasks

No. p / p mutable variable Loop types
10 ≤ a ≤ 10,Δ a=1
-4 ≤ d ≤ 4, Δ d = 0.5
-6 ≤ x ≤ 3, Δ x = 0.5
0 ≤ b ≤ 3 0, Δ b = 1.5 1. With precondition, 2. Countable
-15 ≤ j ≤ 1 0, Δ j = 0.5 1. With precondition, 2. With postcondition
5 ≤ e ≤ 35,Δ e = 2 1. Countable, 2. With postcondition
-5 ≤ m ≤ 15,Δ m = 1 1. With precondition, 2. Countable
1 ≤ c ≤ 70,Δ c=3 1. With precondition, 2. With postcondition
1.5 ≤ c ≤ 15,Δ c = 0.5 1. Countable, 2. With postcondition
-8 ≤ b ≤ 28,Δ b = 2 1. With precondition, 2. Countable
-4.5 ≤ x ≤ 11.5,Δ x = 0.5 1. With precondition, 2. With postcondition
-7 ≤ k ≤ 2,Δ k = 0.3 1. Countable, 2. With postcondition
-1 ≤ m ≤ 21,Δ m = 1 1. With precondition, 2. Countable
-2 ≤ e ≤ 34,Δ e = 2 1. With precondition, 2. With postcondition
-11 ≤ c ≤ 23,Δ c = 2 1. Countable, 2. With postcondition
-13 ≤ p ≤ 50,Δ p=3 1. With precondition, 2. Countable
3.3 ≤ b ≤ 9.3,Δ b = 0.3 1. With precondition, 2. With postcondition
3.5 ≤ y ≤ 12.3,Δ y=0.4 1. Countable, 2. With postcondition
-7.5 ≤ a ≤ 5.7,Δ a = 0.6 1. With precondition, 2. Countable
-1.5 ≤ h ≤ 1.2,Δ h = 0.1 1. With precondition, 2. With postcondition
0 ≤ h ≤ 10,Δ h=0.5 1. Countable, 2. With postcondition
-15 ≤ b ≤ 15, Δ b=2 1. With precondition, 2. Countable
-7 ≤ l ≤ 3, Δ l = 0.5 1. With precondition, 2. With postcondition
-5.5 ≤ b ≤ 6.5, Δ b = 0.5 1. Countable, 2. With postcondition
1 ≤ k ≤ 9, Δ k = 0.4 1. With precondition, 2. Countable
0 ≤ b ≤ 6.9,Δ b = 0.3 1. With precondition, 2. With postcondition
-3 ≤ v ≤ 9,Δ v = 0.6 1. Countable, 2. With postcondition
-2 ≤ p ≤ 2.6,Δ p = 0.2 1. With precondition, 2. Countable

4.3 Control questions and practical tasks:

1. How does the while statement work?

2. How does the do ... while statement work?

3. How does the for statement work?

4. Underline the statements in the program that form the cycle.

5. What is the difference between while and do ... while statements?

6. Replace one loop operator with another in the program.

1. Methods for constructing cyclic computing processes in programs.

2. Entered into the computerNreal numbers. Write a program that displays the arithmetic mean of this set.

Introduction

Loop programs are used in almost any software. In this case, cycles can be explicit and implicit. In particular, the implicit loop is present in interrupt handlers, which actually run in an infinite loop whose body is triggered by the interrupt. Subroutines are also cyclic - window functions of Windows applications. Further, programs with a cycle are considered, the body of which contains functional modules.

Cyclic process is a computational process in which calculations are repeatedly performed using the same formulas for different values ​​of the argument.

Programs that implement a cyclic process are called cyclic programs.

The organization of the cycle can be divided into the following stages:

preparation (initialization) of the cycle (AND);

execution of loop calculations (loop body) (T);

parameter modification (M);

checking the condition for the end of the cycle (U).

The order in which these steps, eg T and M, may be performed may vary. Depending on the location of the test, the loop termination conditions distinguish between loops with lower and upper endings. For a loop with a lower end, the loop body is executed at least once, since first the calculations are performed, and then the exit condition from the loop is checked.


In the case of a top-terminated loop, the loop body may not be executed even once if the exit condition is met immediately.

A loop is called deterministic if the number of repetitions of the loop body is known or determined in advance. The loop is called iterative if the number of repetitions of the loop body is not known in advance, but depends on the values ​​of the parameters (some variables) involved in the calculations.

Loop body This is a part of the program that repeats many times.

Cycle parameter is a variable that takes on new values ​​each time the loop is repeated (there are simple and complex loops).

General view of the cycle n times

In general, the cycle n times is written as follows:

nc number of repetitions times

The service word nts (beginning of a cycle) and kts (end of a cycle) are written strictly one under the other and are connected by a vertical bar. To the right of this line, a repeated sequence of commands (loop body) is written.

The number of repetitions is an arbitrary integer.

When the algorithm is executed, the sequence of commands in the loop body is repeated the specified number of times. The rules of the algorithmic language allow specifying any integer number of repetitions. It can be zero or even negative. These cases are not considered erroneous, just the body of the loop will not be executed even once, and the computer will immediately proceed to the execution of the commands written after the kts

General view of the cycle

In general, the cycle is written as follows:

nc bye condition

| loop body (sequence of commands)

When performing a cycle, the computer repeats the following steps:

a) checks the condition written after the service word;

b) if the condition is not met, then the execution of the loop ends and the computer starts executing the commands written after the kts. If the condition is met, then the computer executes the loop body, checks the condition again, and so on.

General view of the cycle for

nc for i from i1 to i2

| loop body (sequence of commands)

Here i is the name of an integer type value, i1, i2 are arbitrary integers or expressions with integer values. The loop body is sequentially executed for i = i1, i = i1 + 1, i1 + 2, …i = i2.

The rules of the algorithmic language allow specifying any integers i1, i2. in particular, i2 may be less than i1. this case is not considered erroneous - just the body of the loop will not be executed even once, and the computer will immediately proceed to the execution of the commands written after the kts.

Loop n times and loop while

Loops n times and so far are arranged in the algorithmic language in almost the same way. This is not surprising, because both of these commands set up a cycle - a repeating sequence of commands. The service words nts and kts indicate that a cycle is being executed, and the cycle header specifies a specific mechanism for its execution.

However, these two cycles have one significant difference. Starting to execute the loop n times, the computer knows how many times it will have to repeat the body of the loop. When executing a loop, this is not the case yet: the computer checks the condition of the loop each time and cannot determine in advance when the execution will end. You can only find out the number of repetitions of the cycle until after the cycle is completed.

This makes it clear in which cases which cycle should be used. If the number of repetitions is known by the time the loop begins, it is convenient to use the loop n times. If the number of repetitions cannot be predetermined, a cycle by far is necessary.

For example, the automatic control program has the structure shown in Fig. one. Modules included in the cycle(as well as interrupt handling modules), with one input and one output each, usually have a characteristic feature: modules contain static variables that are assigned a value in the current cycle, and the analysis of these variables is performed in the next cycle. Thus, the mentioned variables characterize the state of the module at the end of the current or the beginning of the next program cycle. In the future, we will consider only such modules of cyclic programs and denote them briefly as MCP.


Fig.1. Typical structure of a control program with an infinite loop.

MCPs have a diverse structure, the complexity of which must be assessed according to special criteria. V.V. Lipaev proposed a convenient and objective criterion for the complexity of program modules, namely: the number and total length of paths in the control graph of the module. This only takes into account conditional and select statements. However, this criterion is clearly not enough for the MCP with static memory, because when analyzing the MCP it is necessary to remember the values ​​of all static variables set in the previous cycle. In addition, there are no recommendations for the standardization of algorithms and programs, except for the long-known structured programming in commonly used programming languages ​​such as C and Pascal. This article proposes to fill these gaps in relation to the MCP.

2. Fragments of modules of cyclic programs

A two-pole fragment, or just a fragment, we will consider a program section with one input and one output (including loop operators) under the assumption that the MCPs under consideration are structured. The simplest fragment includes a single operator. A sequence of fragments is also a fragment. The MCP, in turn, is a fragment and consists of a sequence of fragments.

A method of independent fragments is proposed for synthesizing the structure of modules that implement decision tables. In this case, a fragment is considered independent if it can be inserted anywhere in the sequence of module fragments. The independence of the location of such a fragment is due to the fact that the data analyzed in it is not formed in the specified sequence of fragments, and the data generated in an independent fragment is not analyzed in this sequence of fragments. Therefore, independent fragments can be executed in parallel (pseudo-parallel). On fig. 2 shows possible implementations of a module with two independent fragments. In options "a" and "b" the fragments are rearranged without distorting the essence of the program; in the “c” version, the fragments are implemented in parallel.


Fig.2. Options for implementing a module with independent fragments:

a) and b) - sequential implementation,

c) - parallel implementation: a double horizontal line indicates the parallelization of the program, a thick horizontal line indicates the completion of parallel processes.

A dependent fragment is one whose location depends on the location of another fragment(s) in the module. We will distinguish between top- and bottom-dependent fragments. A top-dependent fragment must always be located below some fragment in which the variables used in this (dependent) fragment are formed. A bottom-dependent fragment should always be placed above the fragment that uses the variables generated in this fragment. Two dependent fragments, one of which is upper dependent on the second, and the second dependent on the first from below, will be called mutually dependent fragments. They cannot be interchanged and cannot be implemented in parallel. On fig. 3 shows an example of a module with mutually dependent fragments. Between mutually dependent fragments there may be others, dependent or independent of them. Fig.3. Module with dependent fragments.

We will call a fixed fragment a dependent fragment, the location of which in the module is strictly defined. For example, in the module for recognizing a character entered from the keyboard, the first should be the bottom-dependent fragment of the character directly entered. The "start" and "end" operators of a module are fixed fragments.

Absolutely independent fragments do not exist, if only because in any module there are mentioned fixed fragments of the beginning and end. Therefore, an independent fragment, in general, has a region of possible location limited by two mutually dependent fragments. That is, a more rigorous definition of an independent fragment is as follows: independent with respect to two fixed fragments is a fragment that can be placed anywhere in the sequence of fragments bounded above and below by the specified fixed fragments.

Liked the article? Share with friends!
Was this article helpful?
Yes
Not
Thanks for your feedback!
Something went wrong and your vote was not counted.
Thank you. Your message has been sent
Did you find an error in the text?
Select it, click Ctrl+Enter and we'll fix it!