09.06.2021

Idol simple and compound terms. Performer Robot. cycles. Explanation of new material


Goals: to form the ability to write, execute and debug algorithms using a loop bye; gain an understanding of the use of the cycle bye ; develop the ability to analyze.

Students should know: rules for recording and executing a loop bye; loop properties bye.

Students must be able to: use the loop construction when compiling algorithms bye and write it down in Kumir language, master debugging methods: step by step, continuously.

Teaching method: explanatory-illustrative and reproductive (at the stage of explaining new material), reproductive with elements of the problem (the stage of fixing the material).

Lesson provision:

During the classes

1. Explanation of new material.

In general, the algorithm should be universal, i.e., it should not depend on the distance between the Robot and the wall. For this in algorithmic language there is a special command - loop bye.

General view of the cycle bye

In general, the cycle bye is written like this:

When performing a cycle, the computer repeats the following steps:

a) checks what is written after the service word bye condition;

b) if the condition is not met, then the execution of the cycle ends, and the computer starts executing the commands written after kts . If the condition is met, then the computer executes the loop body, checks the condition again, and so on.

Example.

Debugging Method: step by step.

Computer and Robot Dialogue

Computer: bottom free?

Robot: Yes.

Computer: down.

Robot: moves down to cell B.

Computer: bottom free?

Robot: Yes.

Computer: down.

Robot: moves down to cell B.

Computer: bottom free?

Robot: no.

Since the Robot answered no, thus. written after until the condition is met and the loop ends.

Demonstrate debugging methods in the Kumir system: step by step and continuously.

The body of the loop may never be executed, if condition in loop bye not respected from the start. For example, if in the “down to the wall” algorithm, the Robot answers the first question “from below freely” No, then the computer will not call the "down" command even once.

Looping. Loop execution bye may not terminate if the condition is met all the time. For example, if there are no walls below the Robot, then when the previous algorithm is executed, the computer will “loop”, i.e. will endlessly ask the Robot “free from below”, receive in response Yes and command "down".

Doing exercises.

2. Experiments with the program on the example of the executor Robot.

It is necessary, in the Kumir system, to preliminarily make preparations for the location of the Robot (files 1.fil-6. fil).

Exercises.

Experiments with the program.

Exercise 1. (Download file 1.fil)

It is required to transfer the Robot from cell A to cell B. Use a loop bye:

use robot

Task 2. (Download file 2.fil)

It is given that the Robot is at the left wall inside a rectangle enclosed on all sides by walls. There are no walls inside the rectangle, the dimensions of the rectangle are unknown. It is required to paint over a horizontal row of cells from starting position Robot to the right wall and return the Robot to its original position.

Task 3. (Download file 3.fil)

The robot is in a horizontal corridor. Color all cells of the corridor:

Task 4. (Download file 4.fil)

1. Remake the "paint the row to the right and return" algorithm using a loop in it:

2. Using an auxiliary algorithm, write an algorithm that will make the Robot paint over the rectangle.

Task 5. (Download file 5.fil)

Write an algorithm for painting all cells around a rectangular wall:

Task 6. (Download file 6.fil) (From 2009 exam material)

The robot is on the left upper corner enclosed area in the shape of a rectangle. The dimensions of the rectangle are unknown. Write an algorithm for the Robot that fills four corner cells of a rectangle.

Task 7. (Download file 6.fil) (From 2009 exam material)

The robot is located in the upper left corner of the fenced area, which has the shape of a rectangle. The dimensions of the rectangle are unknown. Write an algorithm for the Robot that paints all the cells located inside the rectangle and adjacent to the bottom side of the rectangle.

3. Fixing. Questions:

What commands will the Computer give to the Robot when executing the cycle:

a) nc until the cell is shaded

b) nc while the cell is shaded

In a situation where the Robot is standing:

  1. in a shaded cage
  2. in unpainted?

The location of the Robot is shown in the following pictures:

How will the loop run?

Homework. Determine the value of the variable S after executing the statements:

Literature: A.G. Kushnirenko "Fundamentals of Informatics and Computer Science".

Nested loops and branches in the KUMIR system

One of the fundamental concepts in computer science is the concept of an algorithm. The origin of the term "algorithm" is connected with mathematics. This word comes from Algorithmi - the Latin spelling of the name of Muhammad al-Khwarizmi (787 - 850), an outstanding mathematician of the medieval East. In his book "On the Indian Account" he formulated the rules for writing natural numbers using Arabic numerals and the rules for working with them in a column.

In the future, the algorithm began to be called an exact prescription that determines the sequence of actions that ensures the required result is obtained from the initial data.

The algorithm may be designed to be executed by a human or an automated device. Creating an algorithm, even the simplest one, is a creative process. It is available exclusively to living beings, and for a long time thought to be only for humans. In the XII century. a Latin translation of his mathematical treatise was made, from which Europeans learned about the decimal positional system calculus and rules of multi-digit arithmetic. These rules were called algorithms at that time.

The definition of the algorithm given above cannot be considered strict - it is not entirely clear what is an "exact prescription" or "a sequence of actions that ensures the desired result is obtained." Therefore, it is common to formulate several common properties algorithms that distinguish algorithms from other instructions.

These properties are:

discreteness (discontinuity, separation) - the algorithm should represent the process of solving the problem as a sequential execution of simple (or previously defined) steps. Each action provided by the algorithm is executed only after the execution of the previous one has ended.

Certainty - 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.

Efficiency (finiteness) – the algorithm should lead to the solution of the problem in a finite number of steps.

mass character - the algorithm for solving the problem is developed in a general form, that is, it must be applicable to 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.

Ways to write algorithms

A variety of means are used to write algorithms. The choice of means is determined by the type of algorithm being executed.

There are the following main ways of writing algorithms:

- verbal when the algorithm is described in human (natural) language. National languages ​​are natural (Russian, English, German, etc.);

- symbolic when the algorithm is described using a set of symbols and is a program (programs are written using programming languages);

- graphic when the algorithm is described using a set of graphic images (flowchart).

Commonly used recording methods aregraphic notation using flow charts andcharacter notation using some algorithmic language - a program.

At graphical way records, flowcharts are compiled, on which, using symbols(geometric figures) denote the various parts of the algorithm. The elements of the block diagrams are shown in the figure.

Programming system KUMIR

When mastering the topic of algorithms, we will use the KUMIR programming system.

KuMir (Set of Educational WORLDs) is a programming system designed to support initial courses computer science and programming in secondary and higher schools.

The KuMir system uses a school algorithmic language with Russian vocabulary and built-in executors Robot and Draftsman, etc.

When entering a program, KuMir performs constant full control of its correctness, reporting on the program margins about all detected errors.

When executing the program in step-by-step mode, KuMir displays the results of assignment operations and the values ​​of logical expressions on the fields. This allows you to speed up the process of mastering the basics of programming.

Graphic Artist Robot

The graphical executor Robot allows you to master the basics of programming and understand the operation of the main algorithmic structures.

Graphic Artistis the control object. A packwe will rule them.

The robot performer is in somestarting environment - a rectangular field, divided into cells, between which there can be walls.

The robot can move around the field, bypassing the walls and painting over the cells. The robot cannot pass through a wall, but it can check if there is a wall next to it. The robot cannot go beyond the rectangle that bounds the field.

R
bot can execute commands
: up, down, right, left, paint over.

The robot can check conditions : free on the top, free on the bottom, free on the right, free on the left, adding a particle not reverses the condition. Not free from above, not free from below, not free from the right, not free from the left.

Basic Algorithmic Structures

    There are three basic algorithmic structures (constructions)-linear (following), branching and cycle, from which any algorithm can be constructed.Each algorithmic structure has one entry point and one exit point.

    We will write algorithms in both school language and in the form of block diagrams.

Linear structure

Linear structure is the simplest organization of algorithms - commands are executed sequentially one after another

Example:

Cyclic structure (cycle)

    Cyclic structure (cycle) provides multiple execution of the same commands. There are several types of cyclic structures.

    Any cyclic structure consists of two parts -header and cycle bodies.

    The set of instructions repeated during the execution of the loop is calledcycle body.

    header determines the number of repetitions of the loop body.

Loop for the number of repetitions (times)

nc N once

<команда>

kts

P Example:

use Robot
alg column

early
.
nc 5 once
. . paint over
. . up
.
kts

con

Loop with precondition (yet)

(notation in algorithmic language)

nc bye <условие>

<команда>

To c

Example:

use Robot
alg Line

early

nc bye top loose
paint over
up
kts

con

Loop with postcondition (at)

(notation in algorithmic language)

n c

<команда>

cc_at <условие>

Example:

use Robot
alg Line

early
nc

paint over; up

cc_at left free

con

Branch structure.

    Branch structure. Solving some problems requires different actions depending on the fulfillment of certain conditions. In such cases, one speaks of branching the algorithm.

    To implement the “branching” structure, two structured commands of the school EL are used - if and choice, each of which can be complete and incomplete.

    In flowcharts and school language<условие>- it boolean expression, which can result in one of two possible values ​​-true or Lying. In school language, these values ​​are written as yes and no. Programming languages ​​often use valuesTrue and False. The computer stores these values ​​as 1 and 0.

Full branch

(notation in algorithmic language)

e if <условие>
. .
then <команда1>
. .
otherwise <команда2>
all

Example:

use Robot
alg branching_full

early
.
if top loose
. .
then up
. .
otherwise down
.
all

con

incomplete branching

(notation in algorithmic language)

e if <условие>
. .
then <команда1>
all

Example:

use Robot
alg branching_incomplete

early
.
if top loose
. .
then up
.
all

con

Auxiliary algorithm (procedure)

    An algorithm by which some subtask from the main task is solved and which, as a rule, is performed repeatedly, is called an auxiliary algorithm.

    An auxiliary algorithm written in a programming language is called a subroutine or procedure.

    The auxiliary algorithm is called from the main program through the name. The auxiliary algorithm is written after the main algorithm. At auxiliary algorithm there must be a name.

use Robot
alg
early
down

square
down

down
con

alg square
early

paint over

right

paint over

down

paint over

to the left

paint over
con

Nested loops and branches

When solving some tasks with a robot, it is necessary to use nested loops or branches.

C an ucl is called nested if it is placed inside another loop.

Consider a nested loop using the example of a while loop.

We know that a loop consists of a loop header that determines the number of times the loop body will repeat.

The body of the loop is the part of the loop that is repeated when the loop is executed.

The body of a loop can be a command, multiple commands, or another loop or branch.

When the loop body is another loop or branch, they are called nested.

nested loop

On the first pass, the outer loop calls the inner loop, which runs to completion, after which control is transferred to the body of the outer loop. On the second pass, the outer loop calls the inner loop again. And so on until the outer loop ends.

Nested branch

Consider the solution of the problem with nested branches and loops:

Task 1 The robot is on the wall, which has holes, moving along the wall to the right, the robot must paint over all the cells where there are holes.

use Robot alg early
.
nc bye on right With freely
nc bye bottom loose
.paint over;
right
. .
kts
. . right
.
kts con WITH
we leave the algorithm for solving the problem with an outer while loop and a nested while loop.

R We solve the same problem using an outer while loop and a nested branch.

Let's solve the same problem with an outer while loop and a nested while loop.

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)" is called 5 times, but it cannot be called in the "N times" loop, because each time it is called with different argument values.

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 to the auxiliary algorithm “coil (arg thing)”.

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.

Khiryanov Timofey Fedorovich

if-then-else-all

General view of the team:

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 recorded 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 team:

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


2022
maccase.ru - Android. Brands. Iron. news