Executor Control Robot in the KUMIR system

The robot exists in a certain environment (rectangular checkered field). Walls can be located between some cells of the field. Some cells may be shaded (Fig. 3.11).

The robot occupies exactly one cell of the field.

On commands up, down, left and right, the Robot moves to the next cell in the specified direction. If there is a wall on the way, then a failure occurs - a message is displayed about the impossibility of executing the next command.

On command to paint over, the Robot paints over the cell in which it stands. If the cell has already been painted over, it will be painted over again, although no visible changes will occur.

The robot can only execute correctly written commands. If you write down instead of the command down, then the Robot will not understand this entry and will immediately report an error.

O
errors: 1 syntactic; 2. logical

Scene descriptions are stored in text files special format (.fil format).

Current- the environment in which the Robot is in this moment(including information about the position of the Robot).

Home- the environment in which the Robot is forcibly placed at the beginning of the execution of the program using the Robot.

Operating procedure:


  1. Ask starting environment according to the task:
Menu Tools → Change the starting environment of the Robot (draw the environment according to the task condition, give a name, save in the Personal folder)

2. Specify the Contractor:

Insert Menu →Use Robot

3. Write an algorithm for solving the problem.

4. Run the algorithm (Menu Run → Run continuously / F9)

The system of commands of the executor Robot in the KUMIR system


Team

Action

up

The robot moves up 1 cell

way down

The robot moves down 1 cell

to the left

The robot moves 1 cell to the left

right

The robot moves 1 cell to the right

paint over

The robot paints the cell in which it is

right free

The robot checks the execution of the corresponding simple terms

left free



top loose



bottom loose



the cell is shaded



cage clean



Cyclic algorithms

Cycle organization of repeating actions until a certain condition is true .

Loop body - a set of repeatable actions.

Condition - boolean expression (simple or complex (compound))
Cycle types:

1.Loop "Repeat n times" 2. Loop "Bye"
nc n times nts bye
. . Loop body. . Loop body
kts kts

Example: nts bye right free


General view of the cycle "Repeat n times:

REPEAT n TIMES

THE END
kts

General view of the while loop:

WHILE TO DO

THE END
Compound conditions formed from one or more simple conditions and service words AND, OR, NOT.


Compound condition A and B(where A, B are simple conditions) is satisfied when each of the two simple conditions included in it is satisfied.

Let A - free on top AT - free on the right then the compound condition A and B- free on top AND free on the right.


Compound Condition A OR B is satisfied when at least one of the two simple conditions included in it is satisfied: top free OR right free
Compound condition NOT A- met when condition A is not met.

Example: Let A be a shaded cell (simple condition).

P Checking the compound condition NOT A:

a) A - done, NOT A (NOT shaded) - not done.

b) A - not done, NOT A (NOT shaded) - done.


Branch command

Branching - a form of organization of actions in which, depending on the fulfillment or non-fulfillment of a certain condition, either one or another sequence of actions is performed.

General view of the IF command:

IF THEN OTHERWISE

THE END

In the KUMIR language:

Full branching: Partial branching:
if then if then

otherwise

all all

Helper Algorithm- an algorithm that solves some subproblem of the main problem.

In the KUMIR system, auxiliary algorithms are written at the end of the main program (after the service word con) are called for execution in the main program by name.

AT surveys and assignments

1. Give all the algorithms of the three commands that will move the Robot from its original position to cell B.

Is there an algorithm for this task, during which the Robot does:

a) two steps b) four steps; c) five steps; d) seven steps?


  1. Petya made an algorithm that transfers the Robot from cell A to cell B with some cells painted over. What should Kolya do with this algorithm in order to obtain an algorithm that takes the Robot from B to A and fills in the same cells?


7. Two auxiliary robot algorithms are known

Draw what happens when the Robot performs the following basic algorithms:


a)

nc 5 times


pattern_1

right; right;


b)

nc 7 times


pattern_2

right; right


in)
right; right; right

up; up

right; right; right

way down; way down


G)
right; right
right; right

8. Create algorithms under which the Robot will paint over the specified cells:



9. It is known that somewhere to the right of the Robot there is a wall. Make up an algorithm, under the control of which the Robot will paint over a number of cells up to the wall and return to its original position.

10. It is known that somewhere to the right of the Robot there is a shaded cell.

FROM leave the algorithm, under the control of which the Robot will paint a number of cells up to the shaded cell and return to its original position.

11. It is known that the Robot is located near the left entrance to the horizontal corridor.

12. It is known that the Robot is somewhere in the horizontal corridor. None of the cells of the corridor is painted over.

Make up an algorithm, under the control of which the Robot will paint over all the cells of this corridor and return to its original position.


13. In a row of ten cells to the right of the Robot, some cells are shaded.

FROM leave the algorithm that paints the cells:

a) below each shaded cell;

b) above and below each shaded cell.


14. What can be said about the correctness of the following fragment of the algorithm?

nts bye the cell is shaded

IF right free THEN

right; paint over

to
c

15. Write a program with which the Robot can get to cell B in all three mazes.


16. Write a program, following which the Robot will be able to go along the corridor from the lower left corner of the field to the upper right. The corridor has a width of one cell and stretches in the direction from left-bottom-right-up. An example of a possible corridor is shown in the figure.

W

adachi GIA


  1. Corridor1. The robot is somewhere in the vertical corridor. None of the cells of the corridor is painted over. Create an algorithm under which the Robot will paint over all the cells of this corridor and return to its original position.

  1. To
    Necessary

    Given
    corridor2. The robot is located in the upper cell of a narrow vertical corridor. The width of the corridor is one cell, the length of the corridor can be arbitrary.

A possible variant of the initial location of the Robot is shown in the figure (the Robot is indicated by the letter "P")

Write an algorithm for the Robot that fills all the cells inside the corridor and returns the Robot to its original position. For example, for the above picture, the Robot should paint over the following cells (see picture):


  1. There is a long horizontal wall on the endless field. The length of the wall is unknown. The robot is in one of the cages directly above the wall. The initial position of the Robot is also unknown. One of the possible positions:
H


Necessary

Given
Write an algorithm for the Robot that paints all the cells above and adjacent to the wall, regardless of the size of the wall and the initial position of the Robot. For example, for the given drawing, the Robot must paint over the following cells:

The final position of the Robot can be arbitrary. When executing the algorithm, the Robot should not be destroyed.



  1. There is a long vertical wall on the endless field. The length of the wall is unknown. The robot is in one of the cages located directly to the right of the wall. The initial position of the robot is also unknown. One of the possible positions of the robot is shown in the figure (the robot is marked with the letter “P”): Write an algorithm for work that paints over all the cells adjacent to the wall: on the left, starting from the top unpainted and through one; on the right, starting from the bottom shaded and through one. The robot must paint over only the cells that satisfy this condition. For example, for the above figure, the robot must fill in the following cells (see figure): The final location of the robot can be arbitrary. The algorithm must solve the problem for an arbitrary wall size and any valid initial position of the robot. When executing the algorithm, the Robot should not collapse.


Write an algorithm for the Robot that paints all cells located to the left of the vertical wall and above the horizontal wall and adjacent to them. The robot must paint over only the cells that meet this condition. For example, for the above picture, the Robot must paint over the following cells (see picture).


H write an algorithm for the Robot that paints the cells adjacent to the wall, from above and below, starting from the left and through one. The robot must paint over only the cells that meet this condition. For example, for the given figure a) the Robot must paint over the following cells (see Fig. b).

The final position of the Robot can be arbitrary. The algorithm must solve the problem for an arbitrary wall size and any valid initial position of the Robot.



R

  1. There is a long vertical wall on the endless field. The length of the wall is unknown. The robot is in one of the cages located directly to the left of the wall. The initial position of the robot is also unknown. One of the possible positions of the robot is shown in the figure (the robot is marked with the letter "P"):
Write for work an algorithm that paints over all the cells adjacent to the wall:

  • all on the left;

  • on the right, starting from the top unpainted and through one.
The robot must paint over only the cells that meet this condition.

B
1102_GIA2011

There are two horizontal walls on the infinite field. The length of the walls is unknown. The distance between the walls is unknown. The robot is located above the bottom wall in a cage located at its left edge. Write an algorithm for the Robot that paints all the cells located above the bottom wall and below the top wall and adjacent to them. The robot must paint over only the cells that meet this condition. For example, for the above drawing, the robot must fill in the following cells (see figure):

The final location of the robot can be arbitrary. The algorithm must solve the problem for an arbitrary field size and any admissible location of walls inside a rectangular field. When executing the algorithm, the Robot should not collapse.


AT
1103_GIA_2011


There is a horizontal wall on the infinite field. The length of the wall is unknown. From the right end of the wall, a vertical wall extends downward, also of unknown length. The robot is located above a horizontal wall in a cage located at its left edge. The figure shows one of possible ways the location of the walls and the Robot (the Robot is indicated by the letter "P").

Write an algorithm for the Robot that paints all the cells located above the horizontal wall and to the right of the vertical wall and adjacent to them. The robot must paint over only the cells that meet this condition. For example, for the above picture, the Robot must paint over the following cells (see picture).

Khiryanov Timofey Fedorovich

if-then-else-all

General view of the command:

if condition
then series 1
aka series 2
all

Series 2 along with the official word otherwise may be missing. In this case, the command looks like:

if condition
then series 1
all

When executing the command if Kumir checks first condition recorded between if and then. If this condition is met, series 1, otherwise - series 2(if it exists), after which KuMir proceeds to execute the commands recorded after the word all.
If the condition is not met, and series 2, together with otherwise, is absent, then KuMir immediately proceeds to execute the commands written after the word all.

Example 1

if a then
b:=b-a
p:=p+q
otherwise
a:=a-b
q:=q+p
all

Example 2

if x > m
then
m:= x
n:= n+1
all

Example 3 (Robot performer)

if the right is free
then
right
all

choice-if-otherwise-all

General view of the command:

choice
under condition 1: series 1
under condition 2: series 2

condition n: series n
otherwise series n+1
all

Keyword otherwise along with the corresponding series of commands may be missing:

choice
under condition 1: series 1
under condition 2: series 2

condition n: series n
all

Kumir checks first condition 1. If it is observed, then KuMir executes commands from series 1, after which it proceeds to execute the commands written after the word all. Otherwise KuMir does the same with condition 2 and teams from series 2 etc.
Commands written after the word otherwise are executed if none of the conditions are met.
In a select command, at most one series of commands is always executed, even if several conditions are true. The execution of the select command ends after the first (in order) condition with the value yes is found (and the corresponding series of commands is executed).

Example 1

choice
for a > 1: i:= i + 1
for a< 0: j:= j - 1
otherwise t:= i; i:= j; j:= t
all

Algorithm for drawing a spiral:

use Drawer
alg
early
. move to a point(3,3)
. lower the pen
. coil(1); coil(3); coil(5); coil(7); coil (9)
. raise the pen
con
alg turn(arg w)
early
. shift by vector(a, 0)
. shift by vector(0, -a)
. shift by vector(-a-1.0)
. shift by vector(0, a+1)
con

Pay attention to the command block:

Coil(1); coil(3); coil(5); coil(7); coil (9)

Auxiliary algorithm "coil (arg thing a)" is called 5 times, but it cannot be called in the "N times" loop, because each time it is called with different values argument.

But you can see that the values ​​of the argument change from 1 to 9, each time increasing by 2. So, we can help loop with counter. Also, such a cycle is called the "for" cycle.

Loop with counter- a loop in which some variable changes its value from a given initial value to a final value with some step, and for each value of this variable, the body of the loop is executed once.

Typically, this loop is used if you need to iterate over some values ​​and perform some actions for each of them.

General view of the cycle with a counter:

nc for<счетчик>from<нач. знач.>before<кон. знач.>[step<знач.>]
<тело цикла (последовательность команд)>
kts

It is not necessary to indicate the step, if it is not specified, then it is considered equal to one.

Now we can rewrite the "spiral" algorithm in this way:

use Drawer
alg
early
. move to a point(3,3)
. lower the pen
. whole size
. nc for size 1 to 9 step 2
. . coil(size)
. kts
. raise the pen
con
alg turn(arg w)
early
. shift by vector(a, 0)
. shift by vector(0, -a)
. shift by vector(-a-1.0)
. shift by vector(0, a+1)
con

In this example, the counter variable "size" will receive the values: 1, 3, 5, 7, 9. That is, loop will be executed 5 times. For each value of the “size” variable, the loop body will be executed once, in our example this is a call auxiliary algorithm"coil(arg thing a)".

Before the first use of a variable, it must be declared, i.e., what type it is. This is done in our program in the line “integer size”, i.e. we indicate that we will use the “size” variable to store integers, and therefore we need to allocate memory for it. We will talk more about variables a little later.

The block diagram of such an algorithm looks like this:

Let's look at another example:

Let's first remember and write an auxiliary algorithm that will draw a square at the point (x, y). For a change for drawing, we will use the command shift by vector(in the previous examples, they were shifted to a point).

The algorithm could be like this:

alg square(arg x, y, side)
early
. move to a point(x, y)
. shift by vector(-side/2, side/2)
. lower the pen
. shift by vector(side, 0)
. shift by vector(0, -side)
. shift by vector(-side, 0)
. shift by vector(0, side)
. raise the pen
con

Using such an auxiliary algorithm, we draw the following figure:

To do this, we use the "for" loop. Study the sample program:

use Drawer
alg figure1
early
. integer z
. nc for z from 2 to 10 step 2
. . square(0, 0, z)
. kts
con
alg square(arg x, y, side)
early
. move to a point(x, y)
. shift by vector(-side/2, side/2)
. lower the pen
. shift by vector(side, 0)
. shift by vector(0, -side)
. shift by vector(-side, 0)
. shift by vector(0, side)
. raise the pen
con

In this example, the variable "z" will receive the values: 2, 4, 6, 8, 10. That is, loop will be executed 5 times. For each “z” value, the loop body will be executed once, in our example, this is a call to the auxiliary square algorithm.

Before the first use of a variable, it must be declared, i.e., what type it is. This is done in our program in the line "integer z", i.e. we indicate that we will use the variable "z" to store integers, and therefore we need to allocate memory for it. We will talk more about variables a little later.

As you noticed, the algorithm used not only numbers, but also algebraic expressions, formulas, for example "-side/2". In computer science, these expressions are called arithmetic. The rules of the language allow, when writing algorithms, wherever you can write a number, write an arbitrary arithmetic expression.

1. Introduction

the system "KuMir" (the name comes from the words "Set of Educational Worlds"), with which this electronic version of the textbook will introduce you.
The developers of the "KuMir" language pursued the goal of creating a simple language for the initial course of computer science that meets modern programming technology and allows production use. The school algorithmic language was taken as a basis. The language was supplemented with some features that turn it from educational to production. The language has:
types whole, thing, lit; the traditional set of operations on data of these types (including operations on strings and standard set mathematical functions);
arrays ( tab) specified types; structural control structures of cycles, branching, etc.
Kumir is open - connecting external executors enriches the language with new features: from database management and working with geometric objects to expanding the set of valid numeric types (in this case, the language will allow mixing new types with existing numeric types in expressions).
Modern technology Programming teaches us to break a program not only into subprograms, but also into larger units: sets of programs that work on common data. AT different languages programming, such units are called differently, in KuMir such a unit is called "Executor". The concept of a performer is extremely important in practical work, and should be introduced at the earliest possible stages of training.
The experience of using KuMir in teaching and for developing educational software has shown that the language is easy to learn and at the same time powerful enough to expand a wide class of production tasks.
Like the E-workshop, KuMir is an integrated system that includes text editor, an incremental compiler with zero response time, as well as a simple and convenient debugger. good name for a system of this kind - "Editor-Compiler": while you are entering your program, the compiler processes it, and at any moment the program is ready to be executed without the slightest delay.

2. Names and types of value. KuMir Operations

Any characters of the Russian and Latin alphabets, as well as numbers, can be used in writing variable names. The name must not start with a number. There are no strict restrictions on the length of names in the KuMir system, but for ease of editing and to avoid overflowing lines, variables and algorithms should not be given too long names. Usually the name is chosen so that you can understand what the algorithm is intended for. When editing programs, it should also be remembered that Russian and Latin letters, similar in spelling, are distinguished by computers. For example, if when describing a variable named A, the user typed "A" in the Latin alphabet, and in the text of the algorithm he tries to access this variable by typing its name in the Russian alphabet, then in this line the "name is not defined" message will appear in the "fields" .
AT algorithmic language The KuMir programming system uses three types of values: integers ( whole), real ( thing) and lettered ( litas).
Value type- defines the set of values ​​that a value can take, and the set of actions that can be performed with this value.
Value is a single information object that has a name, a value, and a type.

A constant value (constant) does not change its value during the execution of the algorithm.
A variable can change its value during the execution of the algorithm.
Expression- a record that defines the sequence of actions on values. An expression can contain constants, variables, operation signs, functions.
The following symbols are used to write expressions in Kumir:

To indicate signs logical operations symbols are used:
= equal;
< >not equal;
< меньше;
> more;
< = меньше или равно;
> = greater than or equal;
For the record difficult conditions operations such as: And, OR NO.
AND -
simultaneous fulfillment of the above conditions (Х > 0 and Х< = 2);
OR- fulfillment of at least one of the conditions (X > 0 or Y > 0);
NOT- denial.

3. Built-in functions of the KuMir language

Here is an example of built-in functions:

Appeal

Function

Types

Argument

Functions

SIN(x) cos(x) T. G. (x) EXP(x) LN(x) ABS (x) SQRT(x) MOD (A,b) int(x) PI

sine x cosine x tangent x
exponent x (ex) nature. logarithm x
module x
square root x
remainder of division (A to B)
the integer part of number
the number "pi" - 3.14159

thing
thing
thing
thing
thing
thing
thing
thing, target
thing
thing

thing
thing
thing
thing
thing
thing
thing
whole
whole
no arguments

An example of writing arithmetic expressions in an algorithmic language:

4. INPUT / OUTPUT commands

It is often required to organize an exchange of information ("dialogue") between a person and a computer in the process of executing an algorithm. To do this, the algorithmic language has special commands for OUTPUT information from the computer memory to the screen and INPUT information from the keyboard (from a person) into the computer memory.
ENTER command - a command by which the values ​​of variables are set through input devices (keyboard).
OUTPUT Command- a command by which the value of the value is reflected on the output device of the computer (monitor screen).
Since values ​​are used in the algorithmic language to store information, the input / output commands indicate the names of the quantities whose values ​​\u200b\u200bare to be displayed (showed on the screen) or entered (remembered in the computer memory).
Example:

Official word NS (new line) tells the computer that the information should be output on a new line.

5. Assignment command. Creation and editing of linear structure programs

In order to remember or change the value of a quantity, there is a special command in the algorithmic language - assignment instruction, which is written as:

VALUE NAME: = EXPRESSION

The sign ":=" (a colon followed by an equal) is called a sign assignments and is read as "assign" (for example, the command "n:=e" reads "n assign e"). When executing an assignment command, the computer first calculates the expression written on the right side (replacing the names of the quantities with their values), and then writes the resulting value of the expression into memory.

Algorithms that are a simple sequence of actions are called linear structure algorithms.
Consider the creation process linear algorithm on the example of the calculation of the expression:
1. Calculate the sum of two numbers
2. Write a program for finding the hypotenuse of a right triangle given two legs
3. Find the volume of a cube if its side is known

6. Creating and editing branching structure programs

Problem solving cannot always be represented as a linear algorithm. There are tasks in which it is required to organize the choice of performing a sequence of actions depending on any conditions. Such algorithms are called branching structure algorithms. In the KuMir programming system, to create an algorithm for a branching structure, the constructions "IF - THEN - ELSE - ALL" and "CHOICE - IF - ALL" are provided.

Branch command: IF - THEN - ELSE - ALL

Branch command - splits the algorithm into two paths depending on some condition; then the execution of the algorithm goes to a common continuation. Branching is complete and incomplete.

Graphic scheme of the construction " if"

Service words "if", "then", "otherwise" have the usual meaning. The word " all" means the end of the construction. Between " then" and " otherwise" - in one or more lines - a sequence of commands of the algorithmic language is written (series 1). Between " otherwise" and " all"another sequence of commands is recorded (series 2). Series 2 together with the service word " otherwise" may be missing. When executing the " if"The computer first checks the condition written between " if" and " then". As a result of the check, either YES, or NO. If possible YES, then SERIES 1 is executed, and if NO, - then SERIES 2 (if any) .
If the condition is not met (it will turn out NO), and series 2 along with " otherwise" is absent, then the computer immediately proceeds to execute the commands written after the word " all".

7. Types of cycles in the KuMir programming system

Algorithms whose individual actions are repeated many times are called algorithms of a cyclic structure. The set of actions of the algorithm associated with repetition is called a cycle.
Loop Command provides repeated execution of a sequence of commands (loop body) according to some condition.
For programming algorithms of a cyclic structure in the KuMir programming system, two types of loops are provided: a loop with a precondition (loop for while) and a loop with a parameter (loop for).

Loop with precondition (while loop)

A loop with a precondition (while loop) is a loop whose execution is repeated while the loop condition is true. Service words NC(beginning of cycle) and KC(end of the cycle) are written strictly one under the other and connected vertical bar. To the right of this line, a repeated sequence of commands (loop body) is written.

When it is executed, the computer cyclically repeats the following actions:
a) checks written after the word bye condition;
b) if the condition is not met (the condition is false), then the execution of the loop ends and the computer starts executing the commands written after KC. If the condition is met (the condition is true), then the computer executes the loop body, checks the condition again, and so on.
If the condition in the loop bye is not observed from the very beginning, then the body of the loop is never executed.
Comment. Loop execution bye may not terminate if the condition is true all the time (this situation is called a loop). Therefore, in order to avoid such situations, the body of the loop should contain commands for changing the condition.

Given a positive integer N. Calculate the factorial of this number: N! = 1 * 2 * 3 * ... * N.

Loop with parameter (loop for)

Loop with parameter(loop for) - repeated execution of the loop body while the integer parameter runs through the set of all values ​​from the initial (i1) to the final (in):

Here i is a variable of an integer type, called a loop parameter: i1, in are the initial and final values ​​of the loop parameter, which can be specified either by arbitrary integers or by expressions with integer values; h - step of changing the value of the cycle parameter, the value of the step can be any integer (both positive and negative). The entry "step h" in the first line may be absent altogether, while the default value of the step is 1.
When executing a loop for, its body is executed for i = i1, i = i1 + h, i = i1 + 2*h, . . . , i = in. The rules of the algorithmic language allow specifying any integers i1, in, h. In particular, in may be less than i1. If, in addition, the value of h< 0, то цикл выполняется нужное количество раз, а если h имеет положительное значение, то этот случай не считается ошибочным - просто тело цикла не будет выполнено ни разу, а ЭВМ сразу перейдет к выполнению команд, записанных после KC. For h = 0, looping occurs.

Example: Given a positive integer N. Calculate the factorial of this number: N! = 1 * 2 * 3 * ... * N.

8. Algorithms for recurrent expressions

In mathematics and computer science, there are often sequences in which each subsequent term is calculated through the previous ones.
AT arithmetic progression, for example, each next term is equal to the previous one, increased by the difference of the progression:

ai =ai-1 +d

In sequence 1, 1, 2, 3, 5, 8, 13, ... ( it's called the Fibonacci sequence) each next term is equal to the sum of the two previous ones. For this sequence

ai = ai-1 + ai-2 , a1 = a2 =1

Formulas that express the next member of a sequence in terms of one or more previous members are called recurrent relations.

9. Table values ​​​​and work with them

To record algorithms that work with a large amount of information, in the algorithmic language there are special tabular values ​​called tables (arrays).
Table values ​​are made up of other values, usually integers or real values, called elements. Elements in the table can be arranged in different ways. The algorithmic language of the KuMir programming system uses the 2 most common types of tables: linear and rectangular tables.

Working with linear tables (one-dimensional arrays)

Like any value linear table occupies a place in the computer memory, has a name, value and type. KuMir uses tables of integer (celtab) and real (vehtab) types. For example:

The record celtab A [ 1: 5 ] means that the value A is a table (tab) consisting of integers (integers), the elements of this table have indices from 1 (lower limit) to 5 (upper limit). The value of A is five integers: 3, 15, 0, -10.101.
Table elements do not have separate names. To designate the i-th element of table A, the record A [ i ] is used. For example, when executing the command A [ 3 ] : = A [ 2 ] + A [ 4 ] the computer will substitute instead of A [ 2 ] and A [ 4 ] the values ​​of the 2nd and 4th elements of table A, i.e. the number 15 and -10, add them up and assign the resulting value to the 3rd element, thus, instead of 0, the value 5 will appear instead of 0 in the place of the 3rd element in the table.
Any (both positive and negative) integers, as well as 0, can be used as table border values. The value of the lower border must be less than the value of the upper border, if they are equal, the table is considered to consist of one element. If in the description of the table, due to a typo, the lower bound turns out to be greater than the upper one, for example, celtab [ 3: 1], then this will not be considered an error, and when entering the algorithm, no messages will appear in the "fields". In this case, it will be assumed that this table does not have a single element, and the first time this table is accessed, the message "bad index" will appear.

A task.

Working with rectangular tables (matrices)

Like a linear table, a matrix takes up space in the computer's memory, has a name, a value, and a type. KuMir uses tables of integer (celtab) and real (vehtab) types.

The notation celtab A [ 1: 5, 1:2 ] means that the value A is a table (tab) consisting of integer (integer) numbers, the elements of this table have indices from (first column, first row) to (last column, last line). The value of A is ten integers: 3, 15, 0, -10, 101, 200, -45, 50, 10, 222.
Table elements do not have separate names. To designate the i-th element of table A, the record A [ i, j ] is used. For example, when executing the command A [ 3, 1 ] : = A [ 2, 1 ] + A [ 4, 1 ] the computer will substitute instead of A [ 2, 1 ] and A [ 4, 1 ] the values ​​of the 2nd and 4th elements of the first column of table A, i.e. the numbers 15 and -10, will add them up and assign the resulting value to the 3rd element in the first row, so that instead of 0, the value 5 will appear instead of 0 in place of the 3rd element of the first row in the table.
Any (both positive and negative) integers, as well as 0, can be used as table border values. The value of the lower border must be less than the value of the upper border, if they are equal, the table is considered to consist of one element. If in the description of the table, due to a typo, the lower bound turns out to be greater than the upper one, for example, celtab [ 3: 1, 5: 2], then this will not be considered an error, and no messages will appear in the "fields" when entering the algorithm. In this case, it will be assumed that this table does not have a single element, and the first time this table is accessed, the message "bad index" will appear.

A task. In the given table B, determine the index and value of the maximum element.

Appendix: Tasks

Linear Algorithms

Task #1

Find the sum of two numbers - a and b
Solution:

alg sum
early thing a, b, c
conclusion "enter the value of 2 numbers"
input a, b
c:= a + b
conclusion ns , "sum of numbers",a,"and",b,"equal to",c
con

Task #2

Find the difference of two numbers
Solution:

alg difference
early thing a, b, c
conclusion " enter variable values ​​"
input a, b
c:= a - b
output ns, " difference of numbers",a,"and",b," equals",c
con

Task #3

Find the product of any two natural numbers
Solution:

whole a, b, c

alg work
early
conclusion
" enter two numbers"
input
a,bc:=a +b
output ns,
"
product of numbers",a,"and",b"equals",c
con

Task #4

Find the quotient of two natural numbers
Solution:

thing a, b, c

alg private
early
conclusion "
enter dividend and divisor"
input
a, b
c:= a / b
output ns,
"quotient",a,"and",b,"equal to",c
con

Task #5

Find the arithmetic mean of five arbitrary numbers
Solution:

thing a, b, c, d, e, f

alg arithmetic
start things a, b, c, d, e, f
conclusion "Enter any 5 numbers"
input a, b, c, d, e
f:=(a + b + c + d + e)/ 5
conclusion ns ," the arithmetic mean of 5 numbers is", f
con

branching

Task #1

Find the largest among 3 integers (numbers are arbitrary)

alg maximum
early target a B C
conclusion "Enter three random numbers"
input a B C
if a>b>c
then the conclusion is ns ," the maximum number is", and
all
if a<б>With
then the conclusion is ns ," the maximum number is", b
all
if a<б<с
then the conclusion is ns ," the maximum number is", with
all
con

Task #2

You are given two arbitrary numbers. If the first number is greater than the second, then assign their sum to it, and their product to the second number. If the second number is greater than the first, then assign their product to the first number, and their sum to the second.

alg condition
start things a, b
conclusion "Enter two numbers"
input a, b
if a > b
then a:= a + b
b := a * b
otherwise a:= a * b
b := a + b
conclusion ns , a, b
con

Task #3:

Find among 4 arbitrary numbers the minimum
Solution:

alg minimum
start things a, b, c, e
conclusion "Enter 4 random numbers"
input a, b, s. e
if a>b>c>e
then the conclusion is ns ," maximum number-",and
all
if a<б>c>e
then the conclusion is ns ,"maximum number -",b
all
if
a<б<с>e
then the conclusion is ns ," maximum number -", s
all
if
a<б<с<е
then the conclusion is ns , "maximum number -", e
all
con

Task #4

Given 2 legs (2 cm and 2 cm) of an isosceles triangle and its base (2.82 cm). Determine if the triangle is a right triangle.
Solution:

alg triangle
start things i, h, s
i:= 2
h:= 2
c:= 2.82
if c**2= (i**2)+(h**2)
then the output "truth"
otherwise output " False"
all
con

Task #5

Print the message "true" if the product of two negative numbers is greater than zero, otherwise print the message "false"
Solution:

alg negative
start things i, h, s, m
conclusion "Enter two negative numbers"
input i h
c:=0
m := i*h
if m>s
then the conclusion is ns , "true"
otherwise output ns ,"False"
all
con

Loop "for"

Task #1

Find Factorial natural numbern ( Factorial of a natural number n is the product of all natural numbers between 1 and n ) Solution:

alg factorial
start things
a. b
whole
n, and
conclusion

input
n
a:= 1
nc for
and from 1 before n
conclusion ns,
" enter the number"
input
b
a:= a * b
kts
conclusion ns
,
"factorial",n,"of integers is", and
con

Task #2

Find the maximum value amongn - integers
Solution:

alg maximum
start things
a, b
whole
i, n
conclusion
"Enter the number of integers to compare"
input
n
a:=0
nc for
and from 1 before n
conclusion ns,
"enter the number"
input
b
if
b>a
then
a:=b
all
kts
conclusion ns,
"the maximum number among the data is a number", and
con

Task #3

Find amongn-integers number of negative
Solution:

alg coincidence
start things
a, b, c
whole
n, i, s
conclusion "
enter the number of natural numbers"
input
n
conclusion ns,
"enter the number"
input
b
n:= n - 1
h:= 0
nc for
and from 1 before n
conclusion ns,"
enter the number"
input
With
if
c = b
then
s:= s + 1
all
kts
conclusion ns
,

con

Task #4

Sequentially entered n-integers. Find the number of matches with the first number
Solution:

start things a, b, c
whole
n, i, s
conclusion
"Enter the number of natural numbers"
input
n
conclusion ns,
"enter the number"
input
b
n:= n - 1
h:= 0
nc for
and from 1 before n
conclusion ns,
" enter the number"
input
With
if
c = b
then s:= s + 1
all
kts
conclusion ns
,
"the number of matches with the first number is", z
con

Task #5

Sequentially entered n-integers. Find the difference between the maximum and minimum values given numbers
Solution:

alg difference
start things
a. b, s, d
whole
n, and
conclusion
"Enter number of numbers"
input
n
a:= 0
c:=0
nc for
and from 1 before n
conclusion ns,
"enter the number"
input
d
if
e>s
then
c:=d
all
if
d<а
then
a:= d
all
kts
b:= c - a
conclusion ns,
"the difference between the minimum and maximum values ​​\u200b\u200bis equal", b
con

The while loop

Task #1

Find the sum of all numbers between 1 and 5
Solution:

alg numbers
start things a, b
conclusion "Enter two numbers such that the second number is greater than the first"
input a, b
nts bye a<б
a:= a + 1
kts
conclusion ns
, a
con

Task #2

Given two numbers such that the second number is greater than the first. It is necessary to add 1 to the first number until it is equal to the second number, display it on the monitor.
Solution:

alg sum
start things a, b, c
conclusion " enter summation interval"
input a, b
c:= a
nts bye a< б
a:= a + 1
c:= c + a
kts
conclusion ns
," the sum of the numbers on the given interval is", with
con

Task #3

You are given two arbitrary numbers. As long as their product is less than 100, increase each number by 2 and display the final numbers on the monitor
Solution:

alg work
start things a, b, c
conclusion "Enter two random numbers"
input a, b
c:= 100
nts bye a*b< с
a:= a + 2
b := b + 2
kts
conclusion ns
, a, b
con

One-Dimensional Arrays

Task #1

Fill an array with random numbers and output its elements Solution:

alg array 2
early target
n,i
thing
b, max
clothestab
a [ 1:n ]
conclusion
"fill array"
input
n
max:= 0
nc for
i from 1 before n
conclusion ns,
"enter array element"
input
b
if
b > max
then max:=b
all
kts
conclusion ns
,

con

Task #2

Find the maximum element of the array and display it on the monitor Solution:

alg array 2
early
whole n,i
thing
b, max
clothestab a [ 1:n ]
conclusion
"fill array"
input
n
max:= 0
nc for
i from 1 before n
conclusion ns,
"enter array element"
input
b
if
b > max
then max:=b
all
kts
conclusion ns
,
" the maximum element of this array is", max
con

Task #3

Find the sum of the elements of a one-dimensional array Solution:

alg sum
early whole
n,i
clothestab
a [ 1:n ]
thing
b, z
conclusion
" enter the number of array elements "
input
n
z:= 0
nc for i from 1 before n
conclusion ns,
"enter array element"
input
b
z:= z + b
kts
conclusion ns
,
"sum",n,"array elements equals", z
con

Task #4

Find the product of the elements of a one-dimensional array Solution:

alg work
early target
i, n
thing
s, d
clothestab
a [ 1:n ]
conclusion
"enter the number of array elements"
input
n
d:= 1
nc for
i from 1 before n
conclusion ns,
"enter the number"
input
s
d:= d*s
kts
conclusion ns
,"
product", n, " elements equals", d
con

Arrays

Task #1


Fill the matrix with random numbers Solution:

alg array is two dimensional
whole
n, j, h, v
start clothestab
a
conclusion
"enter the number of elements in the table"
input
n
h:= 0
v:= 0
conclusion ns,
"fill array"
input
a
nc for
j from 1 before n
if a > 0
then
h:= h + 1
otherwise
v:= v + 1
kts
conclusion ns,
a
con


Task #2


Calculate the number of positive and negative elements of the first row of the matrix Solution:

alg array 2
early
thing b, x, z
whole
i, n
conclusion

input
n x:=0
z:= 0
nc for
i from 1 before n
conclusion
ns," enter the number"
input
b
if
b > 0
then
x:= x + 1
otherwise
z:= z + 1
all
kts
conclusion ns
,

conclusion ns,

con


Task #3


Calculate the sum of the elements of each row Solution:

alg array 3
start things
b, x, z, y
whole
i, n
clothestab
a[ 1:n, 1:n]
conclusion
"enter number of columns"
input
n x:=0
z:= 0
nc for
i from 1 before n
conclusion ns,
"fill array"
input
a[ 1:n, 1:n]
b:= a[ 1,i ]+a[ n, i ]
kts
conclusion ns
,
"the number of positive numbers is",x
conclusion ns,
"the number of negative numbers is", z
con


Task #4


Calculate the sum of the three numbers in the second row of a three-by-three matrix Solution:

alg matrix
early whole
i, n
clothestab a[1:3, 1:3]
conclusion "fill array"
input a[1:3, 1:3]
n:=0
nc for i from 1 before 3
n:= n + a[ 2,i]
kts
conclusion ns,
"the sum of the numbers in the second row of the array is", n
con

Task cards

    Find among n -integer numbers entered from the keyboard the number of negative

    You are given two arbitrary numbers. As long as their product is less than 100, increase each number by 2 and display the final numbers on the monitor

    Sequentially entered n -integers. Find the number of fives in a sequence

    Sequentially entered n -integers. Find the difference between the maximum and minimum values ​​of given numbers

    Find among n -integer numbers entered from the keyboard the number of negative

    You are given two arbitrary numbers. As long as their product is less than 100, increase each number by 2 and display the final numbers on the monitor

    Sequentially entered n -integers. Find the number of fives in a sequence

    Sequentially entered n -integers. Find the difference between the maximum and minimum values ​​of given numbers

    Find among n -integer numbers entered from the keyboard the number of negative

    You are given two arbitrary numbers. As long as their product is less than 100, increase each number by 2 and display the final numbers on the monitor

    Sequentially entered n -integers. Find the number of fives in a sequence

    Sequentially entered n -integers. Find the difference between the maximum and minimum values ​​of given numbers

    Find among n -integer numbers entered from the keyboard the number of negative

    You are given two arbitrary numbers. As long as their product is less than 100, increase each number by 2 and display the final numbers on the monitor

    Sequentially entered n -integers. Find the number of fives in a sequence

    Sequentially entered n -integers. Find the difference between the maximum and minimum values ​​of given numbers