Hardware and software setup

Programming of cyclic computing processes. Cyclic computing processes Programming of cyclic computing processes

1. Methods for constructing cyclic computing processes in programs.

2. Entered into the computerNreal numbers. Write a program that displays the average value of this set on the screen.

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. Next, we consider programs with a cycle whose body 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.

“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 example:

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

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

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 the 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 loop.

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

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

By clicking on the "Download archive" button, you will download the file you need for free.
Before downloading this file, remember those good essays, control, term papers, theses, articles and other documents that are unclaimed on your computer. This is your work, it should participate in the development of society and benefit people. Find these works and send them to the knowledge base.
We and all students, graduate students, young scientists who use the knowledge base in their studies and work will be very grateful to you.

To download an archive with a document, enter a five-digit number in the field below and click the "Download archive" button

Similar Documents

    Drawing up the "Stores" reporting sheet in Excel 2013. Working with tables of the Microsoft Office family. Construction of a pie chart and histogram, graphs. Development of procedures for tabulating functions. Programming user functions in VBA.

    term paper, added 04/03/2014

    Microsoft Office package. Spreadsheet MS Excel. Creation of a screen form and data entry. Formulas and functions. Explanation of MS Excel user functions. Physical statement of problems. Setting boundary conditions for admissible values ​​of variables.

    term paper, added 06/07/2015

    Features of using the built-in functions of Microsoft Excel. Creating tables, filling them with data, plotting graphs. Application of mathematical formulas to execute queries using application packages. Technical requirements for a computer.

    term paper, added 04/25/2013

    File organization and access. file operations. Programming using built-in I/O functions; linear, branching and cyclic computational processes using If-else, the Case selection operator; arrays and matrices.

    term paper, added 05/24/2014

    Microsoft Excel spreadsheet processor - an application program designed to automate the process of processing economic information presented in the form of tables; application of formulas and functions for making calculations; plotting.

    abstract, added 02/03/2013

    Programming of computing processes in the FORTRAN language to ensure the interaction of the "Man-Production-Nature" system. Using arrays to compile cycles for calculating the level of environmental pollution, the cost of waste processing.

    term paper, added 05/30/2014

    Creating an application that will plot function graphs for a given mathematical expression. Development of the program "Generator of mathematical functions". Creating a function wizard for entering a mathematical expression, testing.

    thesis, added 02/16/2016

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!