Let's talk about cycles today. Let's figure out what a cycle is and how to teach our Robot to perform cyclic algorithms.

So, what is a cycle? Imagine that we are in a physical education lesson and we are faced with the task do 7 squats. This task can be formalized as a linear algorithm and then it will look something like this:

do a squat

do a squat

do a squat

do a squat

do a squat

do a squat

do a squat

That is, we repeated the command to do a squat 7 times. Does it make sense to write 7 identical commands? Might be easier to give a command. do 7 squats? Certainly easier and more correct. This is the cycle. You can yourself remember examples of cycles from life - there are quite a few of them.

In this way linear algorithm, where the same commands are repeated, we can issue in the form of a cyclic algorithm- more or less like this:

repeat 7 times

do a squat

end of cycle

So, in the language we invented, we designed the cycle. The Robot performer also has the ability to record cycles. Moreover, cycles are different.. The option we have just considered is called loop with counter or loop with parameter.

Types of cycles.

Cycle with a counter.

Loop with counter used when it is known in advance how many repetitions need to be done. In the example above with squats, this is exactly the case.

In order to write a loop with a counter for an executor, you need to know its syntax. And he's like this:

nc<number of repetitions> times

<команда 1>

<команда 2>

<команда n>

Here we must specify the number of repetitions (number) and the commands that will be repeated. Commands that are repeated in a cycle are called loop body.

Let's look at this with an example.

Initially, the Robot was in the upper left cell.

Let's solve the problem linearly first. In this case, we will paint over the current cell and move 1 cell to the right and the program will look like this:
use robot
alg
early

paint over

right

paint over

right

paint over

right

paint over

right

paint over

right

paint over

right

paint over

right

As you can see, the commands to paint over and to the right are repeated 7 times. Let's now rewrite the program using a loop. By the way, to insert a cycle into your program, you can use the menu Insert select item nc-time-kc or press one of the keyboard shortcuts Esc, P(Russian letter Р) or Esc, H(Latin letter H). And keys must be pressed sequentially- first Esc, release it and only then P or H.

So here is our cycle program will look like this:

use robot

nc 7 times

paint over

right

If we run it, we will see that the result will be the same - 7 filled cells. However, the program has become shorter and much more intelligent from an algorithmic point of view!

As a warm-up and consolidation, I propose to independently write a program for the Robot, which will draw a square with a side of 7 cells. Naturally, using a cycle. Waiting for the solution in the comments.

Conditional loop.

When solving problem 19 of the GIA in informatics with a Robot, it will not work to use a loop with a counter. Since there the field is usually infinite and the walls do not have a specific length. Therefore, we will not be able to determine the number of repetitions for a loop with a counter. But it doesn't matter - it will help us loop with condition.

Let's go back to physical education and change the task. After all, someone may not do 7 squats, while the other is able to do 27. Is it possible to take this into account when creating a cycle? Of course. Only now we will use not a counter (number of repetitions), but a condition. For example, before you get tired, do squats. In this case, a person will not do a specific number of squats, but squat until he gets tired. And our loop in the abstract language will look like this:

bye not tired

do a squat

end of cycle

The words are not tired in our case - this is a condition. When it is true, the loop is executed. If it is false (tired) the body of the loop will not be executed. The robot performer has several conditions

top loose

bottom loose

left free

right free

top wall

bottom wall

left wall

right wall

But in the condition of problem 19 of the GIA, only the first 4 are indicated. So we will use only them.

Now let's solve the next task for the Robot - draw vertical line from the left to the right border of the field I use a loop with a condition. Initially, the Robot is in the upper left corner.

Let's define first verbal algorithm- that is, we will describe in words what the Robot needs to do. This algorithm will sound something like this:

« While on the right, freely take a step to the right and paint over the cell »

As a result, the Robot will run through all the cells to the right and will paint over them until the wall is on the right.

The source code for our Robot program will be something like this:

use robot

nc while right free

right

paint over

As a result of the execution of this program, we will see the following picture:

Performer Robot. Helper Algorithms(2h)

Target: introduce the concept of the main and auxiliary algorithm; explain the rules for using the auxiliary algorithm; parse examples of algorithms using an auxiliary. To develop practical skills in constructing algorithms by the method of sequential refinement.

Lesson Plan

1.Introduction of new terms (main and auxiliary algorithm, call) and explanation of new concepts.

2. Analysis of examples of solving problems using an auxiliary algorithm.

3. Practical work

When solving some problems, it is convenient to break them into smaller subtasks, each of which can be designed as an independent algorithm. In this case, the so-called main algorithm is first compiled, in which calls to auxiliary algorithms are used to solve subtasks, which are added later. This kind of solution is called sequential refinement method. It allows a group of programmers to work on a project, while each one solves his own subtask.

In the process of solving the problem, each auxiliary algorithm can, if necessary, be divided into smaller ones. auxiliary algorithms.

The command to execute the auxiliary algorithm is called challenge and is written in the body of the main algorithm.

One and the same algorithm can be considered as the main and auxiliary in relation to other algorithms. AT algorithmic language first, the main algorithm is written, and auxiliary ones are written in a row below.

Task 1:

The robot is in the upper left corner of the field. There are no walls or shaded cells. Compose an algorithm, using an auxiliary one, drawing four crosses on one horizontal line. The final position of the Robot can be arbitrary.

Solution

Analysis on the board:

Task2. The robot is in the upper left corner of the field. There are no walls or shaded cells. Write an algorithm that paints an 8 x 8 square in a checkerboard pattern. The final position of the Robot can be arbitrary.

Practical work on a PC "Problem solving using auxiliary algorithms"

Task1 . The robot is in the lower left corner of the field. There are no walls or shaded cells. Write an algorithm that paints 6 vertical stripes of the same length in 6 cells. The final position of the Robot can be arbitrary.

Task2 . Using auxiliary, write an algorithm for painting over the cells that form the number 1212.

Homework : Come up with an algorithm that draws the following image: Apply two auxiliary algorithms to solve the problem.

Methodological guide for teachers of informatics. Evgrafova Olga Vladimirovna, gymnasium No. 8 Kolomna, 2013 In the process of studying the subject "Computer Science and ICT", students not only learn the information culture (the culture of competent communication with a computer, paperwork, electronic communication), but also master the basics of programming. One of the most successful domestic developments in this area is the Kumir programming system. In it, students can gain practical skills in creating and debugging an algorithm by working with the Robot and Draftsman performers. The advantages of this system are the possibility of writing commands in Russian in the school algorithmic language and the visibility of the result of the algorithm execution. The KuMir system is designed based on the needs Russian system education. Its features:      freely distributed; multiplatform; zero resource requirements; supported by the Russian Academy of Sciences; among the developers are the authors of many textbooks and manuals on informatics;  Free program shell Kumir can be found at http://www.niisi.ru/kumir/. Unfortunately, the authors of new computer science textbooks pay insufficient attention or generally bypass these performers. At the same time, the developers of the GIA and -1-

p. 2

The USE includes assignments using the data of the performers in the assignments of the examination papers. The topic is studied in the 9th grade in the section "Algorithmization and programming". 9 hours are allotted for the study of the topic. Students should know: what is a performer; SKI Robot, Wednesday performer Robot; what is an algorithm; what are the main properties of the algorithm; ways of writing algorithms: flowcharts, educational algorithmic language; basic algorithmic constructions: following, branching, loop; assignment of auxiliary algorithms. Students should be able to: understand descriptions of algorithms in an educational algorithmic language; perform an algorithm trace for a known performer; compose linear, branching and cyclic control algorithms for the Robot executor; allocate subtasks; define and use auxiliary algorithms. Thematic lesson plan: No. p / p 1. 2. 3. 4. 5. Number of Teo-hours riya Performer Robot. Executor command system. Composition1 0.5 Linear algorithms. Cyclic algorithms. 3 1 Practical work №1. Compilation and debugging of cyclic algorithms. Branching and sequential refinement of the algorithm. 2 1 Practical work №2. Compilation and debugging of cyclic and branching algorithms. Auxiliary algorithms and subroutines. 2 0.5 Ex. slave. Number 3. "The use of auxiliary algorithms". Test. “Creating an algorithm in the environment of the performer” Lesson topic Practice 0.5 2 1 1.5 1 Materials for lessons on the topic “Training performer Robot”: -2-

p. 3

Lesson 1 Executor command system. The robot moves during the execution of the program in a rectangular checkered field, between the cells of which walls can be located. The field is called up on the screen by the "Robot Window" button, which is located on the toolbar. Robot code field. The robot is able to execute commands-orders and commands-questions. Commands-orders: right, left, up, down, paint over. If there is a wall in the way, the command cannot be executed. To directly control the Robot, use remote. By pressing the buttons on the remote control, the person gives the appropriate commands, and the Robot executes them. For program control it is not enough to know which commands and in what sequence to execute. It is also necessary to write down these commands in a form understandable for a computer, i.e. put in the form of an algorithm. In the simplest case, the algorithm for the Robot is written as follows: The name of the algorithm is a sequence of characters or words separated by spaces. The first character of the name must not start with a number. Correct names: g, sum, nickname perimeter, April 12, April twelfth, number_11_cl. The "correct" name will be highlighted in blue. use Robot alg algorithm_name start sequence of commands end For a better understanding of the algorithm in the body of the program, you can use comments. Comments start with |. If comments span multiple lines, each line must be preceded by a | sign. Comments have no effect on the progress of the algorithm. Example 1. Program "Knight's move" (move the Robot from point A to point B). Initial state: Program: Result: The rules of the algorithmic language allow you to write several commands on one line separated by a semicolon. -3-

p. four

Example 2. It is required to move the Robot from point A to point B. The path that the Robot must go through can be divided into five identical sections. It is convenient to group the commands for passing each section in one line - this shortens the record of the algorithm and makes it more understandable. Change of environment Robot. In order to force the Robot to act in a new environment, it must be loaded using the following main menu commands: Select the “Robot” element “Change starting environment” Open desired folder and select a file. Creating a new environment. To create a new environment for the Robot, you must execute the main menu commands: Select the item "Tools" "Edit the starting environment". In the "Settings" window, select the "Settings" element "New Setting" Set the number of rows and columns. Place walls with a mouse click and move the Robot to the desired cell. Save the environment using the commands: "Environment" "Save AS" Specify the file name and folder for placement. Press the "Save" button. Add/Remove wall – click on the border between cells. Colorize / Clear a cell - click on the cell. Put / Remove a point - click on the cell while pressing Ctrl key. Set radiation, labels - click the right mouse button. Move the Robot - drag with the mouse. "Execution" Execution of the program. “Run continuously” Independent work on the computer: 1. Load a new environment, move the Robot to point “B”, painting along the points marked with a “dot” symbol: “Workshop for the Robot” folder “1” task “C” (materials from site of K. Polyakov http://kpolyakov.narod.ru), create a program, open the Robot window and run the program for execution. Save the program in your folder with the name "1_B". 2. Create a new setting, save it in your folder with the name "Settings_1". Compose a program for the new environment and save it in your folder with the name "new_environment_1". 3. **Solve other problems from folder #1 of the workshop. -4- B

p. 5

Lesson 2. Cyclic algorithms. Cycle N - times. Repetition task: Petya made an algorithm for the Walk Robot. Kolya erased one command in it. Determine which command Kolya erased if it is known that the Robot must return to the initial state. Answer: to the right use Robot alg Walk start. up; to the right. ? . way down; left; left end nc N times · series of commands kc When compiling algorithms, there are often cases when a certain sequence of commands needs to be executed several times in a row. For example, example number 2 of the last lesson. To simplify the writing of the algorithm in such cases, you can use the command - "Loop N-times". N is an integer expression specifying the number of repetitions. When the algorithm is executed, the sequence of commands is cyclically repeated the specified number of times. This number can be zero or even negative. These cases are not considered erroneous, just the body of the loop will not be executed even once, and the computer will immediately proceed to the execution of the commands written after the kts. Example 1. It is required to move the Robot from point A to point B. A B Example 2. Color in a row of 16 cells. Example 3. Fill in a 5(rows) x 6(columns) rectangle. use Robot Alg Rectangle start nc 5 times kc end A loop can be placed inside another loop. Then it is called nested. -5-

p. 6

Independent work on the computer: Example 4. Let's move the Robot from the initial position to the point marked with an asterisk and paint over the cells of the labyrinth along the way. Create environment and program. Save files in your folder. Example 5. Transfer the Robot from the initial position to the Base and paint over the cells marked with a dot. Create environment and program. Save files in your folder. Example 6. It is necessary to guide the Robot through the maze from the initial position to point A. Example 7. It is necessary to guide the Robot along the corridor from the initial position to point A, looking into each side corridor. Example 8. Write a program for painting the cells of the field marked with *. The initial position of the Robot is indicated by the icon ◊. Example 9*. Walls are exposed on the performer's field. It is necessary to find the initial, optimal position of the performer and write a program, the result of which will be filled cells (as shown in the figure). **Compose programs using the folder "Workshop for Robot" _ No. 2. -6-

p. 7

Lesson 3 We need to learn how to create universal algorithms that will not depend on the distance between the Robot and the wall, the length of the walls. To do this, we will use the new loop operator. nt while condition a series of commands kts While executing the cycle, the computer repeats the following actions:  checks the condition;  if the condition is met, then the loop body commands are executed and the condition is checked again, etc.  if the condition is not met, then the execution on the left side of the free loop is completed, and the commands are executed, on the right side of the right side freely recorded after kts. bottom wall bottom free  cycle conditions (commands-questions): top wall top free cell filled cell clean Notes.  If the condition is not met from the very beginning, then the body of the loop will not be executed even once!  The execution of the loop may not complete if the condition is met all the time. This situation usually occurs as a result of errors in the NC BYA on the right freely compiling algorithms. right; to the left An example of an infinite algorithm (there is no wall on the right): KC Conditions can be simple and compound. A simple condition is usually some kind of check. Any command-question of the Robot can serve as an example. A compound condition is formed from several simple conditions using the service words AND, OR, NOT. If between simple terms stands AND, then for the compound condition to be satisfied, it is necessary that both simple ones turn out to be true. If there is an OR between simple conditions, then for the compound condition to be satisfied, it is enough that at least one simple condition is true. Example 2. Robot Example 1. Must paint over to move the Rockets indicated by the bot to the Base. on the image. The length of the wall and the distance to the wall are unknown. use Robot alg To Base early. nc while the top is free. . up. kts. nts while the wall is on top. . to the right. kts. up con -7-

p. eight

▪ Independent work on the computer: Somewhere in the Robot field there is a wall in the form of a corner, the dimensions of which are unknown. A robot from an arbitrary cell to the left of the wall, but always opposite it, must reach the wall and paint over all the cells along the wall behind it, as shown in the figure. ▪ It is necessary to move the Robot from its initial position to point A, while painting over the specified cells of the field. The dimensions of the walls and the distance between them can be arbitrary. ▪ Compile programs using the Robot Workshop folder  Folder_4_Tasks_А_С_D  Folder_5_Tasks_A_B_C_D Lesson 4. Practical work №1. Cyclic algorithms for the Robot. Option 1 Task 1. Paint over the cells located to the left of the wall or on top of the wall. The distance from the Robot to the wall is unknown. The length of the walls is unknown. Create an environment and write a program. Save the files in your folder. R Task 2. Performer The robot is somewhere inside a limited square area. The length of the side of the square is unknown. Write an algorithm for the Robot that paints all the cells located on the diagonal of the square from the upper left to the lower right corner. Create an environment and write a program. Save the files in your folder. Task 3. Transfer the performer from cell A to cell B, while painting over the marked cells. The length of the walls and the distance between them are unknown. Create an environment and write a program. Save the files in your folder. -eight-

p. 9

Task 4. It is necessary to move the Robot from the initial position (◊) to point A, while painting over the indicated cells of the field. The dimensions of the walls and the distance between them can be arbitrary. Task 5. There is a ladder on the infinite field. The number of steps can be any. The lower horizontal wall continues infinitely to the right, the upper horizontal wall extends infinitely to the left. The robot is somewhere above the bottom horizontal wall. Paint over the cells located along the vertical part of the steps (see fig.). Any final position of the Robot. Create an environment and write a program. Save the files in your folder. Each step has a size: 2 cells horizontally and 2 cells vertically. Option 2 Task 1. Write for the Robot an algorithm that paints over all the cells located along the left side and above the bottom sides of the rectangle and adjacent to the wall. The distance from the Robot to the walls and the length of the walls are unknown. Create an environment and write a program. Save the files in your folder. Task 2. Performer The robot is somewhere inside a limited square area. The length of the side of the square is unknown. Write an algorithm for the Robot that paints all the cells located on the diagonal of the square from the upper right to the lower left corner. Create an environment and write a program. Save the files in your folder. Task 3. Transfer the performer from cell A to cell B, while painting over the marked cells. The length of the walls and the distance between them are unknown. Create an environment and write a program. Save the files in your folder. -9-

p. ten

Task 4. It is necessary to move the Robot from the initial position (◊) to point A, while painting over the indicated cells of the field. The dimensions of the walls and the distance between them can be arbitrary. Task 5. There is a ladder on the infinite field. The number of steps can be any. The lower horizontal wall continues infinitely to the left, the upper horizontal wall extends infinitely to the right. The robot is somewhere above the bottom horizontal wall. Paint over the cells located along the vertical part of the steps (see fig.). Any final position of the Robot. Create an environment and write a program. Save the files in your folder. Each step has a size: 2 cells horizontally and 2 cells vertically. Lesson 5 Command “if _ then _ else _ all” General view of the command: if condition then actions 1 otherwise actions 2 all Command structure: after checking the condition, actions 1 are performed if the condition is true, otherwise actions 2 are executed. After that, the commands written after the word all. The branch may otherwise be missing (incomplete branching). In this case, the command looks like: if the condition, then all actions. If the condition is not met, then the commands written after official word all. Example 1. Create a program that guides the Robot through a corridor of arbitrary length. Along the way, the Robot must paint over all the cells that do not have a wall below. The number of holes is unknown. - ten -

p. eleven

Example 2. Move the Robot to point B and paint over the cells marked with a dot. The length of the wall and the number of "dead ends" is unknown. use Robot alg To Base start nt while right free to right if top is free then up paint down all kc down end Example** Calculate how many shaded cells are in the corridor (to perform it, you need to parse additional constructions: integer count | description of a variable of integer type count:= count+1 | increase the variable by 1 output count | display the value of the variable on the screen Independent work on the computer: Create an environment and solve the problem Task 1. Create an algorithm for the robot to pass through the environment: Task 2. Write a program in which the Robot overcomes any number of obstacles and reaches the wall.Obstacles can be placed anywhere.-11-

p. 12

Task 3. Color in the cells marked with dots. Task 4. use the Robot alg To the Base, start painting over the nc until the right is free to the right; if it is free from above, then up; paint over; down otherwise paint over all kc down con Task 5. Paint over the cells located next to the walls. Write an algorithm that paints all the cells between two walls. Which wall is to the left is unknown. use Robot alg between_walls start nt until (not free from above) or (not free from below) right if (not free from above) and (not free from below) then paint over all kc end use Robot alg uch5 start right nts until free from below; paint over to the right; down nts until the left is freely painted over; to the left kts nts until the left is freely painted over; paint down kts; left; paint over; up; nts until the top is freely painted over; up kts nts until it is free to paint over from above; left kts con Task 6**. The robot is at the beginning of the corridor. In the corridor there are dead ends above and below of unknown length, but their location is not opposite each other. The length of the corridor is also unknown. Write an algorithm for painting all dead ends and exiting the corridor. - 12 -

p. 13

use robot alg puffins early. nts yet (top wall) or (bottom wall). . to the right. . if (top free) and (bottom wall) . . . then. . . . nc while the top is free. . . . . up; paint over. . . . kts. . . . nc while the bottom is free. . . . . way down. . . . kts. . all. . if (bottom free) and (top wall). . . then. . . . nc while the bottom is free. . . . . way down; paint over. . . . kts. . . . nc while the top is free. . . . . up. . . . kts. . all; . kts; con Commands highlighted in red are needed to stop when exiting the corridor. Lesson 6 Cyclic and branching algorithms for the Robot. Solving tasks using KIM GIA. Comment. When compiling algorithms, the problem must be solved in a general form, i.e. the shape of the figure is defined, but the lengths of the sides, the number of steps and other parameters may vary. The program should work for any figure of a similar kind! Task 1. Create a setting and write an algorithm that paints over all cells located to the right of the vertical sections of the wall. use robot alg task 1 start down; to the right. nc while the top is free. . paint up. kts. nc until the top is free. . to the right. kts. up. nc until the left is free. . paint over; up. kts con - 13 -

p. fourteen

use robot alg task 2 beginning. nts while on the right is free. . to the right. kts. nc until the right is free. . up. kts. to the right. nc until the right is free. . paint up; to the right. kts con The height of each step is one cell, the width is 2 cells. Task 2. Write an algorithm that fills all the cells above the steps. Task 3. Paint over all the cells located above the steps of the stairs descending from left to right. use robot alg task 3 beginning. nc while the bottom is free. . way down; left; to the left. kts. nc until the left is free. . paint over; to the right. . paint over; to the right. . way down. kts con Task 4. Color the cells of the labyrinth. use Robot alg Labyrinth early. to the right. nts while on the right wall. . paint over; up. . paint over; up. . paint over; to the right. . paint over; way down. . paint over; way down. . paint over; to the right. kts con Task 5** The performer is in an arbitrary cell, but always opposite the wall (picture above). As a result, it is necessary to paint over the cells as shown in the figure below. - fourteen -

p. fifteen

use robot alg Exit start. nc while on the right is free to the right; . kts. nc until right wall down; . kts. right; up. nts yet (wall on the left and free on the top) or (wall on the top and free on the right) or (wall on the right and free on the bottom) or (wall on the bottom and free on the left). . if (left wall) and (top free) . . . then. paint over; up. . all. . if (top wall) and (right free) . . . then. . paint over; to the right. . all. . if there is a wall on the right and the bottom is free. . . then. . paint over; way down; . . all. . if the wall is below and the left is free. . . then. . . paint over; left; . . all; . kts; con Lesson 7. Auxiliary algorithms When solving some problems, it is convenient to break them into smaller subtasks, each of which can be formalized as an independent algorithm. In this case, the so-called main algorithm is first compiled, in which calls to auxiliary algorithms are used to solve subtasks, which are added later. This way of solving is called the sequential refinement method. It allows a group of programmers to work on a project, while each one solves his own subtask. The command to execute an auxiliary algorithm is called a call and is written in the body of the main algorithm. In an algorithmic language, the main algorithm is written first, and auxiliary ones are written below in a row. Example 1 Consider the setting from lesson #1. The robot must be transferred to the cell marked with a dot. use Robot alg Corridor start bypass; detour; detour; detour; bypass con alg bypass start up; up; right down; way down; right con - 15 -