/ Programming. Basic algorithmic constructions

Programming. Basic algorithmic constructions

To create any programs you need basicalgorithmic constructions. Following is the simplest way to solve problems. It can be used, for example, to work with similar examples. There are other types: branching and looping. About them will be told in this article. But first you need to understand what the algorithm is all about.

Algorithm

The word "algorithm" came from Latin algoritmi.What does it mean? The authentic word came from the name of a mathematician, whose work fell on the 9th century. Thanks to the al-Khwarizmi treatise, mankind was able to get acquainted with the main type of algorithmic construction and generally with a general concept.

Earlier, the form of writing the word "algorithm" was adopted. Now it is used only in some cases.

Algorithm is a process that means changinginitial data, occurring in the form of discrete steps. With this concept every person encounters in life, whoever he is. Algorithms may well be the preparation of tea or food, multiplication or addition, the solution of equations, etc. All household appliances, whose work process is automated, functions at the expense of clear steps prescribed in the memory of the processor. Such algorithms are called household algorithms. There are other types. Consider them.

Types of algorithms

The basic algorithmic constructions are divided into several types, which will be discussed in this subparagraph. What are they like?

  1. Informational. Such algorithms work with a lot of data, but the very process of processing them is small in length and uncomplicated.
  2. Managers. The work of such algorithms is related to information that is provided from one or another source. After receiving it, special signals are sent to ensure the operation of the devices.
  3. Computational. Unlike information algorithms, the described work with small amounts of data, but produce a large work process.

In fact, the algorithm is accurate to the smallestdetails instructions. However, not all such data can be called a described concept. To understand the algorithm of an instruction or not, it should be checked for certain properties.

Properties of algorithms

All the basic algorithmic constructions must have actions that obey them. Let's consider this issue in more detail.

If we completely track the work of the algorithms and theirproperties, you can see that it is not necessary to understand their components, it is quite clear that they correspond to the plan. The correct result will be obtained, even if you just mechanically stick to the necessary actions. From this we can conclude that because of the lack of meaning in the awareness of actions, the algorithm can really be allocated to the implementation of a computer. In other words, for automated devices, this process is necessary.

What properties should the basic algorithmic constructions have for the most accurate work?

  1. Clarity.Each command should be as clear as possible to the object being executed. It seems to be nothing easier than, for example, to draw a point in the center, no, but until you have a command that will allow you to perform the action, you will not be able to do it.
  2. Effectiveness.What does this property mean? Obligatory receipt of the result. The algorithm can not but lead to some answer. Because of the error, you can get the wrong result, which was desired, but still it will be. Moreover, the answer must be obtained after a certain number of steps.
  3. The mass. Any algorithm should be applicable to some class of tasks. Between them, they can differ in the original data.
  4. Certainty.Each action must have only one value and do not allow for a derivative decryption. Ideally, no matter how much the program is run, the result should always be the same.
  5. Discreteness. Algorithm - sequential steps. Each step is a command, you can not skip or add new ones.
  6. Correctness.Any algorithm that applies to any kind of task must be correct for everyone. In programming, problems often arise not in writing steps, which often does not take much time, but in performing them for various kinds of questions. Therefore, an important step will be debugging the algorithm. Can help in this and the basic algorithmic design, the repetition of which will achieve better results.

Description of algorithms

If we talk about ways to write algorithms, then we should distinguish the following:

  • Verbal. In other words, in a language that is convenient for expressing a constituent.
  • Tabular. Logically, the algorithm is written to tables and, as a rule, is used as an auxiliary element.
  • Formally verbal. The basis is a verbal way of explanation, but such actions also write mathematical formulas or symbols.
  • Graphic. Such an algorithm is written in a special language of block diagrams.

The last point should be clarified.What is a block diagram? This is a linear or non-linear algorithm, the steps of which are recorded using special blocks. They have their own configuration, purpose and function. In the case of such a description, the algorithm is written in block diagrams that are linked together by lines. In them, you must additionally record an action (step).

basic algorithmic constructions

Algorithmic constructions

Some argue that the algorithms have not 3type, and 4. The main algorithmic design: linear, branching, cyclic. What is the reason for this misconception is unclear. However, for simple solving complex problems, the computer uses the algorithms of these three fairly large groups. Consider them.

  1. Linear.Such a computational process received this name due to the fact that all actions are performed in a linear sequence, with each step being performed no more than once. If we consider the scheme of the problem, then the blocks in it are placed one below the other, depending on the sequence number of the task. Linear algorithms work in such a way that the direction and meaning of actions does not change from the initial data. This method of solution is suitable for calculating the sum or difference, the area of ​​a figure or its perimeter, etc. The main type of algorithmic construction is it.
    algorithm basic algorithmic constructions
  2. Branching.This computational process implies the presence of a logical expression (further LV) and the choice of the condition (the branch "lie" and "truth"). In each case, only one of two or more teams is implemented. There are no tasks and can not be, in which other options will be fulfilled. If there are two branches in the algorithm, it is simple, if more than two, it is complex. And the latter process is easily represented at the expense of the former. The main type of algorithmic construction is both the first point and the second. The following species is also included in this list.
  3. Cyclical. In such an algorithm, there will necessarily be an element that repeats many times, and different initial data are used. In other words, such a process is called a cycle.

It should be noted that all the basic algorithmic constructions (follow, branch, cycle) are interrelated with each other, although they can be used separately.

Creation of cycles and their types

What is needed to create a loop?

  • Cycle counter.This is a variable that sets the initial value, and when the action is repeated, it will change. It must necessarily be part of the algorithm. The basic algorithmic constructions of the cyclic type will not work without it.
  • Change the indicator of the above data before a new cycle is repeated.
  • Checking the condition that the computer decides whether to "scroll" the cycle again or more is not needed.

Cycles can be deterministic anditerative. The first represent a repetition of actions with the already known number of repetitions. An iteration cycle is one that repeats indefinitely, until the condition becomes true or false.

The main type of algorithmic construction is

Basic Algorithm

It is worth remembering that the mainalgorithmic constructs do not apply the basic algorithm. What is it? This concept has long been not found in modern literature, but this does not mean that it does not exist any more. Considering that several branches or repetitions can occur in solving problems, the following conclusion can be singled out. The basic algorithmic constructions (linear, branching, cyclic) are basic. In fact, they represent the "structural unit" of each so-called instruction.

Linear Algorithms

As already clear from the above, the algorithmsthere are linear and nonlinear. Consider the first option. Why is it called that? Everything is extremely simple. The point is that all the actions that are reproduced in the algorithm have a clearly sequential execution, all steps are performed strictly one after another. Typically, such tasks are small and have a low level of complexity.

An example of a linear algorithm can be the process of preparing tea:

  1. Pour water into the kettle.
  2. Put the kettle on the stove to boil.
  3. Take the cup.
  4. Pour tea into the cup.
  5. Add sugar.
  6. After boiling, pour boiling water into the cup.
  7. Take a spoonful.
  8. Mix the sugar.

Programming the basic algorithmic constructions is quite difficult, but if it is a question of linear algorithms, then it is often very easy to implement them.

to the basic algorithmic constructions does not apply

Branching Algorithms

How to understand that the algorithm is branching?It is enough to make sure there is a choice of two or more options, depending on whether the condition is fulfilled or not. Each path is called a branch.

The main feature of the branching algorithm is the existence of a conditional branch. It happens during the verification of the expression to true or false.

As a rule, logical expressions are representedsigns "less", "more", "less than or equal to", "greater than or equal to", "equal", "not equal". Sometimes there are variants where the condition is interconnected with the help of the and (and) and or (or) commands.

An example of such an algorithm may be a solutionof the following problem: if the expression ((x + 3) / 1) is equal to a positive number, output the result to the screen, if negative, tell the user about the error.

It is quite simple to use the basic algorithmic constructions in practice. Branching is one of the most common methods of solution.

Deterministic cycle or cycle with a counter

A cycle with a counter is a cycle that includesA variable that changes the value with a certain step. The step is set by the user or prescribed by the programmer while writing the collateral. Most languages ​​for this loop use the for statement.

For the program to display two lines 4 times:

  1. "How are you?"
  2. "Well thank you!"
  3. "How are you?"
  4. "Well thank you!"

It is necessary to create a deterministic cycle. What does it look like? We use the language "Pascal" for better perception of the design.

1. For i: = 1 to 2 do:

- i is the loop counter, it determines the number of repetitions in the loop.

2. Begin (the operator brackets are opened in order for both phrases to be the body of the loop and to be repeated together.)

3. Writeln ('How are you?'):

- the word writeln means the output of a phrase that is in single quotes.

4. Writeln ('Well, thank you').

5. End.

6. i: = i + 1.

As you can see, it is quite easy and even interesting to use the basic algorithmic constructions. Basic algorithms are really widely known, without them it is impossible to write programs.

basic algorithmic constructions linear branching cyclic

Cycle with postcondition

A loop with a postcondition can be repeatedan indefinite number of actions without inserting operator staples or compound words into them. It will be fulfilled at least once. The loop runs until the condition is false. It stops when the indicators are correct. The algorithm is constructed on this. The basic algorithmic constructions of this type work at this pace.

To implement this cycle, a constructionRepeat A until B. Literally it is translated as "repeat actions, while the condition is false". Accordingly, through A the repetition process itself is expressed, through B - the data, which as a result must take the correct value.

basic algorithmic constructions basic algorithms

Cycle with precondition

A loop with a postcondition is constructed in such a way thatit is executed at least once in any case. However, there are cases when a cycle is necessary in the case of a particular condition, and if there is no repetition, it should not. Otherwise, the result will be incorrect. It is in this case that a loop with a precondition is used. To create it, you need a "while A do B" construct. The first command is literally translated as "bye". A is a condition, and B is an action that will be repeated. The whole construction means: "as long as the condition is correct, perform the actions".

All the basic algorithmic constructions workonly in certain cases. What are they in the cycle with a precondition? If it is necessary that not one action is repeated, but several, it is worthwhile to use either composite operators or special brackets. The cycle may well not be fulfilled if the condition is not true when entering it. Accordingly, the actions will be repeated if it is correct.

basic algorithmic construction repetition

Auxiliary algorithm

The auxiliary algorithm is used in otherprocesses by specifying only his name. It does not apply to the basic algorithmic constructions. In programming languages, this process of action is called a subroutine. To facilitate the work with the code and subsequently more simple problem solving, each action is combined into one block, which is an auxiliary algorithm. Each of them can be given a name, which allows you to repeatedly refer to it.

Liked:
0
Popular Posts
Spiritual development
Food
yup