One programming lesson

The other day I had a chance to conduct a practical lesson on programming for tenth grade students of one of the Kharkov lyceums. Six years ago, I taught a programming course at the Polytechnic University, but then I had two whole semesters of time for lectures and laboratory classes to initiate students into this, I’m not afraid to say, science. And here it was only an hour and a half at the most, and I have never worked with such a young contingent. "OK" I said to myself. And started preparing. I was given several tasks that could be solved with schoolchildren. The first of them took as much as 70 lines of Hindu code. Prepared my solution of 10 lines. Thought, “First I will give one solution, then I will show another”. I rewrote one more task in order to shift the emphasis from programming features to the subject area (the task was geometric). The third task was the simplest - one person enters a number from the keyboard, the other guesses. Not interested. Let the computer guess and give hints. For each task, I came up with a sequence of presentation of the material. When the time came, and the schoolchildren sat down at the computers, I asked them: “Do any of you have programming experience? Have you already studied any programming languages?. Having received a negative answer, he mentally said to himself "Sadly", put aside two sheets with a listing of a code of three and made a statement: "Well, well ... Then let's start programming!".

For encoders, this article, for sure, is of no interest. My story will be about the method of teaching in conditions of limited time for people with a fragile child's psyche, using the example of just one lesson. I invite everyone who wants to go under the cat!

An introductory word about programming began something like this. “Computers are now used in almost every area of ​​human life. Therefore, it does not matter which path you choose, who you will study, being able to program is important enough. With the help of this science, you can get a significant benefit.. Next, I gave an example of the “traveling salesman problem”, formulating it as follows: “Imagine that you work for Nova Poshta. You need to deliver a lot of parcels to different cities. It would be nice to choose a path that would be as short as possible. This will save money - the courier will work less hours, you will spend less liters of gasoline.. And a little transition: “But, unfortunately, the computer itself is not able to solve such problems. He can only perform arithmetic and logical operations.(well, and others, but we will not talk about it now). “And he does it over numbers in the form of zeros and ones”(we will not waste time talking about the binary number system - I hope it is in the school curriculum). “Commands to the computer (machine instructions) are also given in the form of numbers. But usually programmers write programs in human-readable languages, such as C, Java, C++.”. Hearing "si-plus-plus", the children perked up. “To convert program code into instructions for the computer, there are several types of programs, for example, compilers. To work with it more conveniently, we will use another program - a development environment, which also includes a text editor and many other useful tools. Find the shortcut for Code::Blocks on your desktop and run it".

Then I told how to create a new project and in detail, line by line, described the contents of the file with the program. The line numbering helped a lot. But the interpretation of the terms turned out to be quite free.

“So, you can see that there are English words in the program code. This and include, And using, And main, And return. In the first line we include, i.e. we use some library. Typically, programmers use code written by other programmers. It is included in all sorts of libraries. In this case, we are using the library iostream. Here i is input (input), o is output (output), stream is a stream. Those. the library contains code for keyboard input and screen output"(It is not worth overloading schoolchildren with information about the redirection of I / O flows). “If there are many libraries, conflicts can arise between them, so the code is usually placed in different spaces. using namespace std needed in order to select the namespace (namespace) std - short for standard (standard). int says that we are talking about integers, about their storage and transmission "(i.e. I meant the declaration of variables and the value returned by the function; I did not talk about explicit type casting) « main– function name. A function is some kind of logically complete piece of code that returns some value. cout... c - console (console - keyboard and screen), out - output, endl- end of line, end of line. In the seventh line, the text enclosed in double quotes is displayed on the screen. return 0 in this case tells the operating system that the program completed successfully".

After that, he suggested pressing F9 to compile the program ( "convert program text to machine instructions"). "Congratulations! You've written your first program!", I said, when I saw consoles appear on the monitors with text. Then he clarified: “Well, not quite written - others have already done it for you. So let's make changes to the code. Change the double quotes to the text Hello world! in some other English language and press F9 again. Now that's another matter!". Someone did not close the window of the running program, so the compilation failed. I had to help. “Now replace the text with some other, in Russian. And be surprised." Those who wrote "Hello" saw the following:

“The thing is that the text is also converted to zeros and ones. And how exactly this conversion will take place depends on the encoding. Has anyone come across this concept? In response - an uncertain lowing ... “Let's set the encoding for the Cyrillic alphabet. Set (set) the appropriate locale (locale). To do this, we lower the seventh line down (put the cursor at the beginning of the line and press Enter). And in the empty seventh line, enter setlocale(LC_ALL, "rus"); And in the second line, enter #include » . Someone wrote LC_ALL in lowercase letters (I had to explain that lowercase and uppercase letters are different), someone wrote off L.C.A.L.L. (yes, the board is in a terrible state), someone wrote "russ" and did not get the proper result. But in most cases I saw a positive outcome. I was a little saddened by the text that one girl wrote, “I want to eat.” In this state, the perception of information suffers quite a lot.

It's time to formulate the conditions of the problem for the students. “Now let's write the program. Let the computer guess a number from 0 to 99, and we will guess this number with its hints.. Yes, this is the third issue.

“To generate a random number, the rand function is used, short for the word random - random. To use it, you need to include the library cstdlib. To generate a number from 0 to 99, you need to take the remainder of dividing the result that the function returns by 100. The operation of obtaining the remainder from division is written as a percent symbol. Here I had to remind the students what the remainder of the division is. I gave the example "5% 2", and it became clear to them what I meant. “The result of the operation of taking the remainder of the division (that is, a random number from 0 to 99) needs to be written somewhere. This number is an integer. It would be strange if we tried to guess some real number, for example, 2.584 or 35.763. We will use a variable to store the result. A variable is a region of computer memory (we don't care where this memory is yet) that can be accessed by name.. Yes, a certain set of operations can be performed on variables of various types, but this does not matter now. “Let's call the variable u (from the word unknown). To declare a variable of an integer type, use the word int. Such a memory area on these computers is 4 bytes and can hold a number from about minus two to plus two billion. It's enough?" Having received an affirmative answer, I wrote the missing code on the board. It turned out the following (along with the correction of the output - now the screen will not display text, but the value of the variable):

Having launched the program, the schoolchildren, one and all, saw the number 41. Not 42, but it will also do. Moreover, the result did not change from run to run. “So we got a random number. Indeed, who would have thought that a computer would give 41? The number 41 satisfies the conditions that we have set. It ranges from 0 to 99. But how do you make it truly random? To do this, you need to set the so-called seed of the random number generator, for example, with the current time. Add a line before the tenth line strand(time(0)); If the program does not compile, add the library ctime»

Now the program produced really random (well, not really random, but it doesn't matter for this task) numbers. The source code of the program at the moment was as follows:

It remains to write the code responsible for guessing it.

"I don't think you can guess a number from 0 to 99 the first time" The students smiled. “If we do some of the same actions several times, then this can be done in the form of a cycle” Since it is difficult to describe in words how to implement a cycle, I first wrote the corresponding lines on the board.

“In the thirteenth line, we declared a variable i (from input), similar to the variable u. In it we will store the entered number. The actual input is carried out in the 16th line. The loop is declared with the keyword do. Anything enclosed in curly braces will be repeated until ( while) the value of the variable i is not equal to u”. As for this section of the code, the typical mistakes of the students were as follows. First, they used parentheses instead of curly braces. Secondly, the comparison operation "!=" was written separately. After compiling the program, the children persistently tried to guess the number u. I was amazed that the girl who had previously written “I want to eat” did it very successfully. Of the runtime errors, I was happy to see the following:

This allowed me to explain that there is no input validation in the program, and entering letters when only numbers are expected of us is not a good idea.

We have reached the finish line. It remains to add hints. I wrote two "ifs" on the board and explained. “If the entered number is greater than the hidden one, we display the corresponding message (line 17). If the entered number is less than the guessed one, we do the same (line 18). Plus, I expanded the output of the message about the end of the "game".

This was the final text of the program, which I typed in the first programming lesson of the 10th grade. The program is far from perfect. In particular, I don't like "Your number is higher!" messages. and "Your number is under!". They are really confusing. If I had a second chance to conduct a similar lesson, I would have formulated it differently.

In this lesson, I also wanted to show the students the algorithm for quickly searching for a hidden number (binary search), but it turned out that they themselves intuitively came to this solution, which could not but please me.

Let's sum up the results.

1. The lesson was successful. All students completed the task. Problem solved. Only one, but solved. Not without difficulty, of course.

2. I got a new teaching experience. For the past two years, I have been lecturing and conducting laboratory work only for fifth-year students, and working with them is a completely different matter. They already have some kind of base, their attitude to learning (and to life in general) is different, and my subjects are highly specialized - the material that I give will be useful in the future at most 2-3 of our graduates from each group. There is also hope that this particular lesson will arouse interest in programming in one or two students.

3. The school curriculum is completely different than the one I studied. Yes, I did not go to an ordinary school. We studied Logo in seventh grade, BASIC in eighth grade, and Pascal in ninth grade. But, nevertheless, even those of my classmates who did not shine with knowledge in other subjects (and I didn’t shine either!), Liked computer science. I am sure that it is necessary to teach programming at school. It perfectly develops the brain and allows you to understand computers (without which we can no longer imagine our lives) from a completely different perspective.

4. The C++ language has a high entry threshold. One lesson to reveal the basics of this programming language is clearly not enough. Yes, I don't know C++. I love C, and when I need OOP, I write in Java. But it is most likely necessary to study C ++ at a university (C, in my humble opinion, is a must). Again, much depends on the university and specialty.

Thank you for your attention to everyone who read to the end! I will be glad to answer your questions.

P.S. There is an idea to write another article about computer science at school. If you support in the comments, the article will most likely (I will not promise) see the light of day.

  • Sergei Savenkov

    some kind of “scanty” review ... as if in a hurry somewhere