0% found this document useful (0 votes)
5 views

Chapter 0 Algorithm

Uploaded by

nouira nesrine
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 0 Algorithm

Uploaded by

nouira nesrine
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Algo & SD

Rania YANGUI
y [email protected]
Chapter 1: Introduction to the
algorithm

1.2
Plan

1. Introduction
2. Structure of an algorithm
3. Example
4. Concept of variable
5. Variable Types
6. Expression and operator
7. Concept of constant
8. Types of instructions
9. Test on instruction assignment
10. Conditional and iterative instructions

1.3
Introduction
● An algorithm is a finite and unambiguous sequence of
operations Or of instructions to solve a problem .
● Comes from the name of the mathematician Al- Khawarizmi ,
the father of algebra .

● Examples algorithms :
- A cooking recipe ( ingredients → prepared dish )
- The research in a dictionary (word → definition )
- The whole division ( two integers → their quotient)

1.4
Introduction
● Program :
○ A program is an assembly and a chain of instructions
elementary written in a programming language , and
executed by a computer in order to process the data of a
problem and return one or several results .
● Programming language :​
○ A programming language​provides a set of keywords and
syntax rules that allow you to create statements that form
programs and that can run , without any problems, on a
machine.

1.5
Introduction

● So the general idea of an algorithm is to give the solution to a

problem in the form of operations which can be translated in a

language understandable by the machine while keeping it readable

and understandable by a person ordinary .

1.6
Introduction

“I will, in fact, claim that the difference between a bad

programmer and a good one is whether he considers his

code or his data structures more important . Bad

programmers worry about the code. Good programmers

worry about data structures and their relationships. ”

Linus Torvalds (creator of Linux)

1.7
STRUCTURE OF AN ALGORITHM

● The first line allow to identify the


algorithm . So , the name assigned does
not change the execution and results .
● Before putting the instructions, it must
declare the constants and variables
used In the algorithm .
● The main part of the algorithm is
located between the keywords ‘Begin'
and ‘End' . It contains the sequence of
instructions to be executed .

1.8
Example

1.9
Concept of variable

● A variable is a word that allows identification​of memory location


where we store a value to manipulate .
● Although the free choice of the name of the variable , it is
recommended , for reasons of readability and understanding , to
choose variable names in functions of this that they represent .
■ For example : Average , Weight , Height , etc.

● Example of variable
■ M, Max: float
■ Weight , Average : int
■ Achievement : Boulean

1.
Variable Types

The most common types of variables in algorithmic :


● Numerical
- int : set of integers relative Z
- real : set of numbers real R
● Alphanumeric
- String : always​​noted between double quotation marks
- character : noted between single quotation marks
● Boolean
- Boolean : stores only values​logical TRUE and FALSE

1.
Expression & operator

To each type is associated a set of operations ( or operators )


● Operators Booleans : No, And, Or , Or Exclusive
● Operators on digital
- We naturally find : +, -, *, /,
- For integers : div and mod , allow respectively to calculate an entire
division and the remainder of that division
- The operator of equality ( = ) or inequality ( ≠ ) allows us to know if
the two operands are equal . The result is a boolean .
- comparison operators​<, ≤, >, ≥
- For operators arithmetic , order of priority is the following (from
highest priority to lowest) priority ): ^, *, /, +, -

1.
Expression & operator

Operators alphanumeric
● &: concatenation

Example :

A ← "Hello"

B ← "everyone"

C ← A & B , C is "Hello world"

1.
Notion of constant

● We can assimilate the notion of a constant to a variable whose

value does not change during the execution of the algorithm .

● Example of declaring constants :

■ VAT = 0.17

■ N = 50

■ Admitted = True

■ pi = 3.14

■ …

1.
Types of instructions

● The part main of an algorithm contains the sequence of


instructions that will be translated to a programming language .
○ Input instruction : This is the instruction to read data from the
device input :

1.
Types of instructions

○ Instruction : This is the instruction to return results to the


output device ( display ):

1.
Types of instructions
○ The instruction assignment
■ This instruction allows to affect a value to a variable, after
evaluation of a logical or arithmetic expression.
■ It's necessary note that the result of evaluating the expression
must have the same type as the variable that will receive
■ It's necessary note that each variable takes the last value
affected

1.
Test on the assignment statement
● Objective : self- assessment of understanding of the effect of
assignment statements on the state of variables.
Exercise 1
What are the values of the variables a,b and c written by the
following algorithm?

1.
Test on the assignment statement

● b =20, a =0, c =20

1.
Test on the assignment statement
Exercise 2
What is the objective of this sequence instructions ?

● Format respected BUT


● Calculate the difference between two variables.
● Swap the values of two variables.
● Without objective .

1.
Conditional Statements
● These instructions are used to make the choice between execution
or not blocks of instructions following the evaluation of a condition.
There are three types :
1. The instruction simple
This type of instructions allows you to choose between
execution or not of a single block of instructions

1.
Conditional Statements
1.'instruction co
2. The instruction alternative
This type of instructions allows you to choose between two blocks
of instructions to execute .

1.
Conditional Statements

1. The instruction conditional choice​

This type of instructions allows you to choose between multiple


blocks depending on the value of a given variable .

1.
Conditional Statements
Example:
Read (x)
Depending on the case (x)
x = 1 : Write ('Very Low')
x = 2 : Write ('Weak)
x = 3 : Write ('Average)
x = 4 : Write ('Good')
x = 5 : Write ('Excellent')
Otherwise
Write ('Value out of accepted range')
End If

1.
Test on the use of conditional statements

Objective : self- assessment Test2


of understanding of the role of Statements :
conditional statements . a, b, c: Integer
Exercise Either the algorithm Beginning
next : What are the
1:Read (a, b)
instructions executed for the
If (b/2 > a) Then :
values : a = 5, b = 10?
2: c ← b - a
Otherwise
3: c ← b + a
4: a ← b - c
End if
5: Write (a, b, c)
END.

1.
Iterative instructions
● Iterative instructions Or repetitive , called Also loops , allow to execute several
times the same block of instructions .
● There are three types :
1. 'For'
In this type of instructions iterative we know the number in advance of
iterations . In In fact , we must specify , in the header of the 'For' instruction,
the value initial and final value and possibly the step

Example :
For i ranging from 1 to 10
do
Writing ('Master BD &
DS')
End for

1.
Iterative instructions
2. The ' While '

- In this type of iterative instructions we do not necessarily know


the number in advance of iterations .
- We continue the repetition of the execution of the block of
instructions so much that a condition is still satisfied .

Example :
i←1
While (i <= 10) do
Write ('Master BDDS')
i ← i *2
End As long as

1.
Iterative instructions
3. The ' Repeat '

- As with the ' While ' statement , the ' Repeat ' statement does not
allow you to know necessarily in advance the number of iterations .
- We continue the repetition of the execution of the block of
instructions until a condition is met .

1.
Iterative instructions
Example :
i←1
Repeat
Writing ('Master BD&DS')
i←i+1
Up to ( i > 10)

Note : The ' While ' statement executes zero Or several times , at the
time when , the instruction ' Repeat ' is executed a Or several times .

1.

You might also like