Contacts

Russian algorithmic language. Algorithmic languages ​​and programming. Basic branching structure

Ministry of Education Russian Federation Perm State Technical University

Department information technologies and automated systems

Vikentieva O. L.

Lecture notes for the course " Algorithmic languages and programming "(Fundamentals of the C ++ language, I semester)

Introduction

In the first semester, the main constructions of the C language and the basic programming technology (structured programming) are considered.

Structured programming is a technology for creating programs that allows, by observing certain rules, to reduce development time and the number of errors, as well as to facilitate the possibility of program modification.

1.1. Algorithm and program

Algorithm is a precise prescription that determines computational process going from variable initial data to the final result, that is, it is a recipe for achieving a goal.

The set of tools and rules for representing an algorithm in a form suitable for execution by a computer is called a programming language, an algorithm written in this language is called a program.

First, an algorithm of actions is always developed, and then it is written in one of the programming languages. The program text is processed by special utility programs - translators. Programming languages ​​are artificial languages. They differ from natural languages ​​by a limited number of "words" and very strict rules for writing commands (operators). The totality of these requirements forms the syntax of the programming language, and the meaning of each construction is its semantics.

1.2 Properties of the algorithm

1. Massiveness: the algorithm should be applied not to one problem, but to a whole class of similar problems (an algorithm for solving a quadratic equation should solve not one equation, but all quadratic equations).

2. Efficiency: the algorithm should lead to a result in a specific number of steps (when dividing 1 by 3, a periodic fraction of 0.3333 (3) is obtained; to achieve the final result, it is necessary to specify the accuracy of obtaining this fraction, for example, up to 4 decimal places).

3. Certainty (determinism) - each action of the algorithm must be clear to its performer (the instruction for a household appliance in Japanese for a person who does not speak Japanese is not an algorithm, because it does not have the property of determinism).

4. Discreteness - the process must be described using indivisible

operations performed at each step (i.e. steps cannot be divided into smaller steps).

Algorithms can be presented in the following forms:

1) a verbal description of the algorithm.

2) graphic description of the algorithm.

3) using an algorithmic programming language

1.2. Compilers and Interpreters

WITH using the programming language, a text is created that describes the previously compiled algorithm. To get a working program, you need to translate this text into a sequence of processor instructions, which is performed using special programs which are called translators. There are two types of translators: compilers and interpreters. The compiler translates the text of the source module into machine code, called an object module, in one continuous process. At the same time, he first looks through the source code of the program in search of syntax errors... The interpreter executes the source module of the program in operator-by-operator mode, by

work progress, translating each operator into machine language.

1.3 programming languages

Different types of processors have different instruction sets. If a programming language is focused on a specific type of processor and takes into account its peculiarities, then it is called a low-level programming language. The lowest level language is assembly language, which simply represents each instruction of machine code in the form of special symbols called mnemonics. With the help of low-level languages, very efficient and compact programs are created, since the developer gets access to all the capabilities of the processor. Because instruction sets for different models processors are also different, then each processor model corresponds to its own assembly language, and the program written in it can be used only in this environment. Similar languages ​​are used to write small system applications, device drivers, etc.

Programming languages high level do not take into account the peculiarities of specific computer architectures, therefore created programs at the source level, they are easily ported to other platforms if the corresponding translators have been created for them. Developing programs in high-level languages ​​is much easier than in machine languages.

The high-level languages ​​are:

1. Fortran is the first compiled language created in 50s of the 20th century. It implemented a number of essential programming concepts. For this language was created great amount libraries ranging from statistical complexes to satellite management, so it continues to be used in many organizations.

2. Cobol - Compiled Language for Economic Calculations and Decisions business problems developed in the early 60s. In Cobol, very powerful tools were implemented for working with large amounts of data stored on external media.

3. Pascal - created at the end 70s Swiss mathematician Niklaus Wirth specifically for teaching programming. It allows you to develop algorithmic thinking, build a short, well readable program, demonstrate the basic techniques of algorithmization, it is also well suited for the implementation of large projects.

4. BASIC - created in 60s also for teaching programming. There are compilers and interpreters for it, it is one of the most popular programming languages.

5. C - was created in the 70s and was not originally considered as a mainstream programming language. It was intended to replace assembler so that it could create programs that are as efficient and short as possible, but not depend on a particular processor. He is in many ways similar to Pascal and has additional features to work with memory. A lot of applied and system programs, and operating system Unix.

6. C ++ is an object-oriented extension of the C language, created by Bjarne Stroustrup in 1980.

7. Java is a language that was created by Sun at the beginning 90s based on C ++. It is designed to simplify the development of C ++ applications by eliminating low-level features from it. main feature language is that it is not compiled into machine code, but into platform-independent bytecode (each instruction takes one byte). This code can be executed using an interpreter - the Java Virtual Machine (JVM).

2.The structure of a C ++ program

A C program has the following structure: # preprocessor directives

. . . . . . . . .

# preprocessor directives function a ()

operators function in ()

operators

void main () // function, which starts the program execution statements

descriptions

assignments

function empty operator

composite

transition

Preprocessor directives - control the transformation of the text of the program before it is compiled. Original program prepared in SI in the form text file, goes through 3 stages of processing:

1) preprocessor transformation of text;

2) compilation;

3) layout (link editing or assembly).

After these three stages, the executable code of the program is formed. The task of prepro-

cessor - transformation of the text of the program before it is compiled. The preprocessing rules are defined by the programmer using preprocessor directives. The directive starts with #. For example,

1) #define - indicates the replacement rules in the text. #define ZERO 0.0

Means that every use of the ZERO name in the program will be replaced

2) #include< имя заголовочного файла>- intended for inclusion in the program text of the text from the "Header files" catalog supplied with standard libraries... Each C library function has a corresponding description in one of the header files. The list of header files is defined by the language standard. Using the include directive does not include the corresponding standard library

library, but only allow you to insert descriptions from the specified header file into the text of the program. The library codes are connected at the linking stage, i.e. after compilation. Although the header files contain all the descriptions of the standard functions, only those functions that are used in the program are included in the program code.

After preprocessing processing, not a single preprocessing directive remains in the program text.

A program is a collection of descriptions and definitions, and consists of a set of functions. These functions should always include a function named main. Without it, the program cannot be executed. The function name is preceded by information about the type of the value returned by the function (the type of the result). If the function returns nothing, then the void type is indicated: void main (). Each function, including main, must have a set of parameters, it can be empty, then (void) is indicated in parentheses.

The body of the function is placed behind the function header. A function body is a sequence of definitions, descriptions, and executable statements enclosed in curly braces. Each definition, description, or statement ends with a semicolon.

Definitions - introduce objects (an object is a named memory area, a special case of an object is a variable) necessary to represent the data being processed in the program. An example is

int y = 10; // named constant float x; //variable

Descriptions - notifies the compiler of the properties and names of objects and functions described elsewhere in the program.

Operators - determine the actions of the program at each step of its execution

Sample C program:

#include // preprocessor directive

Control questions

1. What are the parts of a C ++ program?

2. How is a definition different from an ad?

3. List the stages of creating an executable program in C ++.

4. What is a preprocessor?

5. What is a preprocessor directive? Give examples of preprocessor directives.

6. Write a program that prints the text "My first C ++ program"

2. Basic means of the C ++ language 2.1. The composition of the language

In a text in any natural language, four main elements can be distinguished: symbols, words, phrases and sentences. Algorithmic language also contains such elements, only words are called lexemes (elementary constructions), phrases - expressions, sentences - operators. Lexemes are formed from symbols, expressions from tokens and symbols, operators from symbols of expressions and tokens (Fig. 1.1)

Rice. 1.1. Composition of the algorithmic language Thus, the elements of the algorithmic language are:

Identifiers are the names of objects of SI programs. The identifier can contain Latin letters, numbers and underscore. Uppercase and lowercase letters are different, for example PROG1, prog1 and Prog1 are three different identifiers. The first character must be a letter or underscore (but not a number). Spaces in identifiers are not allowed.

Key (reserved) words are words that have a special meaning to the compiler. They cannot be used as identifiers.

- Operation characters are one or more characters that define the action on the operands. Operations are divided into unary, binary and ternary according to the number of operands involved in this operation.

Constants are immutable values. There are integer, real, character and string constants. The compiler selects a constant as a token (elementary construction) and assigns it to one of the types by its appearance.

Separators are brackets, period, comma, whitespace characters.

2.1.1. Constants in C ++

A constant is a token that represents an image of a fixed numeric, string, or character value.

Constants are divided into 5 groups:

Wholes;

- real (floating point);

Enumerable;

Symbolic;

String.

The compiler extracts a token and assigns it to one or another group, and then internally

three groups to a certain type according to its form of recording in the text of the program and according to its numerical value.

Integer constants can be decimal, octal, and hexadecimal. A decimal constant is defined as a sequence of decimal digits that does not start with 0, unless the number is 0 (examples: 8, 0, 192345). An octal constant is a constant that always starts at 0. 0 is followed by octal digits (examples: 016 - decimal value 14, 01). Hexadecimal constants are a sequence of hexadecimal digits preceded by 0x or 0X characters (examples: 0xA, 0X00F).

V depending on the value of the integer constant the compiler will present her differently

v computer memory (i.e., the compiler will assign the corresponding data type to the constant).

Real constants have a different form of internal representation in computer memory. The compiler recognizes such constants by their appearance. Real constants can have two forms of representation: fixed point and floating point. Fixed-point constant type: [digits]. [Digits] (examples: 5.7,. 0001, 41.). Floating-point constant type: [digits] [.] [Digits] E | e [+ | -] [digits ] (examples: 0.5e5, .11e-5, 5E3). In the notation of real constants, either the integer or fractional part, or the decimal point, or the sign of the exponent with the exponent can be omitted.

Enumerated constants are entered using the enum keyword. These are ordinary integer constants to which unique and easy-to-use designations are assigned. Examples: enum (one = 1, two = 2, three = 3, four = 4);

enum (zero, one, two, three) - if you omit the = and signs in the definition of enumerated constants numerical values, then the values ​​will be assigned by default. In this case, the leftmost identifier will receive the value 0, and each subsequent one will increase by 1.

enum (ten = 10, three = 3, four, five, six);

enum (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Satur-

Character constants are one or two characters enclosed in apostrophes. Character constants consisting of one character are of type char and occupy one byte in memory, character constants consisting of two characters are of type int and occupy two bytes. Sequences starting with a \ are called control sequences, they are used:

- To represent symbols that do not have a graphical display, for example:

\ a - sound signal,

\ b - go back one step, \ n - line feed,

\ t - horizontal tab.

- To represent characters: \, ',? , ”(\\, \’, \?, \ ”).

- To represent characters using hexadecimal or octal codes (\ 073, \ 0xF5).

A string constant is a sequence of characters enclosed in quotation marks.

Control characters can also be used within strings. For example: “\ nNew line”,

“\ N \” High-level algorithmic programming languages ​​\ ””.

2.2. Data types in C ++

The data is displayed in the program the world around it. The purpose of the program is to process data. Data different types stored and processed in different ways. The data type defines:

1) internal representation of data in the computer memory;

2) a set of values ​​that can take values ​​of this type;

3) operations and functions that can be applied to data of this type.

V Depending on the requirements of the task, the programmer chooses the type for the program objects. C ++ types can be divided into simple and complex. Simple types are types that are characterized by a single value. C ++ defines 6 simple data types:

int (integer)

char (character)

wchar_t (wide character) bool (boolean) float (real)

double (double precision real)

There are 4 type specifiers that specify the internal representation and range of standard types.

short long signed

unsigned

2.2.1. Int type

Values ​​of this type are integers.

The size of the int type is not defined by the standard, but depends on the computer and the compiler. For a 16-bit processor, 2 bytes are allocated for it, for a 32-bit processor, 4 bytes.

If the int is preceded by the short specifier, then 2 bytes are allocated for the number, and if the long specifier, then 4 bytes. The amount of memory allocated for the object depends on the set of valid values ​​that the object can take:

short int - takes 2 bytes, therefore, has a range of –32768 .. + 32767;

long int - occupies 4 bytes, therefore, has a range of –2 147 483 648 .. + 2 147 483 647

The int type is the same as short int on 16-bit PCs and long int on 32-bit PCs.

The signed and unsigned modifiers also affect the set of valid values ​​that an object can take:

unsigned short int - occupies 2 bytes, therefore, has a range of 0 ..65536; unsigned long int - occupies 4 bytes, therefore has a range of 0 .. + 4 294 967

2.2.2. Char type

Values ​​of this type are members of a finite ordered set of characters. Each character is assigned a number called the character code. 1 byte is allocated for the value of the character type. The char type can be used with the signed and unsigned specifiers. The signed char data type can store values ​​in the range from –128 to 127. When using the unsigned char data type, the values ​​can range from 0 to 255. ASCII (American Standard Code foe International Interchange) is used for encoding. Characters with codes from 0 to 31 refer to service ones and have independent meaning only in I / O statements.

Values ​​of type char are also used to store numbers from the specified ranges.

2.2.3. Wchar_t type

Designed to work with a set of characters for which 1 byte is not enough, for example Unicode. This type is generally of the same size as short. String constants of this type are written with the prefix L: L “String # 1”.

2.2.4. Bool type

The bool type is called boolean. Its values ​​can be true and false. The internal form is false - 0, any other value is interpreted as true.

2.2.5. Floating point types.

The internal representation of a real number consists of 2 parts: the mantissa and the order. In IBM-compatible PCs, float values ​​occupy 4 bytes, of which one bit is reserved for the mantissa sign, 8 for the order and 24 for the mantissa.

Values ​​of double types occupy 8 bytes, 11 and 52 bits are allocated for the order and mantissa, respectively. The length of the mantissa determines the precision of the number, and the length is of the order of its range.

If there is a long specifier before the name of the double type, then bytes are allocated for the value.

2.2.6. Void type

TO basic types also include the void type. The set of values ​​of this type is

2.3. Variables

A variable in C ++ is a named memory area that stores data of a certain type. A variable has a name and a value. The name is used to refer to the memory area in which the value is stored. Any variable must be declared before use. Examples:

General view of the description operator:

[memory class] type name [initializer];

The memory class can take the following values: auto, extern, static, register. The memory class defines the lifetime and scope of the variable. If the memory class is not specified explicitly, then the compiler determines it based on the context of the declaration. The lifetime can be constant - during the execution of the program or temporary - during the block. Scope is a part of the program text from which normal access to a variable is allowed. Usually the scope is the same as the scope. Except for the case when a variable with the same name exists in the inner block.

Const - indicates that this variable cannot be changed (named constant). When describing, you can assign an initial value to the variable (initialization). Memory classes:

auto is an automatic local variable. The auto specifier can only be specified when defining block objects, for example, in the body of a function. For these variables, memory is allocated upon entry into the block and freed upon exit. Such variables do not exist outside the block.

extern is a global variable, it is located in another place of the program (in another file or further along the text). Used to create variables that are available in all program files.

static is a static variable, it exists only within the file where the variable is defined.

register - similar to auto, but memory for them is allocated in processor registers. If this is not possible, then the variables are treated as auto.

int a; // global variable void main () (

int b; // local variable

extern int x; // variable x is defined elsewhere static int c; // local static variable a = 1; // assignment to a global variable

int a; // local variable a

a = 2; // assignment to a local variable :: a = 3; // assignment to a global variable

int x = 4; // definition and initialization of x

In the example, the variable a is defined outside of all blocks. The scope of the variable a is the entire program, except for those lines where the local variable a is used. Variables b and c are local, their scope is block. The lifetime is different: the memory under b is allocated when the block is entered (because by default the memory class is auto), it is freed when the block is exited. The variable with (static) exists while the program is running.

If the initial value of the variables is not specified explicitly during the definition, the compiler clears the global and static variables. Automatic variables are not initialized.

The variable name must be unique in its scope.

The variable description can be done either as a declaration or as a definition. The declaration contains information about the memory class and the type of the variable, the definition, along with this information, instructs to allocate memory. In the example extern int x; - declaration, and the rest are definitions.

2.4. Operation signs in C ++

Operation signs provide the formation of expressions. Expressions consist of operands, operator signs, and parentheses. Each operand is, in turn, an expression or a special case of an expression - a constant or a variable.

Unary operations

& getting the address of the operand

* Addressing the address (dereferencing)

- unary minus, changes the sign of the arithmetic operand

++ Increase by one:

prefix operation - increments the operand before it is used

postfix operation increments the operand after it has been used

int a = (m ++) + n; // a = 4, m = 2, n = 2

int b = m + (++ n); // a = 3, m = 1, n = 3

decrease by one:

prefix operation - decrements the operand before it is used

postfix operation decrements the operand after it is used

calculating the size (in bytes) for an object of the type that

has an operand

has two forms

sizeof expression

sizeof (float) // 4

sizeof (1.0) // 8, since real constants by default

Algorithmic language - it is a system of notation and rules for the uniform and accurate recording of algorithms and their execution. An algorithmic language is a means for recording algorithms in an analytical form, intermediate between the recording of an algorithm in a natural (human) language and a recording in a computer language (programming language).

There is a difference between the concepts of "algorithmic language" and "programming languages". First of all, a program written in an algorithmic language is not necessarily intended for a computer. The practical implementation of an algorithmic language is a separate issue in each specific case.

Like every language, an algorithmic language has its own vocabulary. The basis of this dictionary is formed by the words used to write commands included in the command system of the executor of one or another algorithm. Such commands are called simple commands. In an algorithmic language, words are used, the meaning and way of using which is set once and for all. These words are called service. The use of function words makes the recording of the algorithm more descriptive, and the form of presentation of various algorithms is uniform.

An algorithm written in an algorithmic language must have a name. It is advisable to choose the name so that it is clear which solution to which problem the given algorithm describes. To highlight the name of the algorithm, the service word ALG (ALGoritm) is written in front of it. Algorithm name (usually with new line) write down his commands. To indicate the beginning and end of the algorithm, its commands are enclosed in a pair of service words START (START) and KON (END). The commands are written sequentially.

ALG - the name of the algorithm

series of algorithm commands

For example, an algorithm that determines the movement of a robot performer can be as follows:

ALG - in_warehouse

When constructing new algorithms, the algorithms compiled earlier can be used. Algorithms used entirely as part of other algorithms are called auxiliary algorithms. Any algorithm from the ones previously compiled can be auxiliary. It is also possible that an algorithm that itself contains a link to auxiliary algorithms may turn out to be auxiliary in a certain situation.

Very often, when compiling algorithms, it becomes necessary to use the same algorithm as an auxiliary one, which, moreover, can be very complex and cumbersome. It would be irrational, starting work, each time to re-compose and memorize such an algorithm for its subsequent use. Therefore, in practice, the so-called built-in (or standard) auxiliary algorithms are widely used, i.e. such algorithms that are constantly at the disposal of the performer. The reference to such algorithms is carried out in the same way as to the "usual" auxiliary algorithms. The robot executor has a built-in auxiliary algorithm that can move to the warehouse from any point in the working field; for the performer-programming language BASIC - this is, for example, the built-in algorithm "SIN".

The algorithm may contain an appeal to itself as an auxiliary, and in this case it is called recursive. If the command for addressing the algorithm to itself is in the algorithm itself, then such a recursion is called straight. There are cases when the recursive call of this algorithm comes from an auxiliary algorithm, to which in this algorithm there is an appeal. This recursion is called indirect. An example of direct recursion:

ALG - movement

traffic

Algorithms, during the execution of which the order of commands is determined depending on the results of checking certain conditions, are called branching. To describe them in an algorithmic language, a special compound command is used - the command branching. As applied to the robot performer, the condition may be to check whether the robot is at the edge of the working field (edge ​​/ not_ edge); checking for the presence of an object in the current cell (yes / no) and some others:

IF condition IF condition IF edge

TO series 1 TO series TO right

ELSE series2 EVERYTHING ELSE forward

The following is an algorithmic notation of the selection command, which is a development of the branching command:

WHEN condition 1: series 1

WHEN condition 2: series 2

WHEN condition N: series N

ELSE series N + 1

Algorithms, in the execution of which individual commands or series of commands are executed repeatedly, are called cyclical. To organize cyclic algorithms in an algorithmic language, a special compound loop instruction is used. It corresponds to block diagrams of the "iteration" type and can take the following form:

WHILE condition NC

series up to condition

Algorithm - an accurate and understandable instruction to the performer to perform a sequence of actions aimed at solving the task.

The name "algorithm" comes from the Latin form of the name of the Central Asian mathematician al-Khwarizmi - Algorithmi. Algorithm is one of the basic concepts of computer science and mathematics.

An algorithm executor is some abstract or real (technical, biological or biotechnical) system capable of performing the actions prescribed by the algorithm.

The performer is characterized by:

elementary actions;

command system;

The environment (or setting) is the "habitat" of the performer. For example, for a performer Robot from a school textbook, Wednesday is an endless cellular field. Walls and shaded cells are also part of the environment. And their location and the position of the Robot itself determine the specific state of the environment.

Each executor can execute commands only from some strictly specified list - the executor's command system. For each command, applicability conditions (in what states of the environment the command can be executed) must be specified and the results of command execution must be described. For example, the "up" command of the Robot can be executed if there is no wall above the Robot. Its result is a displacement of the Robot one cell up.

After calling the command, the executor performs the corresponding elementary action.

Executor failures occur when a command is invoked with an invalid state of the environment.

Usually the executor knows nothing about the purpose of the algorithm. He follows all the commands he receives, without asking the "why" or "why" questions.

In computer science, the computer is the universal executor of algorithms.

The main properties of the algorithms are as follows:

Comprehensibility for the performer - i.e. the executor of the algorithm must know how to execute it.

Discreteness (discontinuity, separation) - i.e. the algorithm should represent the process of solving the problem as a sequential execution of simple (or previously defined) steps (stages).

Definition - i.e. each rule of the algorithm should be clear, unambiguous and leave no room for arbitrariness. Due to this property, the execution of the algorithm is mechanical in nature and does not require any additional instructions or information about the problem being solved.

Performance (or limb). This property consists in the fact that the algorithm must lead to the solution of the problem in a finite number of steps.

Mass character. This means that the algorithm for solving the problem is developed in general view, i.e. it should be applicable for a certain class of problems that differ only in the initial data. In this case, the initial data can be selected from a certain area, which is called the area of ​​applicability of the algorithm.

In practice, the following forms of presentation of algorithms are most common:

verbal (natural language recordings);

graphic (images from graphic symbols);

pseudocodes (semi-formalized descriptions of algorithms in a conditional algorithmic language, including both programming language elements and natural language phrases, generally accepted mathematical notation, etc.);

software (texts in programming languages).

The verbal way of writing algorithms is a description of the sequential stages of data processing. The algorithm is set in free form in natural language.

For example. Write down the algorithm for finding the greatest common divisor (GCD) of two natural numbers.

The algorithm can be as follows:

set two numbers;

if the numbers are equal, then take any of them as an answer and stop, otherwise continue the algorithm;

determine the larger of the numbers;

replace the larger of the numbers with the difference between the larger and smaller of the numbers;

repeat the algorithm from step 2.

The described algorithm is applicable to any natural numbers and should lead to the solution of the problem. See for yourself by determining the greatest common divisor of 125 and 75 using this algorithm.

The verbal way has no widespread the following reasons:

such descriptions are not strictly formalized;

suffer from verbosity of records;

allow for ambiguity in the interpretation of individual prescriptions.

The graphical way of presenting algorithms is more compact and clear in comparison with the verbal one.

In a graphical presentation, the algorithm is depicted as a sequence of interconnected functional blocks, each of which corresponds to the execution of one or more actions.

Such graphical representation called a flowchart or flowchart.

In the flowchart, each type of action (input of initial data, calculation of expression values, checking conditions, control of repetition of actions, completion of processing, etc.) corresponds to a geometric figure represented in the form of a block symbol. Block symbols are connected by transition lines that determine the order in which actions are performed.

Table 1 lists the most commonly used symbols.

The "process" block is used to designate an action or a sequence of actions that change the value, form of presentation or placement of data. To improve the clarity of the diagram, several separate processing blocks can be combined into one block. Representation of individual operations is fairly free.

The "decision" block is used to indicate conditional control transitions. Each "solution" block must indicate the question, condition, or comparison that it defines.

The "modification" block is used to organize cyclic structures. (The word modification means modification, transformation). The loop parameter is written inside the block, for which its initial value, boundary condition and step of changing the parameter value are specified for each repetition.

The "predefined process" block is used to indicate calls to auxiliary algorithms that exist autonomously in the form of some independent modules, and for calls to library routines.

Pseudocode is a notation and rule system designed to consistently record algorithms.

It occupies an intermediate place between natural and formal languages.

On the one hand, it is close to ordinary natural language, so algorithms can be written and read in it like ordinary text. On the other hand, some formal constructions and mathematical notation are used in pseudocode, which brings the notation of the algorithm closer to the generally accepted mathematical notation.

The pseudocode does not accept the strict syntactic rules for writing commands inherent in formal languages, which makes it easier to write an algorithm at the design stage and makes it possible to use a wider set of commands designed for an abstract executor. However, pseudocode usually contains some constructs that are inherent in formal languages, which facilitates the transition from writing in pseudocode to writing an algorithm in a formal language. In particular, in pseudocode, just as in formal languages, there are function words, the meaning of which is determined once and for all. They are highlighted in bold in printed text and underlined in handwritten text. There is no single or formal definition of pseudocode, therefore, various pseudocodes are possible, differing in the set of service words and basic (basic) constructions.

An example of pseudocode is a school algorithmic language in Russian notation (school AY), described in the textbook by A.G. Kushnirenko et al. "Fundamentals of Informatics and computing technology", 1991. Hereinafter we will call this language simply" algorithmic language ".

Basic service words

General view of the algorithm:

alg algorithm name (arguments and results)

the conditions of applicability of the algorithm are given

you need the purpose of the algorithm

start description of intermediate values

| sequence of commands (algorithm body)

The part of the algorithm from the word alg to the word start is called the heading, and the part between the words beginning and the end is called the body of the algorithm.

In the al sentence, after the name of the algorithm, in parentheses, the characteristics (arg, res) and the type of the value (integer, thing, sym, lit or log) of all input (arguments) and output (results) variables are indicated. When describing arrays (tables), the service word tab is used, supplemented by boundary pairs for each index of array elements.

Examples of alg sentences:

alg Volume and area of ​​a cylinder (arg item R, H, res item V, S)

alg Roots QvUr (arg thing a, b, c, rez thing x1, x2, rez lit t)

alg Exclude element (arg integer N, arg rez thing tab A)

alg diagonal (arg cel N, arg cel tab A, res lit Otvet)

Suggestions are given and should be optional. It is recommended to write in them statements describing the state of the environment of the executor of the algorithm, for example:

alg Replacement (arg lit Str1, Str2, arg rez lit Text)

given | the lengths of the substrings Str1 and Str2 are the same

it is necessary | everywhere in the Text string substring Str1 is replaced by Str2

alg Number of maxima (arg integer N, arg real tab A, res integer K)

given | N> 0

it is necessary | K - the number of maximum elements in table A

alg Resistance (arg thing R1, R2, arg int N, res thing R)

given | N> 5, R1> 0, R2> 0

it is necessary | R - circuit resistance

Here in sentences it is given and necessary after the "|" comments are recorded. Comments can be placed at the end of any line. They are not processed by the translator, but they make the algorithm much easier to understand.

Algorithms can be thought of as some structures, consisting of separate basic (i.e. basic) elements.

Naturally, with such an approach to algorithms, the study of the basic principles of their design should begin with the study of these basic elements.

To describe them, we will use the language of algorithmic schemes and the school algorithmic language.

The logical structure of any algorithm can be represented by a combination of three basic structures:

following,

branching,

A characteristic feature of basic structures is that they have one entrance and one exit.

Algorithmic programming language- a formal language used to write, implement and learn algorithms. Unlike most programming languages, the algorithmic language is not tied to the architecture of the computer, does not contain details related to the structure of the machine.

To study the basics of algorithmization, the so-called Russian algorithmic language(school algorithmic language), using words in Russian that are understandable to the student.

An algorithm-like algorithmic language with Russian syntax was introduced into use by Academician A. P. Ershov in the mid-1980s, as the basis for a "machine-less" computer science course.

The main function words of the algorithmic language

Algorithm description

  • alg(algorithm)
  • arg(argument)
  • cut(result)
  • early(start) - the beginning of the algorithm
  • con(end) - end of the algorithm
  • given- initial data in any form
  • necessary- the goal of the algorithm

Data types:

  • intact(whole)
  • things(real)
  • Sim(character)
  • litas(literal) - string
  • log(logical)
  • tab(table) - to denote an array
  • lengths(length) - number of array elements

Condition designation

  • if
  • otherwise
  • choice
  • meaning

Cycle notation

  • nts(start of cycle)
  • kts(end of cycle)
  • while

Logical functions and values ​​for constructing expressions

Input Output

  • input
  • output

General view of the algorithm

1
2
3
4
5
6

alg algorithm name (arguments and results)
| given algorithm applicability conditions
| necessary the purpose of the algorithm
early description of intermediate values
| sequence of commands (algorithm body)
con

Part of the algorithm from the word alg to the word early called the title, and the part between the words early and con- the body of the algorithm.

In a sentence alg after the name of the algorithm in parentheses are the characteristics ( arg, cut) and value type ( intact, things, Sim, litas or log) of all input (arguments) and output (results) variables. When describing arrays (tables), a special word is used tab, supplemented with boundary pairs for each index of the array elements.

Algorithm record keywords usually underlined or in bold. To highlight logical blocks, indentation is applied, and paired words of the beginning and end of the block are connected with a vertical line.

Basic algorithmic structures

A detailed description of the main algorithmic structures is given in this article. Below are the templates for composing these structures in an algorithmic language.
Incomplete fork

| if condition
| | then actions
| all

Full fork

1
2
3
4
5

| if condition
| | then action 1
| | otherwise action 2
| all

Branching

1
2
3
4
5
6
7
8

| choice parameter
| | at meaning value 1
| | | action 1
| | at meaning value 2
| | | action 2
| | otherwise
| | | default actions
| all

Loop with precondition

| nc bye condition
| | actions
| kts

Loop with postcondition

Most often, instructions are written in an algorithmic language. It is necessary for the precise prescription of all steps and their execution. There are clear differences between the school algorithmic language and programming languages. As a rule, not only a computer acts as a performer in the first version, but also another device that is capable of performing work. Any program written in an algorithmic language does not have to be done by technology. Implementation of all instructions in practice is a purely separate issue. Below will also be considered a description of the algorithm in algorithmic language. It will help you understand the structure of this system.

Studying at school

Algorithmic language is often taught in schools, best known as teaching language. It has become widespread due to the fact that it uses words that are most understandable to any student. A similar language with syntax in Russian was introduced a long time ago, namely in the mid-1980s. It was used to provide a foundation for schoolchildren and teach them a computer science course without a computer. Published given language was in 1985 in one of the textbooks. It was also reprinted several times for special books that were intended for teaching in grades 9 and 10. The total circulation of the publication was 7 million copies.

Algorithm recording sequence

First of all, you need to write down the ALG combination of letters. The name of the algorithm follows. Then, after the NACH, you need to describe a series of commands. The KOH operator means the end of the program.

Description of the algorithm in algorithmic language:

ALG Company

START

turn 90 degrees to the left

KOH

When writing, keywords must be underlined or in bold. To indicate logical blocks, you should use indentation, and if there are paired words of the beginning and end, you must use the vertical bar, which denotes the connection.

Compilation of algorithms

Old notes can be used to compose new instructions. Such instructions are called auxiliary instructions. A similar algorithm can be any of all those described previously. It is also possible that in this system an additional algorithm will be applied, which itself received a reference to auxiliary systems.

Often, when creating an instruction, there is a need to use only one algorithm as an additional one. This is why recordings can often be complex and cumbersome. But it is worth noting that the ability to make a reference is easier than rewriting the same records several times.

That is why, in practice, a standard auxiliary algorithm is often used, which is constantly subordinate to the user. The instruction can have a reference, both to itself and to anyone else. Algorithmic language commands are designed for such actions. Such instructions are called recursive.

The self-bind command is within the system itself. This recursion is straight forward. An indirect one is one where the algorithm is called in any other auxiliary instruction.

Algorithms that have a certain order of instructions can constantly change depending on the results of the execution of special parts of the program. Such systems are called branching systems. In order to create them, you need to use a special branching command. It has an abbreviated and complete spelling scheme. Cyclic algorithms are not uncommon that execute specific commands multiple times.

E-workshop

In order to improve the study of theory in the grammatical language, the professionals of the Faculty of Mechanics and Mathematics of Moscow State University in 1985 created a special compiler. It was named "E-workshop". With its help it was possible to enter, modify and execute programs. The following year, a specific set of performers was released. It is about "Robot", "Draftsman", "Two-legged", "All-terrain vehicle". This made it possible to implement the algorithms simply and easily. This compiler has become very popular and has been used on some computers. Enough long time this programming language has been refined and changed. In 1990, a later version of it appeared in a textbook.

Idol

Now the school algorithmic language is experiencing its rebirth, after a special package "Idol" was developed for Windows and Linux. The system works with several performers. Classic among them are "Robot", "Draftsman". The same package is included in setup file Linux "School". This system was developed specially by order of the Russian Academy of Sciences. It is distributed free of charge and freely. Over the past few years, the described language has been actively proposed to be used in the Unified State Exam as one of the

Language assignment

Algorithmic language is used to solve a fairly wide range of problems. It is suitable for mastering both math and exercises in other subjects. It should be noted that it is also used to make it easier for schoolchildren to study similar topics.

Differences between machine and algorithmic languages

The most famous representative of machine-dependent languages ​​is "Assembler". During programming on it, a person must clearly indicate to the translator, thanks to special operators, which memory cells should be filled or transferred. Since the syntax of "Assembler" is as close as possible to the computer form of writing, it is quite difficult to study it. That is why algorithmic language is taught at school, as well as at the beginning of teaching programming in the first year of higher education.

Standard functions

Algorithmic language has special standard functions which have received the status of "built-in". It is thanks to them that you can easily write many operations with numbers and expressions without performing routine entries. The algorithmic language program is quite simple. Standard functions can allow you to calculate square root, logarithms, modulus, and so on. The most popular built-in methods are as follows:

  • absolute module abs (X);
  • square root sqrt (X);
  • natural and ln (X), lg (X);
  • minimum and maximum min (X, Y), max (X, Y);
  • trigonometric functions sin (X), cos (X), tg (X), ctg (X).

Thanks to this, any programmer or just a person who learns to work with an algorithmic language can easily write a mathematical problem without resorting to the invention of the bicycle. Thus, it should be noted that this language is quite user-friendly. It is simple to understand and also as easy to understand as possible. No wonder he was included in the school curriculum. Schoolchildren are happy to study it.



Did you like the article? Share it