Flowchart: examples, elements, construction. Flowcharts
This article will look at examples of flowcharts that you may find in textbooks on computer science and other literature. A flowchart is an algorithm that solves any task assigned to the developer. First you need to answer the question of what an algorithm is, how it is represented graphically, and most importantly, how to solve it, knowing certain parameters. It should be immediately noted that there are several types of algorithms.
What is an algorithm?
This word was brought into use by the mathematician Mohammed al-Khorezmi, who lived in the period 763-850 years. It is he who is the person who created the rules for performing arithmetic operations (there are only four of them). But the GOST from 1974, which states that:
An algorithm is an exact prescription that defines a computational process. Moreover, there are several variables with given values that lead to calculations to the desired result.
The algorithm allows you to clearly specify the performer to perform a strict sequence of actions to solve the problem and get the result. The development of an algorithm is the breaking up of one big task into a certain sequence of steps. Moreover, the developer of the algorithm is required to know all the features and rules for its preparation.
In total, there are eight features of the algorithm (regardless of its type):
- There is a function to enter the initial data.
- There is a conclusion of a certain result after the completion of the algorithm. It must be remembered that the algorithm is needed in order to achieve a certain goal, namely, to obtain a result that is directly related to the source data.
- The algorithm must have a discrete type structure. It should be presented in sequential steps. Moreover, each next step can begin only after the completion of the previous one.
- The algorithm must be unambiguous. Each step is clearly defined and does not allow arbitrary interpretation.
- The algorithm must be finite - it is necessary that it be executed in a strictly defined number of steps.
- The algorithm must be correct - to ask only the right solution to the problem.
- Community (or mass character) - it should work with different source data.
- The time given to solve the algorithm should be minimal. This determines the effectiveness of the task.
And now, knowing what algorithm flowcharts exist, you can begin to consider how to write them. And they are not very many.
This form is usually used when describing the procedure for a person:“Go there, I do not know where. Bring something, I do not know what. "
Of course, this is a comic form, but the essence is clear. As an example, for example, the usual record on the windows of buses:"In the event of an accident, pull out the cord, squeeze out the glass."
Here the condition is clearly set under which two actions must be performed in a strict sequence. But these are the simplest algorithms, there are more complex ones. Sometimes formulas, special designations are used, but with the obligatory condition - the performer must understand everything.
It is allowed to change the order of actions if it is necessary to return, for example, to the previous operation, or to bypass some command under a certain condition.In this case, it is desirable to number the teams and the command to which the transition is taking place is required"Having finished all the manipulations, repeat paragraphs 3 to 5."
Record in graphic form
This entry includes elements of flowcharts. All elements are standardized, each team has a certain graphic record. A specific team must be recorded within each of the blocks in the usual language or mathematical formulas. All blocks must be connected by lines - they show exactly what order in the executed commands. Actually, this type of algorithm is more suitable for use in the program code, rather than verbal.
Writing in programming languages
In that case, if the algorithm is necessary for the task to be solved by a program installed on a PC, then you need to write it down with a special code. For this there are many programming languages. And the algorithm in this case is called a program.
A block diagram is a representation of the algorithm in graphical form. All teams and actions are represented by geometric shapes (blocks). Inside each figure fits all the information about the actions that need to be performed. Connections are depicted as normal lines with arrows (if necessary).
For the design of flowcharts of algorithms there is a GOST 19.701-90. He describes the procedure and rules for creating them in graphical form, as well as the basic methods of solving. This article contains the basic elements of flowcharts that are used in solving problems, for example, in computer science. And now let's consider the rules of construction.
Basic rules for creating a flowchart
We can single out such features that any flowchart should have:
- Must be present two blocks - "Start" and "End". And in a single copy.
- From the initial block to the final link should be drawn.
- Of all the blocks, except the final one, the flow lines should go out.
- There must be a numbering of all blocks: from top to bottom, from left to right. The sequence number should be placed in the upper left corner, making the mark break.
- All blocks must be connected to each other by lines. They should determine the sequence with which actions are performed. If the flow moves upwards or from right to left (in other words, in the reverse order), then arrows are drawn.
- Lines are divided into outgoing and incoming.It should be noted that one line is for one block outgoing, and for the other one incoming.
- From the initial block in the scheme, the flow line only leaves, since it is the very first.
- But the final block has only an input. This is clearly shown in the examples of flowcharts that are available in the article.
- To make it easier to read the flowcharts, incoming lines are drawn from above and outgoing lines from below.
- The presence of gaps in the flow lines. They are necessarily marked with special connectors.
- To facilitate the flowchart, it is allowed to write all the information in the comments.
Graphic elements of flowcharts for solving algorithms are presented in the table:
Linear type algorithms
This is the simplest form, which consists of a specific sequence of actions, they do not depend on what data is entered initially. There are several commands that are executed once and only after the previous one is done. The linear block diagram looks like this:
Moreover, connections can go both from top to bottom and from left to right. Such a flowchart is used to write the calculation algorithms using simple formulas that have no restrictions on the values of the variables included in the formulas for calculation.A linear algorithm is an integral part of complex calculation processes.
Flowcharts based on such algorithms are more complex than linear ones. But the essence does not change. A branching algorithm is a process in which further action depends on how the condition is fulfilled and what solution is obtained. Each line of action is a branch.
The diagrams depict blocks that are called "Solution". It has two exits, and a logical condition is written inside. It is on how it will be executed that the further movement in the scheme of the algorithm depends. You can divide the branching algorithms into three groups:
- “Bypass” - while one of the branches has no operators. In other words, several actions of another branch are bypassed.
- "Branching" - each branch has a specific set of actions to be performed.
- A “multiple choice” is a fork in which there are several branches, and each contains a specific set of actions to be performed. And there is one feature - the choice of direction directly depends on what values are given for the expressions included in the algorithm.
These are simple algorithms that are solved very simply. Now let's get to the more complex.
Everything is very clear here - the cyclic block diagram represents an algorithm in which homogeneous calculations are repeated many times. By definition, a cycle is a specific sequence of any actions that are performed multiple times (more than once). And there are several types of cycles:
- Who knows the number of repetitions of actions (they are also called cycles with a counter).
- In which the number of repetitions is unknown - with a postcondition and a precondition.
Regardless of what type of cycle is used to solve the algorithm, it must have a variable with which the output occurs. It determines the number of repetitions of the cycle. The working part (body) of a cycle is a specific sequence of actions that is performed at each step. And now we will take a closer look at all the types of cycles that can occur when drawing up algorithms and solving computer science problems.
The figure shows a simple block diagram in which there is a cycle with a counter.This type of algorithm shows that the number of repetitions of a given cycle is known in advance. And this number is fixed. In this case, the variable counting the number of steps (repetitions) is called a counter. Sometimes in textbooks one can come across other definitions - a cycle parameter, a control variable.
The block diagram illustrates very vividly how a loop works with a counter. Before proceeding with the first step, you need to assign an initial value to the counter - it can be any number, it depends on the specific algorithm. In the case when the final value is less than the value of the counter, a certain group of commands will be executed that make up the body of the loop.
After the body is executed, the counter is changed to the counter step size, denoted by the letter h. In the event that the value that is obtained is less than the final, the cycle will continue. And it will end only when the final value is less than the loop counter. Only in this case will the execution of the action that follows the cycle.
Typically, a block called a “Preparation” is used in the block diagram notation.It is written in the counter, and then the following data is indicated: the initial and final values, the step change. In the block diagram, these are the parameters I n, Ik and h, respectively. In the case when h = 1, the step size is not recorded. In other cases, do it necessarily. It is necessary to follow the simple rule - the flow line should enter from above And the flow line that comes out below (or to the right, depending on the specific algorithm) should show the transition to the next operator.
Now you have fully studied the description of the flowchart shown in the figure. You can proceed to further study. When a cycle with a counter is used, certain conditions must be met:
- The body is not allowed to change (forcibly) the value of the counter.
- It is forbidden to transfer control from outside to the operator of the body. In other words, one can enter a cycle only from its beginning.
This type of cycles is used in cases where the number of repetitions is not known in advance. A cycle with a precondition is a type of algorithm in which, immediately before the body begins to perform, it checks the condition under which the transition to the next action is allowed.Notice how the block diagram elements are depicted.
In the case when the condition is satisfied (the statement is true), a transition to the beginning of the cycle body occurs. It directly changes the value of at least one variable that affects the value of the condition set. If you do not adhere to this rule, we get "looping". In the event that after the next verification of the condition of the execution of the loop body, it turns out that it is false, then an exit occurs.
In the flowcharts of the algorithms, it is allowed to check not the truth, but the falsity of the initial condition. In this case, the cycle will exit only if the value of the condition is true. Both options are correct, their use depends on what specifically it is more convenient to use to solve a particular task. This type of cycle has one feature - the body may not be executed when the condition is false or true (depending on the option that is used to solve the algorithm).
Below is a flowchart that describes all of these actions:
What is a post-conditional loop?
If you look closely, then this kind of cycles is somewhat similar to the previous one.We will try to build a flowchart describing this cycle on our own. The peculiarity is that the number of repetitions is not known in advance. And the condition is set after the withdrawal from the body. This shows that the body, regardless of the decision, will be executed at least once. For clarity, take a look at the flowchart describing the condition and statements:
There is nothing difficult in the construction of algorithms with cycles, it is enough to understand them only once. And now let's move on to more complex structures.
Complex are those constructions within which there is one or more simple cycles. Sometimes they are called nested. At the same time, those constructions that cover other cycles are called “external”. And those that are included in the construction of external - internal. When executing each step of the outer loop, full scrolling of the inner loop occurs, as shown in the figure:
That's all, you have reviewed the main features of the construction of flowcharts for solving algorithms, you know the principles and rules. Now you can consider specific examples of flowcharts from life.For example, in psychology, such constructions are used in order for a person to decide a question:
Or an example from biology to solve the problem:
Solving problems with flowcharts
And now we will consider examples of problems with flowcharts that can be found in computer science textbooks. For example, a block diagram is given, according to which some algorithm is solved:
In this case, the user independently enters the values of variables. Suppose x = 16, and y = 2. The process of doing this:
- The values of x and y are entered.
- The conversion operation is performed: x = √16 = 4.
- Fulfilled condition: y = y2=4.
- The calculation is performed: x = (x + 1) = (4 + 1) = 5.
- The next variable is calculated further: y = (y + x) = (5 + 4) = 9.
- The solution is displayed: y = 9.
In this example, the flowcharts for computer science clearly show how the algorithm is solved. You need to pay attention to the fact that the values of x and y are set at the initial stage and they can be any.