Chapitre 4 (Introduction to Algorithmics)
Chapitre 4 (Introduction to Algorithmics)
CHAPTER 4
INTRODUCTION TO ALGORITHMICS
1
INTRODUCTION TO ALGORITHMICS
CHAPTER OUTLINE
Algorithmics ?
3
I) FUNDAMENTALS AND BASIC CONCEPTS
1. INTRODUCTION
▪ The program will be the translation of the algorithm into a programming language,
➢ In other words, a language simpler than natural language (pseudo code) in its
syntax, unambiguous, which the machine can use and transform to execute the
actions it can describe.
LAROUSSE :
Implementation of a program :
▪ Solving a given problem involves the following sequence of steps:
Language Code
Compilation
pseudocode
Machine Code
Execution
5
I) FUNDAMENTALS AND BASIC CONCEPTS
1. INTRODUCTION
Implementation of a program :
▪ During the process of writing a program, you can encounter two types of errors:
1. Syntax errors: These errors are noticed during compilation and result from
incorrect syntax in the programming language.
2. Semantic errors: These errors are noticed during execution and result from faulty
analysis. These errors are more serious as they can occur during the program's
runtime operation.
6
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
Algorithm Algorithm-Name
Variables:
Declaration of variables and constants
Initialize variables
Declaration of functions
Begin
Read input values (if any)
List of instructions
Display/write output values (if any)
End 7
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
▪ A variable is a named memory space of a fixed size, which takes an indefinite number
of different values during the algorithm's execution.
▪ This change of value is done through the assignment operation denoted by (←).
▪ A variable differs from the concept of a constant which, as the name implies, takes
only one unique value during the execution of the algorithm.
▪ Declaration: it allows specifying which variables will be used during the algorithm as
well as the type of value they must respectively take.
8
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
❖ Integers
▪ The possible arithmetic operations on this type are: ‘+’, ‘-‘, ‘*’, ‘/’
▪ These operations, when applied to integer operands, provide an integer result except
for the ‘/’ operation which gives a real result.
▪ Meanwhile, the relational operations denoted by ‘<’, ‘>’, ‘>=’, ‘<=’, ‘<>’ provide a logical
result
9
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
❖ Integers
Algorithm Example_Integers
Example:
Variables:
Constant x ← 1
a : integer
b, c : integers
coefficient : integer
Begin
………
End
10
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
Begin
………
End
11
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
Begin
………
End
12
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
Begin
………
End
13
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
▪ It is a single word composed of numbers and characters, with the exception of a few
(depending on the programming language).
▪ A keyword is a reserved word that has a special meaning in a program, such as: Begin,
End, Real, Algorithm,Variables, If, Else, Integer, ....
14
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
2.5 Instructions:
15
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
2.5 Instructions:
Variables:
Algorithm Assignment 1 A,B : integers
A←0
Variables: B←0 (Initialization)
A,B : integers
Begin
Begin A ← 10
A ← 10 B←5
B ← A+15 A ← A+ B
End End
16
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
2.5 Instructions:
❖ Write (Output)
▪ Write is the elementary action that allows an algorithm to provide results to the user. It
is done through the command (Function) Write.
Algorithm Writing 2
Examples:
Algorithm Writing 1 Variables:
A,B : integers
Variables:
……. Begin
Begin A ← 10
B ← A+15
Write (" Hello ! ") Write (B)
End
End 17
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
2.5 Instructions:
Begin
A ← 10
Read (B)
C ← A+B
End
18
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
Algorithm Exercise
Variables:
A,B,C : integers
Begin
A←1
B ← 10
C ← A*2+B-3
Write (C)
End
19
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
Algorithm Comments
▪ A comment is an optional text (sentences)
located between the two symbols { } that Variables:
has no effect on the functioning of the A,B,C : integers
algorithm.
Begin
▪ It is used to explain the rationale behind {this program calculates the sum of two numbers}
certain instructions used in a program.
Read (A)
Read (B)
C ← A+B
Write (B)
End
20
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
2.6 Expressions:
▪ Boolean (logical) expressions: These involve logical operators (AND, OR, NOT, ...) as
well as relational operators (>, <, <=, <>, ...).
21
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
2.6 Expressions:
❖ Operator Precedence
▪ In cases where two operators have the same precedence, the one on the left is evaluated
first.
▪ Opening and closing parentheses are considered to be the highest priority operators.
22
I) FUNDAMENTALS AND BASIC CONCEPTS
2. FOUNDATION OF AN ALGORITHMIC LANGUAGE
2.6 Expressions:
❖ Operator Precedence
() Parentheses
+- plus, minus
Not Logical NOT
* / Mod Multiplication, Division, Modulus
Highest
+- Addition, Subtraction
< <= > >= Relational Operators
= Equality
AND Logical AND
OR Logical OR
Lowest ← Assignment Operators
23
2) CONTROL STRUCTURES
-> INTRODUCTION
A control structure is any action that allows a group of actions to be executed under
condition(s), thus guiding the flow of a program based on the fulfillment of these
conditions.
24
2) CONTROL STRUCTURES
1. CONDITIONAL OR ALTERNATIVE STATEMENT
IF (condition) Then
Instructions (Action 1)
Else
Instructions (Action 2)
ENDIF
25
2) CONTROL STRUCTURES
1. CONDITIONAL OR ALTERNATIVE STATEMENT
IF (condition) Then
Instructions (Action 1)
ENDIF
▪ In this case :
➢ If the condition is verified, the action (or group of instructions) <Action 1> is executed then
the actions following the EndIf.
➢ On the other hand, if the condition is not verified, only the actions following the EndIf will be
executed.
26
2) CONTROL STRUCTURES
1. CONDITIONAL OR ALTERNATIVE STATEMENT
Example:
Algorithm IF_Example
Variables:
X : integer
Begin
Read (X)
IF (X ≥ 0) Then
Write (“X is positive!”)
Else
Write (“X is negative !”)
ENDIF
END
27
2) CONTROL STRUCTURES
2.THE 'WHILE' REPETITION LOOP
▪ The "While" loop specifies that an action (or group of instructions) should be repeated
as long as the condition remains true.
▪ The group of instructions <Actions> is repeated until the condition becomes false, so
the action part must be capable of modifying the parameters involved in the loop
condition in order to exit the loop.
▪ Syntax :
While (condition) Do
Instructions (Actions)
EndWhile
28
2) CONTROL STRUCTURES
2.THE 'WHILE' REPETITION LOOP
Algorithm Example
Variables:
X : integer
Begin
X←0
While (X < 10) do
Write (X)
X←X+1
EndWhile
End
29
2) CONTROL STRUCTURES
3.THE 'FOR' REPETITION LOOP
▪ This loop is used when a task needs to be performed a known number of times
▪ The action is re-executed until the value of the counter exceeds the final value.
▪ Syntax:
For (i=1 to n ) Do
Instructions (Actions)
EndFor
30
2) CONTROL STRUCTURES
3.THE 'FOR' REPETITION LOOP
Algorithm Example
Variables:
X : integer
Begin
For (x=0 to 9 ) Do
Write (X)
EndFor
End
31
2) CONTROL STRUCTURES
4.THE 'REPEAT --- UNTIL' REPETITION LOOP
▪ This loop allows repeating an action (group of instructions) until the condition is
satisfied.
▪ The instructions inside the loop are executed at least once, unlike the "while" loop
where the condition is checked before execution.
▪ Syntax :
Repeat
Instructions (Actions)
Until (condition)
32
2) CONTROL STRUCTURES
4.THE 'REPEAT --- UNTIL' REPETITION LOOP
Algorithme Example
Variables:
X : integer
Begin
X←0
Repeat
Write (X)
X←X+1
Until (X > 9)
End
33
2) CONTROL STRUCTURES
5.THE CONDITIONAL TEST WITH MULTIPLE CHOICES ‘CASE -- OF’
▪ To allow for multiple possible choices, nested if-then-else-EndIf statements are used.
34
2) CONTROL STRUCTURES
5.THE CONDITIONAL TEST WITH MULTIPLE CHOICES ‘CASE -- OF’
Algorithm Example
Variables:
nbd, month : integers
bis: boolean
Begin
Read (month, bis)
Case month of
1, 3, 5, 7, 8, 10, 12 : nbd ← 31
4, 6, 9, 11 : nbd ← 30
2 : IF (bis = true) then nbd ← 29 Else nbd ← 28 EndIF
Default : nbd ← 0
EndCase
Write (nbd)
End
35
3) ARRAYS
1. INTRODUCTION
▪ Let's imagine that in a program, we need to work with 12 values simultaneously (for
instance, grades to calculate an average).
▪ Naturally, the only current solution we have is to declare twelve variables, like Notea,
Noteb, Notec, etc.
➢ This will lead to syntactical complexity and could increase the program's size.
▪ This is why programming allows us to group all these variables into a single structure,
where each value is identified by an index.
▪ Arrays are static programming structures; the size of the array must be specified
beforehand.
36
3) ARRAYS
2. ONE-DIMENSIONAL ARRAY (VECTOR)
▪ A one-dimensional array is a collection of values that share the same variable name,
and each value is identified by a number called an index, which is used to access each
element of the array individually.
▪ Whenever we need to refer to an element of the array, we use the array's name followed
by the index of the element enclosed in square brackets.
37
3) ARRAYS
2. ONE-DIMENSIONAL ARRAY (VECTOR)
▪ A one-dimensional array is declared as shown below, specifying the number and type of
values it will contain:
Algorithm Declaration_Example
Variables:
Tab : array [1 .. 100] of integers
Grade : array [1 .. 10] of reals
Begin
……..
End
Remarks
• The cells of the array (elements) are numbered starting from 1.
• During the declaration of the array, the number of elements in the array is specified
38
3) ARRAYS
2. ONE-DIMENSIONAL ARRAY (VECTOR)
Example
Algorithm manipulation
Variables:
Grade : array [1 .. 10] of reals
Begin
Read (Grade [1])
Grade [3] ← 15,5
Write (Grade[3])
End
39
3) ARRAYS
2. ONE-DIMENSIONAL ARRAY (VECTOR)
Variables:
Solution 1:
Grade : array [1 .. 12] of reals
i: integer
Average: real
Begin
For (i=1 to 12 ) Do
Read (Grade[i])
EndFor
Average ← 0
Fro (i=1 to 12 ) Do
Average ← Average + Grade [i]
EndFor
Algorithm manipulation
Solution 2: Variables:
Grade : array [1 .. 12] of reals
i: integer
Average: real
Begin
Average ← 0
For (i=1 to 12 ) Do
Read (Grade [i])
Average ← Average + Grade [i]
EndFor
41
3) ARRAYS
2. ONE-DIMENSIONAL ARRAY (VECTOR)
Exercise 2:
• Write an algorithm that reads and inverses the content an array of 10 integer elements.
Example:
1 2 5 0 9 11 4 6 10 3
3 10 6 4 11 9 0 5 2 1
42
3) ARRAYS
2. ONE-DIMENSIONAL ARRAY (VECTOR)
Variables:
X : array [1 .. 10] of integers
i,j,temp: integers
Begin
For (i=1 to 10 ) Do
Read (X [i])
EndFor
i ← 1 j ← 10
While (i<j) Do
temp ← X [i]
X [i] ← X [j]
X [j] ← temp
i←i+1
j←j-1
EndWhile
End
43
3) ARRAYS
3.TWO-DIMENSIONAL ARRAY (MATRIX)
▪ A two-dimensional array is a collection of values with the same variable name, identified
by two indexes (coordinates).
▪ The first index is used to represent rows, and the second index is for columns.
44
3) ARRAYS
3.TWO-DIMENSIONAL ARRAY (MATRIX)
Algorithm Declaration_Example
Variables:
X : array [1..100, 1..100] of integers
Y : array [1..10, 1..47] of reals
Begin
……..
End
45
3) ARRAYS
3.TWO-DIMENSIONAL ARRAY (MATRIX)
Example
Algorithm manipulation
Variables:
X : array [1..100, 1..100] of integers
Begin
Read (X [1, 1])
X [2, 3] ← 5
Write (X[50, 90])
End
46
3) ARRAYS
3.TWO-DIMENSIONAL ARRAY (MATRIX)
Exercise: write an algorithm that fills the matrix Mat[10, 20] with randomly entered
integer values.
Algorithme exercise
Variables:
Mat : array [1..10, 1..20] of integers
i, j : integers
Begin
For (i=1 to 10 ) Do
For (j=1 to 20 ) Do
EndFor
EndFor
End
47
3) ARRAYS
2. ONE-DIMENSIONAL ARRAY (VECTOR)
Exercise2: write an algorithm that reads 12 student grades with coefficients and
calculates the student's average grade.
Algorithm student_Average
10 3
Variables:
15 2
Grade : array [1 .. 12, 1..2] of reals
i,j: integers 9 4
Average, Coefficient : reals 17 2
Begin 8 3
Average ← 0 Coefficient ← 0
14 4
For (i=1 to 12 ) Do
For (j=1 to 2 ) Do 11 3
Read (Grade [i,j]) 16 5
EndFor
Average ← Average + (Grade [i,j]*Grade [i,j-1]) 12 2
Coefficient ← Coefficient + (Grade [i,j]) 10 4
EndFor
12 3
Write (Average / Coefficient)
13 4
End
48
3) ARRAYS
3.TWO-DIMENSIONAL ARRAY (MATRIX)
Exercise 3: Write an algorithm that fills the integer matrix Mat[10, 10] with randomly
entered integer values and then calculate the sum of its diagonal.
Algorithme diagonal
Variables:
Mat : array [1..10, 1..10] of integers
i, j, sum : integers
Begin
For (i=1 to 10 ) Do
For (j=1 to 10 ) Do
Read (Mat [i, j])
EndFor
EndFor
sum ← 0
For (i=1 to 10 ) Do
sum ← sum + Mat [i, i]
EndFor
Write (sum)
End
49
3) ARRAYS
4. N-DIMENSIONAL ARRAYS
▪ An N-dimensional array is declared in the following manner, specifying the number and
type of values it will contain:
Algorithm Declaration_Example
Variables:
X : array [1..100, 1..100, 1…300] of integers ;
Y : array [1..10, 1..47, 1..12, 1..7] of reals ;
Begin
……..
End
50
4) FUNCTIONS AND PROCEDURES
1. INTRODUCTION
51
4) FUNCTIONS AND PROCEDURES
1. FUNCTIONS
52
4) FUNCTIONS AND PROCEDURES
1. FUNCTIONS
❖ Function Declaration
▪ The general structure of a function is as follows :
Begin
End
Note : At the end of a function, the result of the processing must be returned.
53
4) FUNCTIONS AND PROCEDURES
1. FUNCTIONS
▪ Variables declared in the declaration part of a function are called local variables.
▪ They are unknown to the main program where the function has been declared.
▪ Variables declared in the main program are known to the functions declared in the same
program.They are called global variables.
54
4) FUNCTIONS AND PROCEDURES
1. FUNCTIONS
▪ The parameters described in the function header during declaration are called formal
parameters. They serve to represent the structure and operation of the function.
▪ On the other hand, the values that replace them in function calls are called actual
parameters or actual arguments.
❖ Function Call
▪ To call a function, you place the function name to the right of a variable assignment, often
referred to as a return variable. The syntax is:
Variable ← Function_Name(parameters)
55
4) FUNCTIONS AND PROCEDURES
1. FUNCTIONS
❖ Usage of Functions
▪ Once declared, a function can be called from the main program or another function.
56
4) FUNCTIONS AND PROCEDURES
1. FUNCTIONS
Algorithm CNPCalculation
Variables:
cnp, n, p: integers
Function Factorial (n : integer) : integer
Variables: t, i : integer
Begin
t←1
For (i =1 to n) Do
t←t*i
EndFor
Return (t)
End
Begin
Write ("Give the value of n and p" )
Read (n, p)
Cnp ← Factorial(n) / (Factorial(n-p) * Factorial(p) )
Write ("cnp = ", cnp)
End
57
4) FUNCTIONS AND PROCEDURES
1. FUNCTIONS
Exercise 2 :
Write an algorithm that calculates the greatest common divisor (GCD) of two positive
integer numbers.
58
4) FUNCTIONS AND PROCEDURES
1. FUNCTIONS
Algorithm GCDCalculator
Variables:
x,y: integers
Begin
Read (x, y)
Write ("GCD = ", GCD(x,y))
End
59
4) FUNCTIONS AND PROCEDURES
2. PROCEDURES
▪ A procedure is very similar to a function except that it may not return any result or may
return multiple results.
❖ Declaration of a Procedure
Procedure ProcedureName (InputVar1 : type1, …InputVarN : typeN, OutputVar1: Type1,…, OutputVarN: typeN)
Variables: variable1, …VariableN : type {declaration of procedure-specific variables}
Begin
60
4) FUNCTIONS AND PROCEDURES
2. PROCEDURES
Notes
▪ The last instructions in a procedure are usually reserved for assigning the results of the
processing done by the procedure to its output parameters.
▪ The concepts of global and local variables, formal and actual parameters for procedures
have the same meanings as for functions.
▪ Calling a procedure is done by writing its name and replacing the formal parameters with
the actual parameters.
61
4) FUNCTIONS AND PROCEDURES
2. PROCEDURES
Variables:
x, y, Max: Integers
Procedure Maximum (a: integer, b: integer, c: integer)
Begin
Begin
Read (x, y)
Maximum (x, y, Max)
Write (Max)
End
62
4) FUNCTIONS AND PROCEDURES
2. PROCEDURES
Exercise 2: write an algorithm that that uses a procedure to calculate the area of a
circle based on the radius entered by the user.
Algorithm CircleAreaCalculator
Constant PI ← 3.14159
Variables:
x,y: reals
Procedure CircleArea (R: real, Area: real)
Begin
Area ← PI * R * R
End
Begin
Read (x)
CircleArea (x, y)
Write (" Circle Area = ", y)
End
63
4) FUNCTIONS AND PROCEDURES
3. CONCLUSION
Conclusion:
Subprograms (functions and procedures) are program segments that are often
repeated either within the same program or across multiple programs,
➢ Therefore, it is preferable to write them once and use them as needed. They
also help simplify the development of large programs.
64