Cours D'algorithme
Cours D'algorithme
Content :
• Overview of the lecture’s goals and syllabus.
• Understanding the concept of Algorithms and Programs.
• Pseudocode sketching and Input/Output Operations
1/38
Making a Cup of Tea
2/38
Historical Milestones
3/38
Algorithms are useful!
• The user of an algorithm simply has to follow the instructions, in the right
order, to get a solution to a given problem.
4/38
What is an Algorithm?
→ The word Algorithm comes from the Latinized name of the Persian
mathematician Al-Khawarizmi.
→ Algorithms are the basis of all computer programming.
Definition of an Algorithm
An algorithm is
• a finite sequence of well-defined,
• unambiguous,
• and executable steps or instructions,
that solve a particular problem.
Example
An algorithm must:
7/38
Manifestation of Algorithms
• Human-Human communication :
• Natural language description : Set of instruction written in human language.
• Human-Machine communication :
• Computer programs, also called codes.
8/38
From Algorithms to Programs
9/38
From Algorithms to Programs
It involves:
10/38
General Structure of an Algorithm
Syntax
Algorithm Name_of_algorithm ;
< List of variables and/or constantes > ;
Begin
< Sequence of instructions > ;
End
11/38
Units of Measurement : A Memory Cell
12/38
Binary representation: Overview
Example
Decimal 23 22 21 20
13 1 1 0 1
Binary
That is,
1 · 23 + 1 · 22 + 0 · 21 + 1 · 20 = 8 + 4 + 0 + 1 = 13 in decimal.
13/38
Binary representation: Overview
15/38
Variables and Constants
Syntax
Var <variable_ID> : <type> ;
Const <constant_ID> = <value> ;
16/38
ID choice
Remark:
The choice of variable names is subject to a few rules:
• A name must begin with an alphabetical letter.
! • Must be different from reserved words (e.g.: integer, float, else,
switch, case, for, return, etc.)
Examples
For readability, choose meaningful names that describe the data being manipulated.
17/38
Variables and Constants
Example
Var a, b : Integer;
Const pi = 3.14;
18/38
Integer Type
• This is a numeric type representing the set of relative integers, such as:
−9, 0, 31, . . . etc.
• The operations allowed on this type are: +, - , *, div (integer division) and
mod (modulo or remainder remainder of integer division).
• The keyword is: integer.
Example
Var x : integer;
19/38
float Type
Example
Var y : float;
20/38
Character Type
• This type represents all alphanumeric characters, such as ’a’, ’A’, ’3’, ’%’, space,
etc.
• Operations supported by this type: =, , <, <=, >, >=.
• The keyword is: character.
Example
Var a : character;
21/38
Boolean Type
• This type is used in logic to represent two values: true and false.
• The operations supported are: NOT, AND, OR.
• The keyword is: boolean.
Example
Var b : boolean;
22/38
Declarations Examples
Var x, y : integer;
z, w : float;
letter : character;
name : string;
Status: boolean;
Const n = 100;
arobase = ’@’;
word = "hello";
23/38
Assignment
Syntax
<variable_ID> ← <value>;
Example
x ← 8;
24/38
Assignment : Example 1
Example
Algorithm Assignment_Example ;
Var x, y : integer ;
Begin
x ← 2 ;
y ← 5 ;
End ;
x 2 y 5
25/38
Assignment : Example 2
Example
Algorithm Assignment_Example ;
Var x, y : integer ;
Begin
x ← 2 ;
x ← 1 + 2 ;
y ← x ;
y ← x + 2 ;
y ← x + y ;
End ;
26/38
Exercise : permutation of two variables
Algorithm Permutation ;
Var x, y, i : integer ;
Begin x y i
x ← 2 ; - - - - - 2
x ← 8 ; - - - - - 2 8
i ← y ; - - - - - 2 8 8
y ← x ; - - - - - 2 2 8
x ← i ; - - - - - 8 2 8
End ;
27/38
Exercise : permutation of two variables
Algorithm Permutation ;
Var x, y: integer ;
Begin x y
x ← 2; - - - - - 2
x ← 8; - - - - - 2 8
x ← x + y ; - - - - - 10 8
y ← x - y ; - - - - - 10 2
x ← x - y ; - - - - - 8 2
End ;
28/38
Arithmetic Operations
Operation Symbol
Addition +
Subtraction −
Multiplication ∗
Division /
Modulo % or mod
Integer Division div
Exponentiation ˆ or ∗∗
29/38
Arithmetic Operations
30/38
Relational Operations
32/38
Operator Precedence Order
• Input and output operations are essential in algorithms for processing data.
34/38
Input Instruction
• It allows you to read input values and assign them to variables stored in
memory.
• The values assigned are often data entered from an input device such as the
keyboard. Syntax:
Syntax
Read(<var1_ID>, <var2_ID>, ...);
It allows the algorithm to write the output data resulting from a processing
(value, text, etc.) by displaying them, for example, on an output device such as
the screen.
Syntax:
Write(<var1_ID>, <var2_ID>, <expr1>, <expr2>, ...);
Remark:
In the case of writing an expression, it is the result of the evaluation of that
! expression that is displayed, not the expression itself.
Example
Algorithm Average_of_two_floats;
Var x, y, z: float;
Begin
Write ("Enter the first value: ");
Read (x);
Write ("Enter the second value: ");
Read (y);
z <- (x + y) / 2;
Write ("The average is: ", z);
// Alternatively, you can use a single line:
Write ("The average is: ", (x + y) / 2);
// In this case, you don’t need z
End; 37/38
Example IO Operations
Write an algorithm that calculates the area of a circle using the formula: Area =
Radius * Radius * Pi.
Algorithm Circle_Area;
Const Pi = 3.14;
Var Radius: integer;
Area: float;
Begin
Write("Enter the radius value: ");
Read(Radius);
Area <- Radius * Radius * Pi;
Write("The area of the circle is: ", Area);
End;
38/38