Professional Documents
Culture Documents
FP урок 01 new en
FP урок 01 new en
Lesson 1
Introduction
to Programming
Contents
1. Prolegomena ................................................................. 4
Opening Remarks..................................................................4
Historical Facts.......................................................................5
2. First View of Algorithms............................................... 9
The Concept of an Algorithm..............................................9
Algorithm Properties...........................................................10
Types of Algorithms: Linear, Branched, Cyclic................11
3. Graphical Description of an Algorithm...................... 14
The Concept of a Flowchart................................................14
Basic Symbols in Flowcharts..............................................14
4. Installing Microsoft Visual Studio 2019..................... 19
5. The First Project.......................................................... 27
Scenario 1. Commands are written
in an interpreted language..................................................28
Scenario 2. Commands are written
in a compiled language........................................................29
2
Сontents
Lesson materials are attached to this PDF file. In order to get access
to the materials, open the lesson in Adobe Acrobat Reader.
3
Lesson 1
1. Prolegomena
Opening Remarks
Welcome to the world of programming! What is pro-
gramming? Perhaps, each of you has heard this word. To
paraphrase a famous quote, we can say: “Programmer, that
sounds proud!” And so it is. If you came across job-offer-
ing websites when surfing the Internet, you probably paid
attention to the payment that was offered to programmers.
And you might have wondered, why do programmers have
higher salaries? All according to the laws of the market:
there is a demand for programmers, but the number of
good specialists is too small at the moment. Of course, if
one could become a programmer in a short period of time
just by having memorized a number of terms, the profes-
sion would not be so popular. But do not worry! We, your
teachers, will try to do everything we can in order to not only
tell you how to code, but also teach you how to incorporate
programming in your life. However, our efforts should be
mutual. A teacher, no matter how hard he tries, will not be
able to teach the student, if the latter does not want it. Only
constant self-cultivation will help you master every aspect
of programming.
We sincerely hope that you will be fond of the magical
world of programming!
Before proceeding to the study of any science, it is always
necessary to find out how this science appeared, how it has
developed, what its roots and value in history are.
4
1. Prolegomena
Historical Facts
A 19th-century English mathematician Shanks spent more
than 20 years of his life to calculate π that was accurate to
707 significant digits after the point. This result got the glory
of the calculation record in the 19th century. However, later
it was discovered that Shanks made a mistake in the 520th
digit, and therefore all subsequent significant digits were
calculated incorrectly.
In 1804, a French inventor Joseph Marie Jacquard created
a “programmable” weaver’s loom. Punched cards interconnected
in the shape of a tape were used to control the loom. Guided
by the holes in the punched cards arranged in a different order,
wooden studs of the machine “reading device” determined
which threads were to lift, and which threads were to lower
in order to obtain the desired pattern. In 1890, a US inventor
Herman Hollerith developed an electromechanical tabulating
machine for punched cards. It was used for the tabulation
of results of the US population census. A tabulator compa-
ny founded by Hollerith later turned into the corporation
International Business Machines (IBM).
In 1936, a 25-year-old student from Cambridge University,
an Englishman Alan Turing, published a paper “On computable
numbers” which considered a hypothetical device (“Turing
machine”), suitable for solving any solvable mathematical or
logical problem — a prototype of a programmable computer.
In 1941, a German engineer Konrad Zuse built an op-
erational computer Z3, which was using a binary system.
Programs were recorded on a punched tape.
In 1945, a physicist John Mauchly and an engineer Prosper
Eckert from the Higher Technical School, University of
5
Lesson 1
6
1. Prolegomena
7
Lesson 1
8
2. First View of Algorithms
2. First View
of Algorithms
The Concept of an Algorithm
We solve simple problems every day. How do we manage
to do everything? We plan our day minute by minute to make
the most of our time. We set priorities, determine the sequence
of our actions. And all this is done in order to finally get the re-
sult we need. It turns out that we are constantly confronted
with instructions, prescriptions, recipes, rules, according to
which this or that human activity occurs.
The executor of the algorithm is usually a computer, but this
is not always the case. Imagine a simple situation — cooking
breakfast. In this case, a human is the executor. What will be
his actions if he wants to cook scrambled eggs?
1. Turn on the oven;
2. Put the frying pan on the stove;
3. Pour oil in it;
4. Heat up the frying pan;
5. Break 2 eggs;
6. Fry them for 5 minutes over low heat.
Having performed this sequence of actions, we get a hot
and tasty breakfast. But what will happen if we accidentally
swap steps two and three? This will result in a dirty oven and
a bad mood for the entire day. So the algorithm is a clear
and strict sequence of actions, leading to the desired result.
Let’s imagine the work of a large plant for assembling cars for
9
Lesson 1
Algorithm Properties
The algorithm is created so that the executor can follow
the instructions precisely and obtain the desired result effi-
ciently. Therefore, the algorithm has several basic requirements
in the form of its properties.
Result. First of all, when writing an algorithm, we must
aim at obtaining the desired result.
Correctness. Besides, the result should be correct (a break-
fast or a dirty stove?) After all, when the algorithm is being
compiled, we can determine the possible result in advance. If
after executing the instructions we get a result that is different
from the expected one, it is most likely that either the sequence
of actions or the actions themselves were incorrect.
Accuracy. All our actions must be specified accurately,
without double meaning. Otherwise, the executor of such
an algorithm will not understand how long it takes to fry eggs
or how many eggs to break into a frying pan.
Clarity. The algorithm’s executor must fully understand
what actions he is compelled to perform. Otherwise, such
an algorithm will be useless. Imagine that we show our reci-
pe to a foreigner. He cannot understand what we want from
him and is unable to fulfill the task assigned to him. So, it is
necessary to use one language for communication. To create
10
2. First View of Algorithms
Algorithm
Result Correctness
Accuracy Clarity
Discreteness Generality
Figure 1
11
Lesson 1
Linear Cyclic
Branched
Figure 2
12
2. First View of Algorithms
13
Lesson 1
3. Graphical Description
of an Algorithm
The Concept of a Flowchart
One of the scientific studies states that in regards to the per-
ception of information, most people are visuals. That is, data
analysis is easier to study not as text but as graphics. A few
years ago, when the use of gadgets was not so popular, one of
the problems for most people was orientation on the terrain.
Sometimes one had to explain several times how to get to
the right place. In the end, the cleverest got a sheet of paper
and drew a roadmap. The use of arrows and names of streets
allowed solving this small problem.
So, the graphical method is unique in that you can
quickly determine the essence of the entire algorithm by
just looking at it. Therefore, flowcharts are used to explain
most algorithms.
A flowchart is a graphical way of describing an algorithm.
It consists of several functional blocks, and each of them fulfills
its clear purpose.
14
3. Graphical Description of an Algorithm
START/END
Linear
Branched
INPUT/OUTPUT
Cyclic
ACTION
CONDITION
LOOP
START
LOOP
END
Figure 3
15
Lesson 1
START
END
Figure 4
START
END
Figure 5
16
3. Graphical Description of an Algorithm
17
Lesson 1
START
YES NO
IS IT GREEN?
END
Figure 6
18
4. Installing Microsoft Visual Studio 2019
4. Installing Microsoft
Visual Studio 2019
Microsoft Visual Studio is a package of software products
by Microsoft, which is used for the development of various
solutions: from students’ lab researches to corporate projects.
Visual Studio includes an integrated development environment
(IDE), numerous tools, and utilities. Using Visual Studio, you can
create console applications as well as applications with complex
graphics interface. We will use Visual Studio products as a part of
our learning process. The history of Visual Studio began in 1997,
the year when Visual Studio 97 was released. You will learn to
program using Microsoft Visual Studio 2019 which is the current
version at the moment. Before we consider the installation of
Visual Studio, let us analyze an issue related to the editions of
Visual Studio. Visual Studio edition is a version of Visual Studio,
which differs from another edition with its set of features. Visual
Studio 2019 has the following editions: Enterprise, Professional,
Community. Enterprise is a complete edition of Visual Studio.
Each edition has its own pricing policy. The only exception
is Community. This is a set of Visual Studio(s) “sharpened” to
meet specific needs, for example, the development of mobile
solutions, web-based solutions, or desktop solutions, etc. You
can use Community absolutely free, including commercial
development. It is important to note that different editions
of Visual Studio are available in different languages. For in-
stance, you may want to install the Cambodian localization (if
Cambodian is your mother tongue). Do not do this! As a part
19
Lesson 1
Figure 7
Figure 7.1
20
4. Installing Microsoft Visual Studio 2019
Figure 7.2
21
Lesson 1
Figure 8
22
4. Installing Microsoft Visual Studio 2019
Figure 9
23
Lesson 1
Figure 10
Figure 11
24
4. Installing Microsoft Visual Studio 2019
Figure 12
Figure 13
25
Lesson 1
Figure 14
26
5. The First Project
5. The First Project
A friend of mine once told me about his impressions of
visiting Prague: “If you meet a waiter, who does not speak
Russian, then take the menu and point your finger on the things
you want to order”. Sometimes similar words mean different
things. For example, the Russian word “fruit” sounds almost
like “vegetables” in Czech. Guess what the waiter will bring
if you ask him for a vegetable salad? J”.
That friend of mine wanted to warn his friends about
a possible mistake, but he would hardly be able to help if we
had to communicate with a computer instead of a waiter. Alas,
the latter does not know at what menu item you are poking
your finger. The computer thoroughly follows clear instruc-
tions. These instructions must be given by means of special
commands. The commands, in turn, make up the whole in-
dependent languages. Languages understood by the computer
are called programming languages.
Conclusion: in order to find a common language with
a Czech waiter, one has to be a ready-witted person. But
to find a common language with a computer, one must
know the language.
You already know from the first section that C++ is a pro-
gramming language. The language that allows us to explain
to the computer what we want from it. Although to be per-
fectly frank, the computer understands only one language,
i.e., the language of machine codes. Let’s take as an example
27
Lesson 1
28
5. The First Project
29
Lesson 1
Figure 15
30
5. The First Project
Now we are going try to create a project that will become our
program eventually. We will figure out what a project is later when
we write large programs. Until then, we will think of a project
as an association of several files. Now let us do it step by step:
■■ Once you have loaded the shell, go to File->New->Project.
You will see a dialog box.
■■ The Create New Project dialog box that opens offers you
a list of languages. Select C++ and Windows Desktop Wiz-
ard as a project type (Figure 16).
Figure 16
31
Lesson 1
Figure 17
Figure 18
32
5. The First Project
Figure 19
33
Lesson 1
Figure 20
34
5. The First Project
/* a comment */
/* This line adds the library
iostream to the program. A library is a file that
contains a description of various functions
implemented by other programmers.
The program can use functions contained in
the iostream library now */
#include <iostream>
/* C++ has a concept of namespaces.
This space defines a certain area,
at which actions of a statement or
function are aimed. In order to use a statement
located in a certain namespace, you need to
add this space to your program.
The namespace called std is added below */
cout<<"Hello, World!\n";
// There is a semicolon at the end of the command.
// This symbol MUST end every command in C.
// return 0 to the operating system, which means
// that the program has ended without errors
return 0;
}
35
Lesson 1
Figure 21
Figure 22
36
5. The First Project
machine codes, and you can run it for execution now. Running
a program for execution is a very simple procedure. In the Debug
menu, select Start Without Debugging (Figure 22).
The program will work, and you will see a window like
this (Figure 23):
Figure 23
Figure 24
37
Lesson 1
Figure 25
38
6. Data Output
6. Data Output
You already know that we can display various text lines
using the cout<< command. However, to make the compiler
understand this command, you must keep in mind the fol-
lowing main points:
1. The header of the program should contain the line
#include <iostream>
39
Lesson 1
40
6. Data Output
My name is — Ira.
I’m from Odessa
My eyes are blue
That’s all!!!
\b' // Backspace
\n' // New line
\t' // Horizontal tab
\\' // Backslash \
\"' // Double quotation mark "
\'' // Single quotation mark '
// Header
#include <iostream>
// Main function
int main()
{
/* The following command adds 4 tabs, prints the
Escape Sequences text, and moves to a new line
*/
41
Lesson 1
cout<<"\t\t\t\tEscape Sequences\n";
// Prints a blank line
cout<<"\n";
/* After 2 tabs, prints the text for \b, then
Backspace after another tab. Then \n moves
the output to the next line
*/
cout<<"\t\t\\b"<<"\tBackspace\n";
// Prints a blank line
cout<<"\n";
/* After 2 tabs, prints \n and
New line after 1 more tab
Then \n moves to a new line
*/
cout<<"\t\t\\n"<<"\tNew line\n";
// Prints a blank line
cout<<"\n";
/* After 2 tabs prints \t and
Horizontal tab after one more tab. Then \n moves
to a new line
*/
cout<<"\t\t\\t"<<"\tHorizontal tab\n";
// Prints a blank line
cout<<"\n";
42
6. Data Output
P. S.
You must have noticed that we use only Latin characters
when printing data. The thing is that each character in any op-
erating system has a numerical code. And the system identifies
a character by its code. Cyrillic character codes in the console
43
Lesson 1
44
7. Data Types
7. Data Types
After you have read the previous sections of the lesson,
you can easily write a program that displays something on
the screen.
// Header
#include <iostream>
// Main function
int main()
{
// prints "Congratulations on good start!!! :)"
// the phrase prints after 3 tabs,
// and then two blank lines are added
cout<<"\t\t\tCongratulations on"
"good start!!! :)\n\n";
return 0;
}
45
Lesson 1
46
7. Data Types
Table 1
Size in
Description Types Range of values
bytes
Describes integers int 4 from -2147483648
to 2147483647
Describes short integers short 2 from -32768 to 32767
Describes long integers long 4 from -2147483648
to 2147483647
Describes long integers long 8 from –9,223,372,036,
long 854,775,808
to 9,223,372, 036,854,775,807
Character Type
The type is intended for storing only one character.
Table 3
Logical Type
The type is intended for storing logical (boolean) data.
We will look at it in detail later. Logical data can take one of
two values: true or false.
47
Lesson 1
Table 4
Size in
Description Types Values
bytes
Describes logical (boolean) values bool 1 true false
48
8. Variables and Constants
49
Lesson 1
50
8. Variables and Constants
cout<<variable_name1<<variable_name2;
// no quotes in this case
51
Lesson 1
<<"Text"<<variable_name2<<"\n";
What does this line mean? The word const stresses out that
a constant is being declared. The word int says that the constant
52
8. Variables and Constants
53
Lesson 1
// Header
#include <iostream>
// define the namespace that contains cout<<
using namespace std;
// Main function
int main()
{
// prints a blank line
cout<<"\n";
// Declare integer constants
int DayIn_2000Year=366;
int HourInDay=24;
// declare an integer variable
int HourIn_Year2000;
// Calculate the desired value and
// put it in the variable HourIn_Year2000
HourIn_Year2000=DayIn_2000Year*HourInDay;
// print the value of the variable
// HourIn_Year2000
cout<<"\t\t In 2000 year "<< HourIn_Year2000;
cout<<" hours\n ";
return 0;
}
float Weight;
Weight=12.3452;
double weight_atom;
weight_atom= 2.5194е+017;
54
8. Variables and Constants
// Header
#include <iostream>
// define the namespace that contains cout<<
using namespace std;
// Main function
int main()
{
// Declare the Discount variable
float Discount=0.05;
// Declare the variable
Cost float Cost=10.50;
// Declare the variable
Count int Count=5;
55
Lesson 1
// Declare the variable
Price float Price;
// Calculate the value of the Price variable
Price=Count*Cost-Count*Cost*Discount;
// Output the total price of the item considering
// the discount
cout<<"Please, pay:"<<Price<<"\n";
return 0;
}
Figure 26
// Logical variable
bool Flag;
Flag=true;
// One character is always specified in single quotes
56
8. Variables and Constants
char Symbol='A';
/* Escape sequence is considered by the compiler as
a single character and can be accordingly written
in the variable or constant of
the char type
*/
const char NewLine='\n';
cout<<NewLine // prints a blank line
57
Lesson 1
9. Data Input
You are already familiar with the operation that displays
information on the computer screen — cout, but most pro-
grams require not only to display information on the screen
but allow entering data into the computer from a keyboard.
The previous section showed the program for calculating a dis-
count. Naturally, it would be convenient to enter parameters
such as price and quantity of the goods from the keyboard at
the stage of the program execution. Let’s see how you can do
this. If we need to enter data into the computer, we will use
the command cin. How do we use it?
The syntax of the input statement is as follows:
cin>>variable_name;
cin>>variable_name1>>variable_name2>>... >>
variable_nameN;
58
9. Data Input
// Header
#include <iostream>
// define the namespace that contains cout<<
using namespace std;
// Main function
int main()
{
// Declare the Discount variable
float Discount=0.05;
59
Lesson 1
// Header
#include <iostream>
// define the namespace that contains cout<<
using namespace std;
// Main function
int main()
{
// Invitation: "Let’s play!"
cout<<"Let’s play!\n";
60
9. Data Input
// Input of the number
cin>>i;
return 0;
}
Let’s play!
Enter a number: 67 I have 68
I’m a winner!
Press any key to continue...
61
Lesson 1
If you replace i+1 with i-1, then the player will always win,
because the number displayed by the computer will always be
one less than the number entered by the player.
In conclusion, we want to draw your attention to the op-
erators + (plus) and – (minus). They are used for addition
and subtraction. C++ also has a division operator: /. This
information will be helpful in your homework assignment,
and we will talk about operators and statements in more detail
in the next lessons.
62
10. Literals
10. Literals
Literals are fixed values that the program is unable to
change. Each C++ type has a literal, including character and
boolean, integers, and floating numbers.
Some examples:
5 integer literal — int
5l l or L means long
true Boolean literal — bool
5.0 floating-point literal, is thought of as double
5.0f f or F is floating-point, is thought of as float
0.3е–2 floating-point literal «double»; e or E separate the exponential
part
'd' character literal
«Visual» string literal — a quoted set of random characters.
The compiler takes it as a set of characters and does not
execute it, even if the quotes contain some keywords or
operations.
63
Lesson 1
11. Homework
1. Write a program that would display a text table (use escape
sequences):
Figure 30
64
11. Homework
Input data:
The battery capacity of the smartphone (mAh) -> 3000
––––––––––––––––––––––––––––
Output data:
The minimum capacity is 9000 mAh.
65
Lesson 1
Introduction to Programming
© Tatyana Lapshun
© STEP IT Academy,, www.itstep.org
All rights to protected pictures, audio, and video belong to their authors
or legal owners.
Fragments of works are used exclusively in illustration purposes to the
extent justified by the purpose as part of an educational process and
for educational purposes in accordance with Article 1273 Sec. 4 of the
Civil Code of the Russian Federation and Articles 21 and 23 of the Law
of Ukraine “On Copyright and Related Rights”. The extent and method
of cited works are in conformity with the standards, do not conflict with
a normal exploitation of the work, and do not prejudice the legitimate
interests of the authors and rightholders. Cited fragments of works can be
replaced with alternative, non-protected analogs, and as such correspond
the criteria of fair use.
All rights reserved. Any reproduction, in whole or in part, is prohibited.
Agreement of the use of works and their fragments is carried out with the
authors and other right owners. Materials from this document can be used
only with resource link.
Liability for unauthorized copying and commercial use of materials is
defined according to the current legislation of Ukraine.