0% found this document useful (0 votes)
17 views51 pages

ASD_I-Course-Chapter_2-Simple_Sequential_Algorithms

Course

Uploaded by

chifaachott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views51 pages

ASD_I-Course-Chapter_2-Simple_Sequential_Algorithms

Course

Uploaded by

chifaachott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Ministry of Higher Education and Scientific Research

Djilali BOUNAAMA University - Khemis Miliana(UDBKM)


Faculty of Science and Technology
Department of Mathematics and Computer Science

Chapter 2

Simple Sequential Algorithm

MI-L1-UEF121 : Algorithms and Data Structures I

Noureddine AZZOUZA

1
Course
Topics
1. Structure of an algorithm

2. Objects: Constants, Variables and Types

3. Basic actions

3.1 Assignment

3.2 Expressions

3.3 Inputs / Outputs

4. Representation of an algorithm by a flowchart

2 5. Translation into Programming Language


ASD I Noureddine AZZOUZA
Structure of an
algorithm
3
Algorithmic Language

Need for an algorithmic language


Structure of an Algo

Natural
Language

Definition Express the Organigram /


Problem Analyse
(precise statement) solution Flowchart

Algorithmic
Language

 Natural Language (literary descriptions): imprecision, ambiguity, own rules


and conventions, different explanations of the same concept ...

 Flowchart : clutter, hard to modify and update, ...

4
ASD I Noureddine AZZOUZA
Algorithmic Language

Algorithmic Language
Structure of an Algo

 An algorithmic language (or formalism) is a set of conventions (or rules)


in which we can express any solution to a given problem.

 Also called: Pseudo Code,

 Properties :

 A common language;

 Principle of communication;

 Precision and clarity (non-ambiguity)

5
ASD I Noureddine AZZOUZA
Structure of an Algorithme

Structure of an Algorithme
Structure of an Algo

Header Algorithm name_of_algorithm

Type 𝑫𝒆𝒄𝒍𝒂𝒓𝒂𝒕𝒊𝒐𝒏 𝒐𝒇
Environment Const 𝒗𝒂𝒓𝒊𝒂𝒃𝒍𝒆𝒔(𝒐𝒃𝒋𝒆𝒕𝒔)
𝒂𝒏𝒅 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏𝒔
Var

begin
𝑰𝒏𝒊𝒕𝒊𝒂𝒍𝒊𝒛𝒂𝒕𝒊𝒐𝒏𝒔
-
-
- 𝒐𝒋𝒆𝒄𝒕𝒔
Body - 𝒑𝒓𝒐𝒄𝒆𝒔𝒔𝒊𝒏𝒈 𝒂𝒏𝒅
- 𝒎𝒂𝒏𝒊𝒑𝒖𝒍𝒂𝒕𝒊𝒐𝒏
-
- 𝑹𝒆𝒔𝒖𝒍𝒕𝒔 𝒂𝒏𝒅 𝒐𝒖𝒕𝒑𝒖𝒕

end.
6
ASD I Noureddine AZZOUZA
Structure of an Algorithme

Comments
Structure of an Algo

 Gives a human description in machine code.

 Simplified code maintenance and therefore, accelerate debugging

 Important when writing functions for other users

Syntax

//
/*
*/

7
ASD I Noureddine AZZOUZA
Structure of an Algorithme

Example : Square of a Number


Structure of an Algo

Header Algorithm Carre;

Environment Var N, carre : entier ;


Declaration
begin
//les entrées
read (N);

//manipulation des données


Body carre ← N*N

//les sorties
write ( ‘ le carré de ‘,N,’ est ‘, carre);

end.

8
Structure of an Algorithme

Example : Square of a Number


Structure of an Algo

program Carre_Exemple; Entête #include <stdio.h> Déclaration des


bibliothèques
uses crt; Déclaration des int main (){
bibliothèques
Déclaration des
var N, carree : Integer; int N, carre;
variables

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

Objects: Memory Representation


Variables, Constants and Types

 Any object manipulated by an algorithm (or a program) is stored in


central memory (RAM).

 Central memory is made up of a series of contiguous boxes called


memory “boxes” or “cells”.

 Each memory box is characterized by :

 An address: unique which references the box

 A space: to store object values

11
ASD I Noureddine AZZOUZA
Variables, Constants and Types

Adress Memory Object

Objects: @ (Valeur) (Var, Const, ..)


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

 All constituent objects of an algorithm must be described or declared in


the environment (or the “declaration” part).

 Each object is characterized by :

 A Name: “unique identifier”: a series of alphanumeric characters


which allows it to be designated and distinguished

 A Type: which indicates the nature of the set in which it takes its
values

 A value: which indicates the size taken by an object at a given moment


13
ASD I Noureddine AZZOUZA
Variables, Constants and Types

Objects: identifiers
Variables, Constants and Types

 A name or identifier is a sequence of alphanumeric characters whose


first character is alphabetic

 The identifier can be: program name, variable and constant names,
function names
 Can contain letters and numbers
 No (most) punctuation marks

 Examples :

 Valid identifiers: product, i, j, T1, L_21, surface, student_name,

 Invalid identifiers: 5students, release date, x+y, T1, ...


14
ASD I Noureddine AZZOUZA
Variables, Constants and Types

Programming: Identifiers
Variables, Constants and Types

1. L’identificateur est “Unique” 1. Les meme conditions que le langage PASCAL

2. Il ne doit pas contenir des: 2. fait la distinction entre lettres minu-

 Caractères accentués, ni d'espace, scules et majuscules.

ni des caractères tels que %,?,*,.,-, 3. Par convention:

3. Il doit exclusivement être composé des:  Les noms des variables et fonctions ne

 26 lettres de l'alphabet, des 10 contiennent pas de majuscules

chiffres et le caractère de soulignement  Les constantes ne contiennent pas de

4. Un chiffre ne peut pas être placé au minuscules

début d’un identificateur.  utilise le souligné pour séparer les

5. ne différencie pas entre majuscule et mots

minuscule.

15
Variables, Constants and Types

Objets: Categories
Variables, Constants and Types

 Objects are used to store data manipulated by the algorithm

 There are two categories of objects

 Constant : it is an object whose value


Objects
is invariable

 Variable : it is an object that can vary

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

program Exemple_Const; #include <stdio.h>

const pi = 3.14; #define PI 3.14


cent = 100; #define CENT 100
Lettre = 'M'; #define LETTRE 'M'
Titre = 'Résultat :'; #define TITRE 'Résultat :'

var ... int main (){

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

 Integer : it is the set of relative integers.

 Real : It is the set of numbers having a fractional part.

Type Octets Plage

Byte (octet) 1 0 à 255


Entier simple 2 -32 768 à 32 767
Entier long 4 -2 147 483 648 à 2 147 483 647
-3,40x1038 à -1,40x1045 pour les valeurs négatives
Réel simple 4
1,40x10-45 à 3,40x1038 pour les valeurs positives
1,79x10308 à -4,94x10-324 pour les valeurs négatives
Réel double 8
4,94x10-324 à 1,79x10308 pour les valeurs positives
21
ASD I Noureddine AZZOUZA
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

program Exemple_Const; #include <stdio.h>

var x, y, z : Integer; int main (){


mois, annee : Integer; int x, y, z;
age : ShortInt; int mois, annee;
longueur, largeur : Integer; short age;
surface : Integer; float longueur, largeur;
totale : Double; float surface;
double totale;
begin
//... /*
...
*/
end. return 0;
}

23
Types

Alphanumeric Types
Variables, Constants and Types

 CHARACTER : it matches a single character (depending on the system). Character


sets may vary.

 Placed between two single quotes.


 It includes all alphabetical, numerical characters, punctuation marks, special signs,
space (blank), empty character, etc.

 STRING : It is a set (group) of characters.

24
ASD I Noureddine AZZOUZA
Types

Alphanumeric Types
Variables, Constants and Types

 Each character has an ASCII code

 The ASCII code is a table of 0 to 255 characters

which contains: lowercase and uppercase

letters, numbers, punctuation, special symbols

and graphics ...

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

character : Char character : char


String: String String : table if characters

program Exemple_Const; #include <stdio.h>

var c : Char; int main (){


mot: String; char c;
nom, prenom : String[25]; char mot[];
char nom[25], prenom[25];
code : Integer;
begin //
//... c = code;
c ← Chr(82); printf("c = %c", c); // Affiche : c = R
WriteLn('c = ',c); // Affiche : c = R
c = 'T';
code ← Ord('T'); printf("c = %d", c);
WriteLn('code = ',code); // code = 84 /* Affiche : c = 84 */

//...
//
end. return 0;
27
}
Types

Boolean Type
Variables, Constants and Types

 BOOLEAN : it is the set of values {TRUE, FALSE}.


Declaration

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

Boolean : nécessite un bibliothèque


Boolean : Boolean « stdbool.h » sinon, toutes valeur non nulle
correspond à « true » et 0 correspond à « false »

program Exemple_Const; #include <stdio.h>

var trouver : Boolean; int main (){

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

 Its role is to assign (give, attribute) a value to a


variable
14792
 This value can be :
14791 14.75 Note

 A constant 14790 0

 The value of another variable or constant


8566 exam
 an expression
8565
Syntax 8564

← exam ← Note

31
ASD I Noureddine AZZOUZA
Basic Instructions

Assignment
Basic Instructions

 Double rôles de l’affectation Dual roles of assignment :

1) Calculate and evaluate the expression to the right of the symbol: ←

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

program Exemple_Const; #include <stdio.h>

var a, b, c : Integer; int main (){

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

 An expression is a set of values (operands), linked by operators, and


equivalent to a single value.

 An operator is a sign that connects two values to produce a result

Syntax
=

34
ASD I Noureddine AZZOUZA
Basic Instructions

Expressions : Arithmetic
Basic Instructions

 Operands : integer, real.

 Operator :

+ : addition - : soustraction

* : multiplication / : division

DIV: quotient in Euclidean division

MOD: remainder in Euclidean division (modulo)

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

program Exemple_Const; #include <stdio.h>

var a, b, c : Integer; int main (){

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

 Operands : caracters, strings.

 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

program Exemple_Const; #include <stdio.h>

var nom, prenom : String[25]; int main (){


titre : String;

begin return 0;
//... }
nom := 'BENALI';
prenom := 'Karim';

titre := nom + prenom;


WriteLn('titre = ', titre);
//titre = BENALIKarim
titre := nom + " " + prenom;
WriteLn('titre = ', titre);
//titre = BENALI Karim

end.
38
Basic Instructions

Expressions : logic
Basic Instructions

 Operands : boolean (true, false).

 Operator :

NON : negation

AND : logic « AND »

OR : logic « OR »

39
ASD I Noureddine AZZOUZA
Expressions : logic

PASCAL C
Opérateurs : AND, OR, NOT Opérateurs : && (AND), || (OR), ! (NOT)
Basic Instructions

program Exemple_Const; #include <stdio.h>

var nom, prenom : String[25]; int main (){


titre : String;

begin return 0;
//... }

end.

40
Basic Instructions

Expressions : relational
Instruction de base

 Operands : integer, real, character, string.

 Operator :

< : less than <= : less than or equal

> : greater than >= : greater than or equal

= : equal <> : different

41
ASD I Noureddine AZZOUZA
Expressions : relational

PASCAL C
Opérateurs : <, <=, >, >=, =, <> Opérateurs : <, <=, >, >=, ==, !=
Basic Instructions

program Exemple_Const; #include <stdio.h>

var nom, prenom : String[25]; int main (){


titre : String;

begin return 0;
//... }

end.

42
Basic Instructions

Expressions
Basic Instructions

43
ASD I Noureddine AZZOUZA
Basic Instructions

Expressions : Evaluation
Basic Instructions

 The evaluation of an expression is performed from left to right, taking


into account priorities:
 Level 1: NOT
 Level 2: * / DIV MOD AND
 Level 3: + - OR
 Level 4: Relational operators

 Parentheses can be used to alter the priority.

44
ASD I Noureddine AZZOUZA
Basic Instructions

Expressions : Operators Hierarchy


Basic Instructions

 Operators hierarchy allows to determine how an expression will be


evaluated.

1. We start with operators that have the highest hierarchy and then
move on to those with immediately lower hierarchy, and so on.

2. In the case of an arithmetic expression, we start by performing all


multiplications, divisions, integer divisions, and modulos, and
then proceed to additions and subtractions.

 When the hierarchy is the same, the expression is evaluated from


left to right.

45
ASD I Noureddine AZZOUZA
Basic Instructions

Expressions : Evaluation
Basic Instructions

46
ASD I Noureddine AZZOUZA
Basic Instructions

Inputs / Outputs : Reading


Basic Instructions

 Allows you to provide values from outside

 Values are entered using the keyboard

 The P1, P2, ...Pn are variables


Syntax

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

program Exemple_Const; #include <stdio.h>

var a1, a2 : Integer; int main (){


b : Real;
c : Char; int a;
float b;
begin char c;
//...
ReadLn(a1, a2); scanf("%d", &a);
ReadLn(b); scanf("%f", &b);
Read(c); scanf("%c", &c);

//... //...

end. return 0;
}

48
Basic Instructions

Inputs / Outputs : Ecriture


Basic Instructions

 Allows you to display the results of an algorithm

 The values are displayed on the screen

 E1, E2, ...En can be: variables, strings or expressions


Syntax

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

program Exemple_Const; #include <stdio.h>

var a1, a2 : Integer; int main (){


b : Real;
c : Char; int a;
float b;
begin char c;
//...
//...
WriteLn('Hello, World'); printf("Hello, World");
WriteLn(a1, a2); printf("%d", a);
WriteLn(b); printf("%f", b);
WriteLn(c); printf("%c", c);
WriteLn('Résultat est ', a1+a2); printf("Résultat est %d", a);
//...
//...
return 0;
end. }
50
Ministry of Higher Education and Scientific Research
Djilali BOUNAAMA University - Khemis Miliana(UDBKM)
Faculty of Science and Technology
Department of Mathematics and Computer Science

Chapter 2

Simple Sequential Algorithm

MI-L1-UEF121 : Algorithms and Data Structures I

Noureddine AZZOUZA

51

You might also like