ASD_I-Course-Chapter_2-Simple_Sequential_Algorithms
ASD_I-Course-Chapter_2-Simple_Sequential_Algorithms
Chapter 2
Noureddine AZZOUZA
1
Course
Topics
1. Structure of an algorithm
3. Basic actions
3.1 Assignment
3.2 Expressions
Natural
Language
Algorithmic
Language
4
ASD I Noureddine AZZOUZA
Algorithmic Language
Algorithmic Language
Structure of an Algo
Properties :
A common language;
Principle of communication;
5
ASD I Noureddine AZZOUZA
Structure of an Algorithme
Structure of an Algorithme
Structure of an Algo
Type 𝑫𝒆𝒄𝒍𝒂𝒓𝒂𝒕𝒊𝒐𝒏 𝒐𝒇
Environment Const 𝒗𝒂𝒓𝒊𝒂𝒃𝒍𝒆𝒔(𝒐𝒃𝒋𝒆𝒕𝒔)
𝒂𝒏𝒅 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏𝒔
Var
begin
𝑰𝒏𝒊𝒕𝒊𝒂𝒍𝒊𝒛𝒂𝒕𝒊𝒐𝒏𝒔
-
-
- 𝒐𝒋𝒆𝒄𝒕𝒔
Body - 𝒑𝒓𝒐𝒄𝒆𝒔𝒔𝒊𝒏𝒈 𝒂𝒏𝒅
- 𝒎𝒂𝒏𝒊𝒑𝒖𝒍𝒂𝒕𝒊𝒐𝒏
-
- 𝑹𝒆𝒔𝒖𝒍𝒕𝒔 𝒂𝒏𝒅 𝒐𝒖𝒕𝒑𝒖𝒕
end.
6
ASD I Noureddine AZZOUZA
Structure of an Algorithme
Comments
Structure of an Algo
Syntax
//
/*
*/
7
ASD I Noureddine AZZOUZA
Structure of an Algorithme
//les sorties
write ( ‘ le carré de ‘,N,’ est ‘, carre);
end.
8
Structure of an Algorithme
Fonction principale
Déclaration des
begin variables //lecture des entrées
//lecture des entrées scanf("%d", &N);
ReadLn(N);
carre = N*N;
carree := N*N; Corps
{ /* écriture et affichage
Corps
écriture et affichage des résultats */
des résultats printf("le carre de %d est %d", N, carre);
}
WriteLn('le carre de ',N,' est ', carree); return 0;
}
end.
9
Objects
Variables, Const,
Types
10
Variables, Constants and Types
11
ASD I Noureddine AZZOUZA
Variables, Constants and Types
63999
Memory 63998
63997
Representation
14792 10 i
14791 14.75 Note
14790 0 J
14789
14788 Ali Nom
2
1
0
12
ASD I Noureddine AZZOUZA
Variables, Constants and Types
Objects: Definition
Variables, Constants and Types
A Type: which indicates the nature of the set in which it takes its
values
Objects: identifiers
Variables, Constants and Types
The identifier can be: program name, variable and constant names,
function names
Can contain letters and numbers
No (most) punctuation marks
Examples :
Programming: Identifiers
Variables, Constants and Types
3. Il doit exclusivement être composé des: Les noms des variables et fonctions ne
minuscule.
15
Variables, Constants and Types
Objets: Categories
Variables, Constants and Types
Constants Variables
during the execution of an algorithm .
16
ASD I Noureddine AZZOUZA
Variables, Constants and Types
Constants
Variables, Constants and Types
Declaration
Const =
Examples
Algorithm exemple_const;
Const pi = 3.14
cent = 100
Lettre = 'M'
Titre = 'Résultat :'
begin
...
...
end.
17
ASD I Noureddine AZZOUZA
Variables, Constants and Types
PASCAL C
Variables, Constants and Types
Const = #define
begin /*
//... ...
*/
end. return 0;
}
18
Variables
Variables: Types
Variables, Constants and Types
integer
real
Standard
booleen
Simple caractere
Types
Structured enumerated
Non
standard
interval
19
ASD I Noureddine AZZOUZA
Variables
Variables
Variables, Constants and Types
Declaration
Var : Type
Examples
Algorithme exemple_vars;
Var C : character;
N, i, j, jour, mois: integer
x, y, racine : real
trouver : boolean
begin
...
...
end.
20
ASD I Noureddine AZZOUZA
Types
Numerical Types
Variables, Constants and Types
Numerical Types
Variables, Constants and Types
Declaration
Var : integer
: real
Examples
Algorithm exemple_vars_num;
Var N, i, j, jour, mois: integer;
x, y, racine : real;
begin
...
...
end.
22
ASD I Noureddine AZZOUZA
Numerical Types
PASCAL C
Variables, Constants and Types
integer : byte, shortint, integer, longint integer : short, int, long, double long
real : real, double, extended real : float, double, long double
23
Types
Alphanumeric Types
Variables, Constants and Types
24
ASD I Noureddine AZZOUZA
Types
Alphanumeric Types
Variables, Constants and Types
ASCII Code
25
ASD I Noureddine AZZOUZA
Types
Alphanumeric Types
Variables, Constants and Types
Declaration
Var : character
: string
Examples
Algorithme exemple_vars_aplha;
Var c, aplha: character;
jour, mois, nom: string;
begin
...
...
end.
26
ASD I Noureddine AZZOUZA
Alphanumeric Types
PASCAL C
Variables, Constants and Types
//...
//
end. return 0;
27
}
Types
Boolean Type
Variables, Constants and Types
Var : Boolean
Examples
Algorithme exemple_vars_bool;
Var trouver, continue: boolean;
begin
...
...
end.
28
ASD I Noureddine AZZOUZA
Boolean Type
PASCAL C
Variables, Constants and Types
int trouver = 0;
begin
//... if (trouver){
trouver := true; printf("trouver est VRAI");
WriteLn('trouver = ', trouver); }else{
// Affiche : trouver = TRUE printf("trouver est FAUX");
}
//...
return 0;
end. }
29
Basic
Instructions
30
Basic Instructions
Assignment
Basic Instructions
A constant 14790 0
← exam ← Note
31
ASD I Noureddine AZZOUZA
Basic Instructions
Assignment
Basic Instructions
2) Assign and store the result in the variable to the left of the symbol ←
Examples
Algorithm exemple_affect;
Var n, m, l: integer
begin
...
n ← 10
m← n
l ← n*2 + m*3
...
32 end.
ASD I Noureddine AZZOUZA
Assignment
PASCAL C
variable ← expression variable = expression
Basic Instructions
int a, b, c;
begin
//... a = 5;
a := 5; b = a + 3;
b := a + 3; c = a * b;
c := a * b;
printf("a = %d", a); // Affiche : a = 5
WriteLn('a = ', a); // a = 5 printf("b = %d", b); // Affiche : b = 8
WriteLn('b = ', b); // b = 8 printf("c = %d", c); // Affiche : c = 40
WriteLn('c = ', c); // c = 40
return 0;
//... }
end.
33
Basic Instructions
Expressions
Basic Instructions
Syntax
=
34
ASD I Noureddine AZZOUZA
Basic Instructions
Expressions : Arithmetic
Basic Instructions
Operator :
+ : addition - : soustraction
* : multiplication / : division
35
ASD I Noureddine AZZOUZA
Expressions : Arithmetic
PASCAL C
Opérateurs : +, - , * , / , DIV (Division Opérateurs : +, - , * , / (Division entière) ,
entière), MOD (Modulo) % (Modulo), ++ (Incrément),-- (Décrément)
Basic Instructions
int a, b, c;
begin
//... a = 15;
a := 15; b = a++;
b := a + 1; c = b % 3;
c := b MOD 3;
printf("a = %d", a); // Affiche : a = 15
WriteLn('a = ', a); // a = 15 printf("b = %d", b); // Affiche : b = 16
WriteLn('b = ', b); // b = 16 printf("c = %d", c); // Affiche : c = 1
WriteLn('c = ', c); // c = 1
return 0;
//... }
end.
36
Basic Instructions
Expressions : Alphanumeric
Basic Instructions
Operator :
+ : concatenation
Examples
Algorithm exemple_alpha;
Var c1, c2: chaine
begin
...
c1 ← “nom” + “prénom” // résultat : “nomprénom”
c2 ← “nom” + “ “ + “prénom” // résultat : “nom prénom”
...
end.
37
ASD I Noureddine AZZOUZA
Expressions : Alphanumeric
PASCAL C
Opérateurs : + Opérateurs :
Basic Instructions
begin return 0;
//... }
nom := 'BENALI';
prenom := 'Karim';
end.
38
Basic Instructions
Expressions : logic
Basic Instructions
Operator :
NON : negation
OR : logic « OR »
39
ASD I Noureddine AZZOUZA
Expressions : logic
PASCAL C
Opérateurs : AND, OR, NOT Opérateurs : && (AND), || (OR), ! (NOT)
Basic Instructions
begin return 0;
//... }
end.
40
Basic Instructions
Expressions : relational
Instruction de base
Operator :
41
ASD I Noureddine AZZOUZA
Expressions : relational
PASCAL C
Opérateurs : <, <=, >, >=, =, <> Opérateurs : <, <=, >, >=, ==, !=
Basic Instructions
begin return 0;
//... }
end.
42
Basic Instructions
Expressions
Basic Instructions
43
ASD I Noureddine AZZOUZA
Basic Instructions
Expressions : Evaluation
Basic Instructions
44
ASD I Noureddine AZZOUZA
Basic Instructions
1. We start with operators that have the highest hierarchy and then
move on to those with immediately lower hierarchy, and so on.
45
ASD I Noureddine AZZOUZA
Basic Instructions
Expressions : Evaluation
Basic Instructions
46
ASD I Noureddine AZZOUZA
Basic Instructions
Examples
Algorithm exemple_affect;
Var n, m, l: integer
begin
read (n, m)
read (l)
47
...
end. ASD I Noureddine AZZOUZA
Inputs/outputs: Reading
PASCAL C
Syntaxe: Read, ReadLn Syntaxe: scanf (<stdio.h)
Basic Instructions
//... //...
end. return 0;
}
48
Basic Instructions
Examples
Algorithm exemple_affect;
Var n, m, l: integer
begin
write (n, m)
write (‘la somme =‘, n+m)
49
...
end. ASD I Noureddine AZZOUZA
Inputs/outputs: Writing
PASCAL C
Syntaxe: Write, WriteLn Syntaxe: printf (<stdio.h)
Basic Instructions
Chapter 2
Noureddine AZZOUZA
51