ComputersProgramming

Programming. Basic algorithmic constructions

For the creation of any programs, basic algorithmic constructions are needed. The 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 basic 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 changing the input data, occurring in the form of discrete steps. With this concept, every person encounters in life, whoever he is. Algorithms may well be called 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 process of work.

In fact, the algorithm is accurate to the smallest detail 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 you completely track the work of algorithms and their properties, 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 simply to follow the necessary actions mechanically. 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 that nothing is 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? Required result. The algorithm can not lead to any 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 from 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 is 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 into tables and, as a rule, is used as an auxiliary element.
  • Formally verbal. The basis of the verbal method of explanation is taken, but mathematical formulas or symbols are also recorded in such actions.
  • 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, it is necessary to additionally write down an action (step).

Algorithmic constructions

Some argue that the algorithms have not 3 kinds, but 4. The main algorithmic constructions: linear, branching, cyclic. What is the reason for this misconception is unclear. However, for a simple solution of 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. Such a solution method 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.
  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 branches are complex. And the last 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 "rewind" the cycle or more, there is no need.

Cycles can be deterministic and iterative. The first represent a repetition of actions with the already known number of repetitions. An iteration cycle is one that repeats an indefinite number of times until the condition becomes true or false.

Basic Algorithm

It is worth remembering that the basic algorithm does not apply to the basic algorithmic constructions. What is he like? 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 algorithms are linear and nonlinear. Consider the first option. Why is it called that? Everything is extremely simple. The fact is that all the actions that are reproduced in the algorithm have a clearly sequential execution, all steps are performed strictly one after another. As a rule, such tasks are small and have a low level of complexity.

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

  1. Pour the 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 the 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 a difficult matter, but when it comes to linear algorithms, it is often very easy to implement them.

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 met or not. Each path is called a branch.

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

Typically, logical expressions are represented by less than, greater than, less than or equal to, greater than or equal to, equals, or not equal to. Sometimes there are variants where the condition is related to each other with the help of the and (and) and or (or) commands.

An example of such an algorithm may be the solution of the following problem: if the expression ((x + 3) / 1) is equal to a positive number, then 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 loop with a counter is a loop that includes a 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. How 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.

Cycle with postcondition

A loop with a postcondition can repeat an 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 built on this. The basic algorithmic constructions of this type work at this pace.

To implement this cycle, the Repeat A to B construction is necessary. It is literally translated as "repeat actions while the condition is false". Accordingly, the process of repetition is expressed through A, and the data through B, which as a result must take the correct value.

Cycle with precondition

A loop with a postcondition is constructed in such a way that it 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 design means: "as long as the condition is correct, perform the actions".

All the basic algorithmic constructions work only in certain cases. What are they in the cycle with a precondition? If it is necessary to repeat not one action, 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.

Auxiliary algorithm

The auxiliary algorithm is used in other processes by specifying only its 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.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.