Algorithm Course
Algorithm Course
MQ1: Algorithm
The objective of this course is to answer these three questions:
• What is algorithmic?
• What is an algorithm?
• What are the different representations of an algorithm?
• What are the steps to solving a problem?
• What is a programming language?
• What are the basic concepts of the algorithm?
Introduction
A computer program allows the computer to solve a problem, but before...
to communicate to the computer how to solve this problem, it is first necessary to be able to
to solve it ourselves make an algorithm.
1.1. Algorithms
designates the discipline that studies algorithms and their applications in
computer science
1.2. An algorithm
is a method that allows to solve a given problem in finite time, it
represents a logically ordered sequence of instructions (action)
Problem
Result
algorithm
solution
provided
AMARA.A
National Institute Specialized in Professional Training Batna2 algorithm
Perform an action
Example:
Début
Read (a)
Read (b)
a a+b
Write (a)
fin
Logic diagrams have the advantage of being easy to understand; you just need to follow the thread.
nevertheless, they have two drawbacks:
AMARA.A
National Institute Specialized in Vocational Training Batna2 algorithm
Example
addition algorithm
variables a, b as integer
read
read (b)
a a+b
write (a)
end
problem
Understand and analyze the statement of the problem
Models
Specify the resolution model: The necessary data and
The resulting data and the mathematical formulas
But during the writing of a program, one may encounter 2 types of errors:
• Syntax errors: they are noticed at compilation and are the result
of a bad
writing in the programming language.
• Semantic errors: they become apparent during execution and are the result of a
poor analysis. These errors are much more serious because they can lead to
trigger during the operation of the program.
AMARA.A
National Institute Specialized in Vocational Training Batna2 algorithm
AMARA.A
National Institute Specialized in Vocational Training Batna2 algorithm
• A name (Identifier)
• A type that indicates the set of values that the variable can take (integer,
real, boolean, character, string, ...
• A value
Identifiers: rules
The choice of a variable name is subject to some rules that vary depending on the language.
but in general:
• A name must start with an alphabetical letter. Example: E1 (1E is not)
valid)
• Must consist only of letters, numbers, and underscores (' _ ')
Avoidpunctuationcharactersandspaces.Examples:SMI2008,SMI_2008.
(SMP 2008, SMP-2008, SMP;2008: are not valid)
• Must be different from the reserved words of the language (for example in Pascal: integer,
real, char, program, if, for...
• The length of the name must be less than the maximum size specified by the language
used
Advice: for code readability, choose meaningful names that describe the data.
manipulated.
StudentGrade
Note: in algorithmic pseudo-code, we will follow the mentioned rules, even if we
is free in the syntax
Types of variables
The type of a variable determines the set of values it can take. The types
offered by most languages are:
• Numeric type (integer or real)
• Byte (coded on 1 byte): from [-27,27[ or [0, 28[
• Short integer (coded on 2 bytes): [-215, 215[
• Long integer (coded on 4 bytes): [-231, 231[
• Simple real precision (encoded in 4 bytes): precision of order 10-7
• Double precision real (encoded on 8 bytes): precision of the order of 10-14
• Logical or boolean type: two values TRUE or FALSE
• Character type: uppercase letters, lowercase letters, numbers, symbols, ... . Examples: 'A',
'b', '1', '?', ...
• String type: any sequence of characters. Examples: " ", " Last name, First name"
postal code: 1000
A) Integer: To represent integers, the operations usable on integers
are:
• All basic elementary operations are allowed: +, -, *, /
• The classic comparison operators: <, >, =, ...
• The division / is the Euclidean (or integer) division. E.g. 11 / 4 = 2 and not 2.75!)
• There is the modulo operator %, which gives the remainder of the Euclidean division. Example:
11%4 = 3
B) Real: To represent real numbers, the operations usable on reals are:
• The classic arithmetic operations: + (addition), - (subtraction), * (product), / (division)
division
• The classic comparison operators: <, >, =, ...
• The division gives a decimal result
C) Boolean: A variable of logical type (boolean) can take two values: TRUE or
FALSE. The most commonly used main operations are:
AMARA.A
National Specialized Institute for Professional Training Batna2 algorithm
AMARA.A