Professional Documents
Culture Documents
Course Algo Ch1 Ch2 Part1 2023 Cne2
Course Algo Ch1 Ch2 Part1 2023 Cne2
Course Algo Ch1 Ch2 Part1 2023 Cne2
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
Introduction to algorithms
Chargé de cours
Name Grade Faculty/Institute E-mail
mail address
BADECHE Mohamed MCB New technologies algo.assistance@gmail.com
o.assistance@gmail.com
Etudiants concernés
Faculty/Institute Department Year Speciality
New technologies MI License 1 Common Core
Objectives :
Introduce the concept of algorithm.
State syntactic conventions.
Become familiar with the basic instructions.
instruc
Getting started with conditional and repetitive processing.
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
Sommaire
Chapter 1: Introduction .......................................................................................................................................................... 1
1.1. Components of a computer ......................................................................................................................................... 1
Chapter 2: Algorithm .............................................................................................................................................................. 2
2.1. Introduction to the concept of algorithm .................................................................................................................... 2
2.1.1. Simple examples of program execution ............................................................................................................... 2
2.1.2. Algorithm example................................................................................................................................................ 3
2.1.3. Definition of an algorithm..................................................................................................................................... 3
2.1.4. Structure of an algorithm ..................................................................................................................................... 4
2.1.5. Basic instructions (‘Read’, ‘Write’ and assignment) ............................................................................................. 4
2.1.6. Algorithm vs Program ........................................................................................................................................... 5
2.1.7. User vs Programmer (Developer) ......................................................................................................................... 5
2.1.8. Basic syntactic conventions .................................................................................................................................. 5
2.1.9. Manual execution of an algorithm ....................................................................................................................... 6
2.1.10. Comprehension questions .................................................................................................................................. 7
2.1.11 Exercises............................................................................................................................................................... 8
2.2. Equivalent algorithms .................................................................................................................................................. 9
2.2.1. Optimal (best) algorithm ...................................................................................................................................... 9
2.2.2. Indicative and explanatory messages ................................................................................................................... 9
2.2.3. Questions de compréhension ............................................................................................................................. 10
2.2.4. Exercises.............................................................................................................................................................. 10
2.3. Types of variables ...................................................................................................................................................... 11
2.3.1. Exercise ............................................................................................................................................................... 12
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
Chapter 1: Introduction
Computer science
Hardware Software
1
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
Chapter 2: Algorithm
Description of
Running a program in Visual Basic (VB) Executing a program in C language
execution steps
Private Sub Form_Load() int main()
{
Dim a, b, c As Integer int a, b, c;
a = InputBox("") scanf("%d",&a);
Program code
b = InputBox("") scanf("%d ",&b);
c=a*b c = a*b;
MsgBox (c) printf("%d", c);
End sub }
2
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
1
Wikipedia
2
Philippe Flajolet, Étienne Parizot, « Qu'est ce qu'un algorithme ? », interstices.fr, 2004.
3
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
4
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
The assignment instruction always has a variable in its left part and can have in its right part, either a constant
(A 3.14), or a variable which has a value (A B), or an evaluable expression (A B*2+3). Writings like:
3.14 A, A + 2 B and A B 5 are syntactically incorrect.
The Write instruction displays either variable values: Write(A), or messages: Write("Hello"), or a combination
of messages and variables: Write("the area of the circle is ", A, " cm2").
By convention in this course, the Write instruction displays and jumps to the next line.
Algorithm Program
Pseudo code Code
Not directly executable on machine 3 Executable directly on machine
It was created for the programming service
Inputs Outputs
Treatment
The programmer must sometimes put himself in the user's shoes to test his program himself, and thus plays
both roles.
- We can put several instructions on the same line separated by a semicolon (but we prefer to put one per line).
3
The case of applications like 'AlgBox' on PC or 'Algo' on Smartphone, which in appearance allow you to execute algorithms, are in
reality only educational tools for simulating the execution of an algorithm on a machine.
4
« Algorithmique et programmation pour non-matheux », Christophe Darmangeat, 2013, Université Paris 7,
http://pise.info/algo/introduction.htm
5
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
- There is no distinction between upper and lower case for keywords. We can write for example: BEGIN, Begin
or begin.
- There is a distinction between upper and lower case letters for variables. The two variables: Name and NAME
are distinct (although this point is not considered in the correction of the final exams of this course).
- The name of the algorithm and variables can be composed of letters and numbers and must begin with a letter,
must not contain indices and punctuation letters, in particular space. We can write ex_01 as the algorithm name
and not ex 01.
- Blanks and tabs before instructions are allowed, as do before and after variables, commas and parentheses.
The extra line breaks also are allowed.
- Semicolons at the end of instructions are not required.
- Parentheses are required in both Read and Write instructions. For example, we write Read(a) and not Read
a.
- Messages that appear in the Write instruction must be delimited by " (double quotes) on both sides, such as:
Write("Hello").
- Variable types must be singular. We write: Integer, and not Integers.
- You can put several instructions on the same line (but we prefer to put one per line).
- The basic adopted keywords are:
o Algo ou Algorithm
o Var ou Variables ou Variable
o Begin
o End
o Read
o Write
o Integer
o Real
o Char ou Character
o Str ou String
o as an assignment symbol
o * as multiplication operator instead of ×. (+: for addition, -: for subtraction, /: for division, there is no
symbol equivalent to the power mathematical operation).
6
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
Example
Algorithm Algorithm execution with value 2.5:
Algo Aire
Var R A Screen
R, A : Real
2.5
Begin
Read(R) 19.625
A 3.14 * R * R
Write(A) 19.625
End
7
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
2.1.11 Exercises
Exercise 01: The algorithm of the 1st box is syntactically correct. Identify the syntactical errors in each of the
following scripts (2, 3, 4 and 5).
1 2 3 4 5
Algo Exo8 Algo Exo8 Algo Exo8 Algo Exo8 Algo Exo8
Var Var Var Var Var
a : Integer a : integer a : Integer b : Real af : Integer
b : Real b : Real b : Real a : Integer b1: Real
Begin Begin Begin Begin Begin
Read(a) Rid(a) Read(a) Read(a) Read(af)
b a/2 * 5 b a/2* 5 b a/2 * 5 b= x 5 b1 af/2 * 5
Write(b) Show(b) Write(b) Write(b1)
End Write b End End End
End
Exercise 02: What is the final result of running the following algorithm:
Algo Exo1_4
Var
A, B : Integer
C : Real
Begin
Read (A)
B 0
A A*A
B A+B
C A+B/A+5
Write (C)
End
For A = 2 , C =
Exercise 03:
a- Run the following algorithm for values PI=150 and R=30.
Algo Exo3
Var
IP, R, PP : Real
Begin
Write("Please give the initial price and discount")
Read(IP,R)
PP IP –IP * R /100
Write("The price to pay is ", PP)
End
b- What is displayed on the screen for each of the following case ?
PP 105 PP 105 PP 105
Write("The price to pay is ", PP) Write("The price to pay is PP") Write("The price to pay is " PP)
IP 150 IP 150
PP 105 PP 105
Write("The initial price is IP and The price to pay is PP") Write("The initial price is ", IP, " and The price to pay is ", PP)
IP 150 IP 150
PP 105 PP 105
Write("The initial price is IP and The price to pay is ", PP) Write("The initial price is ", " IP and The price to pay is ", PP)
8
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
Exercise 02 :
We want to permute the values of two integer variables A and B.
1- Run the following two algorithms with the values (6, 17):
Algo Exo1_19_A Algo Exo1_19_B
Var Var
A, B : Integer A, B : Integer
Begin Begin
Read(A, B) Read(A, B)
A B B A
B A A B
Write(A, " ", B) Write(A, " ", B)
End End
2- Rewrite the algorithm to allow the two variables to be swapped correctly, and verify it by running it with the
same values from question 1.
• Questions 3 and 4 are Additional.
3- Write the algorithm which allows you to permute the values of 3 variables A, B and C, in a circular way as
follows:
A B C
4- Generalize for 4 and 5 variables.
10
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
Algo Welcome
Var
FN, LN : String
Begin
Write ("Please what is your first and last name?")
Read (FN, LN)
Write ("Bonjour ", FN, " ", LN)
End
In this example, we ask the user to give him first and last name and we display a welcome message: Hello first
name last name. If the user enters Salmi for the last name and Karim for the first name, the algorithm will
display: Hello Karim Salmi
The only operation that can be applied to character strings at the moment is concatenation, which is
symbolized with the character +.
The previous algorithm can be rewritten as follows:
Algo Welcome2
Var
FN, LN, FullN : String
Begin
Write("Please what is your last and first name?")
Read(LN, FN)
FullN FN + " " + LN
Write("Hello ", FullN)
End
For the character type, here is an example:
Algo Welcome3
Var
F, L : Char
Initials : String
Begin
Write ("Please give the first letter of your first and last name")
Read (F, L)
Initials F + '.' + L
Write ("Hello ", Initials)
End
In this example: if the user enters F = 'K' and L = 'S', the algorithm displays: Hello K.S
Remarquer qu’on délimite un caractère (comme le point dans cet exemple) par simples cotes et non pas doubles
cotes comme pour les chaînes de caractères.
Note that we delimit a character (like the point in this example) by single quotes and not double quotes as for
character strings.
11
Course: Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
Dr. Mohamed BADECHE Common Core – MI, NTIC Faculty – 1st year (2023-2024)
2.3.1. Exercise
Write an algorithm that asks the user to enter their last name, first name, age and address. And displays the
entered information as follows:
++++++++++++++++++++++++++++++++
Hello Salmi Karim
You are: 18 years old
You live in : Benboulaid City – Constantine
----------------------------------------------------
12