⬆️ ⬇️

One programming lesson

The other day I had a chance to hold a practical programming lesson for students of the tenth grade at one of Kharkov lyceums. Six years ago, I taught a programming course at the polytechnic, but then I didn’t be afraid to say that I had a full two semesters of time for lectures and laboratory classes. And here it was only half an hour from the force, and I did not work with such a young contingent yet. “Okay ,” I said to myself. And began to prepare. I was given several tasks that could be solved with schoolchildren. The first of them occupied as much as 70 lines of Hindu code. Prepared his decision from 10 lines. I thought, "First I will give one solution, then I will show another . " I rewrote another task in order to shift the emphasis from programmer 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 one guesses. Not interested. Let the computer better guess and give hints. For each task came up with a sequence of presentation of the material. When the time came, and the students sat down at the computers, I asked them: “Do any of you have programming experience? Have any programming languages ​​already been studied? ” Having received a negative answer, he mentally told himself “Sad” , put aside two sheets with a printout of the code of three and made a statement: “Well, well ... Then let's start programming!” .



For coders, this article is probably of no interest. My story will be about teaching methods in a limited time for people with a fragile child psyche using the example of just one lesson. I invite everyone to the cat!



The introductory word about programming began like this. “Computers are now being used in almost every sphere of human life. Therefore, it does not matter which way you choose, whom you will learn, to be able to program is important enough. With the help of this science, you can get significant benefits . " Further, I gave an example of the “traveling salesman problem”, formulating it as follows: “Imagine that you are working in New Mail. 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. It will save money - the courier will work less hours, gasoline will spend less liters . ” And a small transition: “But, unfortunately, the computer itself does not know how to solve such problems. He is able to perform only arithmetic and logical operations " (well, and others, but let's not talk about it now). “And he does it over numbers in the form of zeros and ones” (let's not waste time talking about the binary number system - I hope it is in the school curriculum). “Computer commands (machine instructions) are also given in the form of numbers. But usually programmers write programs in languages ​​understandable to humans — for example, C, Java, C ++ . ” Hearing the "C-plus-plus", the children came to life. “There are several types of programs to convert the program code into computer commands, for example, compilers. To make it more convenient to work with it, we will use another program - the development environment, which also includes a text editor and many other useful tools. Find the shortcut on the desktop for the Code :: Blocks program and launch 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. Line numbering really helped. But the interpretation of the terms turned out pretty loose.

')

image



“So, you can see that in the program code there are English words. This includes and using , and main , and return . In the first line we include, i.e. use some library. Usually programmers use code written by other programmers. It is included in all sorts of libraries. In this case, we use the iostream library. Here i is the input, o is the output, and stream is the stream. Those. the library contains code for keyboard input and screen output ” (it’s not worth overloading schoolchildren with information on redirecting input / output streams). “If there are many libraries, conflicts may arise between them, so the code is usually placed in different spaces. using namespace std is needed in order to choose a namespace (namespace) std - abbreviation from standard (standard). int says that we are talking about integers, their storage and transmission " (ie, I meant the declaration of variables and the return value of the function; I did not tell about explicit type conversion) " main is the name of the function. 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. The seventh line displays the text enclosed in double quotes on the screen. return 0 in this case tells the operating system about the successful completion of the program . "



After that I suggested pressing F9 to compile the program ( “convert the text of the program into machine instructions” ). "Congratulations! You wrote your first program! ” , I said when I saw consoles with text appearing on the monitors. Then he clarified: “Well, I didn’t write at all - others have already done it for you. So let's make changes to the code. Change the double quotes for the text Hello world! on some other one in English and press F9 again. Now it'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 something else, in Russian. And be surprised. ” Those who wrote“ Hello ”saw the following:



image



“The thing is that the text is also converted to zeroes and ones. And how exactly this conversion will occur depends on the encoding. Has anyone come across this concept? " In response, an uncertain shuffle ... " Let's set the encoding for Cyrillic. Set (set) the appropriate locale. To do this, drop 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 <locale> . " Someone LC_ALL wrote in lower case letters (I had to explain that lowercase and capital letters are different), someone wrote off the LCALL board (yes, the board is in terrible condition), 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.



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



“To generate a random number, the rand function is used, the abbreviation for the word random is random. To use it, you need to connect the library cstdlib . To generate a number from 0 to 99, you need to take the remainder of dividing the result, which the function returns, by 100. The operation of obtaining the remainder of dividing is written with the percent symbol ". Then I had to remind the students what the remainder of the division is. He gave an example of “5% 2”, and it became clear to them what I meant. “The result of the operation of taking the remainder of the division (ie, 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 a real number, for example, 2.584 or 35.763. To store the result we will use a variable. A variable is a region of computer memory (for the time being it does not matter to us where this memory is located), which can be accessed by name . ” Yes, it is possible to perform a certain set of operations with variables of different 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 takes 4 bytes and can hold a number from about minus two to plus two billion. Is that enough? ” Having received an affirmative answer, I wrote the missing code on the board. The following came out (along with the correction of the output - now the screen will not have text, but the value of the variable):



image



Running the program, the students, one and all, saw the number 41. Not 42, but it would also come down. And the result did not change from launch to launch. “So we got a random number. Indeed, who would have thought that the computer will issue 41? The number 41 satisfies the conditions we set. It ranges from 0 to 99. But how to make it really random? To do this, you need to specify the so-called grain of the random number generator, for example, the current time. Add before the tenth line the line srand (time (0)); If the program does not compile, add the ctime library



Now the program gave out 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:



image



It remains to write the code responsible for its guessing.



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



image



“In the thirteenth line we declared the variable i (from input), similar to the variable u. In it we will store the entered number. Actually input is carried out in the 16th line. The loop is declared with the do keyword. Everything that is enclosed in curly brackets will be repeated until ( while ) the value of the variable i is not equal to u ". As for this part of the code, the typical mistakes of the students were such. First, they put round braces instead of curly braces. Secondly, the comparison operation “! =” Was written separately. After compiling the program, the children persistently tried to guess the number u. It struck me that the girl who previously wrote “I want to eat” did it very successfully. Of the runtime errors, I was glad to see the following:



image



This allowed me to explain that the program does not validate the input data, and to enter letters when only numbers are expected from us is not a good idea.



We came to the finish line. It remains to add tips. I wrote two “ifs” on the board and explained. “If the entered number is greater than the number, we output the corresponding message (line 17). If the entered number is less than the guess, we do the same (line 18). ” Plus, I expanded the output of the message about the end of the“ game ”.



image



It was the final text of the program, which was typed in the first programming lesson 10-per class. The program is far from perfect. In particular, I do not like the message “Your number is more!” And “Your number is less!”. They are really confusing. If I had a second chance to hold such a lesson, I would have formulated differently.



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



image



Results summarize.



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



2. I got a new teaching experience. For the last two years I have been giving lectures 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 study (and life in general) is different, and my highly specialized subjects - the material I give will in the future be useful for 2–3 our graduates from each group. Here, there is hope that this particular lesson will arouse interest in programming for one or two students.



3. The school curriculum is completely different than the one I studied. Yes, I did not go to a simple school. In the seventh grade, we studied Logo, in the eighth grade - BASIC, and in the ninth grade - Pascal. But, nevertheless, even those of my classmates who did not shine with knowledge in other subjects (and I didn’t shine too!), I liked computer science. I am sure that it is necessary to give programming in school. It perfectly develops the brain and allows you to understand computers (without which we can no longer imagine our life) on the other side.



4. The C ++ language has a high threshold of entry. One lesson is not enough to reveal the basics of this programming language. Yes, I do not know C ++. I adore C, and when I need OOP, I write in Java. But studying C ++ in a higher education institution is most likely necessary (C in my humble opinion is necessary). Again, much depends on the university and specialty.



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



PS Have an idea to write another article about computer science at school. If you support in the comments, the article most likely (I will not promise) will see the light.

Source: https://habr.com/ru/post/330744/



All Articles