Cours16122021 Algos2 Angr - En-1
Cours16122021 Algos2 Angr - En-1
a-Introduction:
To solve a computer problem and solve it by a computer program, it is necessary to follow
certain steps, procedures. These procedures and instructions are commonly known by the
word "Algorithm".
b-Definition:
It is a set of steps and instructions to follow to solve a computer problem. This set of
structured instructions represents the steps to follow for thesimplificationand solving the
problem.
2
c- Example
Exercise :Find the oldest student.
The solution to this problem is:
Ask each student their age and then compare their age to the student sitting next to them.
3
In the computer field it is necessary to declare the data and variables to be used. This will
allow the control of the results obtained and also the memory space to be reserved for the
entire program. We distinguish
II.2.1. Constants
Constants are data that do not change values during the execution of the algorithm.
Example: Pi =3.14
11.2.2. Variables
The variables of an algorithm contain the information necessary for execution
sequences of the algorithm. These variables allow to memorize the values of the types. The
variables that we use in our programs do not all have the same properties, there are several
characters... We say that the variables are typed.
11.2.3. Types
4
It is necessary to specify a type for the variable, because this allows:
- On the one hand, to control their use (for example: we cannot divide thecharactersby
integers)
- And on the other hand, because it indicates that a variable must be preserved. Generally
speaking, programming languages offer the following types:
a. Integers
These variables are intended to contain positive or negative integers: 29, - 29, 5 16 25 ... In
our example we will write the declaration of an integer type variable
Syntax
Variablewhole ;
b. Real
Inthis section thevariablessaid real arevariablesdigital withof thepositive or negative digits
after the decimal point: -1.0235, 10.1156,......etc.
5
SyntaxS,X,Y
real ;
6
d. Character and character strings
Variables known as character or character string types are sequences of alphabetic or
numeric characters.
Syntax
ABC :character
e. The indexed variables “Tables”
Arrays are an entity or variable with a single name and multiple values. These
values can be accessed via an index, which is why they are calledvariablesindexed
orpaintings
Syntax
T(10):paintings real
7
î..Simple Data Operations
Beginning
2 Instructions
3 End
8
î..Reading and writing data
To allow users to interact with programs, i.e. to read keyboard input and/or display a
result on the screen, at least two instructions are required, one forRead the other
fordisplay(print).
has.Reading
The second instruction is used to prompt the user to do something or to show a result
found on the screen.
SHOW ”Text, Variable...” ;
9
Example :
AF FICHER "give your favorite hero..."
READ hero_name
AF FICHE R "your hero's name is: "hero_name".
Noticed:
For clarity in our program: Any reading instruction
will be preceded by an instruction to write an information message.
2. The assignment
It is used to pass an expression to a variable.
Variable < - - Expression
Which is read as "variable receives value" and which means that we memorize the
value at
a place named variable, this remains valid as long as the value passed and the
variablehave the same type.
The expression may be:
□A value,
□The result of a calculation,
■The contents of another variable. 10
For example: i ■ 1;
Finished■ TRUE- pi + i;
Norn-Hamid;
3. .Examples
has.“Price including tax” algorithm
{Stubborn}
(Algorithm for calculating a price with tax, tax rate: 20%}
Variable Price excluding VAT, Price including VAT: real Start
{Data Entry}
Write ('Give price excluding tax') Read (Price
HT)
{Calculation}
Price including tax < - Price excluding tax * (1 + 20 /
100)
{Results}
r
Beginning
13
2..Method ofpseudo- language or written
THEpseudo-language lies in the middle, between natural language and the
language of
programming. Thus, algorithms are expressed by a common language
(in our case it is French), but subject to some structuring rules.
Therefore, an algorithm can be described as a set of actions. The
The course of actions in a program can be controlled by three types of
fundamental structures.
{Stubborn}
Algo calcul Moy {Algorithm for calculating the average of 3 numbers}
{Declarative zone}
Variable a, b, c, avg: real {Data entry}
r
15
SOaction
The word if is underlined so that one can easily recognize such a
structure. The condition follows the
word if, while the action that only takes place if the condition is met comes
after.
After a condition, we can do one thing or another, In general we
has:ifwe are in such a caseSOwe do thisOtherwisewe do this.
b. Binary choiceifcondition
action 1
Otherwise
action 2
The underlined word otherwise precedes action 2 (or actions) to be executed
when the condition is not met;
16
II.5.3.Repetitive structure
A program almost always has the role of repeating the same action a certain number of
times. To do this, we use a structure that allows us to say "Perform such actions until
such condition is met".
In general, programming languages offer three waysFor
represent thestructuresiterative:
c.To . . . Do
Very often weusedastructurerepetitive with a counter and
we stop when the counter has reached its final value.
Inpseudo-language is thestructureFOR :
ForvariableRanging frominitial valueHASfinal value [Not[step value]
Do
Actions
End ForExample
17
For i Going from 1 to 10 Do
s ■ s+ 1 End For
Finpour
a. L e As Long As
In pseudo-language, we writeAs long ascondition
Do
Actions
FTQ
Which means: as long as the condition is true, the actions are
executed.
Example :
18
As long as(k< 10)
Do
Show Hello everyone
k ■ k+ 1
FTQ
c. To do ... Until In pseudo-language, we write
Do
Actions
Untilcondition
Which means that the actions are executed until the condition is true.
Example :
Do
19
j "j + l
Show long live programmingUntilat(j>
10)
20