0% found this document useful (0 votes)
8 views14 pages

Chapitre 1-1

Uploaded by

ferg.yasmine
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)
8 views14 pages

Chapitre 1-1

Uploaded by

ferg.yasmine
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/ 14

1 People`s Democratic Republic of Algeria

The Ministry of Higher Education and Scientific Research

University Of Science And Technology Houari Boumediene


Faculty of Computer Science

Algorithmic and Data Structures


the basics of algorithmic
First year in Computer Science engineering

By: PhD. HAOUARI Ahmed Taha


[email protected]

2024/2025
2

1
How to solve a problem
using algorithm ?
We’re talking about computation problems
3

The steps to solve a computation problem


The most important steps are:
1. Understanding the problem
2. Identifying necessary inputs and expected
output
3. Design a flowchart (isn’t common nowadays)

4. Write the algorithm

5. code the program


4

Example of a problem
• Calculate the circumference C of a circle with the radius r ?

(this problem will follow us all the way to the end of the chapter)

• Understanding the problem:


• The problem is to calculate the circumference of a circle

• To asses the circumference we need this equation:


C= 2 * π * r

• The necessary inputs and expected output :


• In our problem, the input would be r (the length of the radius)
• The output or the result, is no other than the circumference C
5

Designing a flowchart
• A flowchart is a diagram that uses various symbols and shapes to represent a process or a series of steps, Which help
us to visualize the solution of our problem, in our case : an overview of the algorithm,

• The most important shape are :

1. the oval: being and End of the program

2. The parallelogram : input and outputs operation

3. The rectangle: the process/action/instruction or computation step to be carried

4. The Diamond (Rhombus): The decision making or the branching/ the tests

5. The arrows/ flow lines the direction of the algorithm/program logic


6

The flowchart of our problem


a potential answer to a problem:
Begin
Exercise:
Find a better solution ?

Input r

Yes No
C=2* π * r
r>0 C=0

Output C

End
7

2
Writing our first
algorithm
Before that we need to understand the different parts
of an algorithm
8

What’s algorithm ?

• Reminder :
• An algorithm is a finite sequence of elementary actions executed in a specific order
on a set of data, aiming to solve a problem.

• For a computer problem, analysis consists of:


• Defining all the objects used to represent input data and output results.
• Defining all the elementary actions/instruction that will be executed in a specific
order to achieve the results.
9

The structure of an algorithm


Before we get further, let see how the structure of
Algorithm look like:

The header • Name of the algorithm

Declaration • Defining the objects

Actions • Set of steps to solve the problem


10

The header section


It’s used to define the name of the algorithm.
To do this, we use the keyword: Algorithm
Thus:
Algorithm <algorithmName>;
The syntax must be respected. (don’t forget the semicolon)

<algorithmName>: This is an identifier representing the name of the algorithm.


We can choose any name, but it's preferable that it is descriptive.
Example:
Algorithm Invoice;
Algorithm Equation;
Algorithm CalculateSum;
Exercise: name the algorithm for our problem ?
11

The Declaration section


For a computer problem, we need to define (Declare) all the objects that represent the input
and output data

What’s an object ?
An object is the entity manipulated by an action. Two classes of objects are distinguished:
constants and variables.

For example, the action C = 2*Pi *r


manipulates four objects: two variable objects (C and r) and two constant object (2 and Pi ).
Where going to see why Pi is a constant

What’s the difference between a constant and a variables ?


1. A variable can change its value during the algorithm and can be modified .
2. Constant maintains the same value throughout the execution of the algorithm
12

Characteristics of an Object:
An object is characterized by:
• Name: Called an identifier, it allows the object to be identified.
• Type: The set of values that an object can take (real, integer, character, etc.),
• Value: An element of the type taken at a given moment.
For example:
Name: Age Type: Integer Value: 18
• An identifier must follow certain naming rules:
• It is formed from alphabet characters (A to Z or a to z), digits (0 to 9), and the underscore character(_).
• It can be at least one character long. The first character must be a letter.
• Example:
• Correct Identifiers: TTC, Gr3, sect, Nom_Et, Note_1_2_3, X, y
• Incorrect Identifiers: 9TH, Gr 3, S?, Nom-Et
• Recommendations :
• It's preferable to choose meaningful names. And start with a lowercase character
• Some words are not allowed (see reserved keywords), And avoid long name,
13

How to declare an object ?


we declare all the objects used in the algorithm (constants and variables) by specifying two characteristics:
The name and type for variables.
The name and value for constants.

The declaration syntax for both types of objects is as follows:


Const <ObjId> = <ObjValue>;
Var <ObjId> : <ObjType>;
Where:
<ObjId> is the identifier of the object.
<ObjValue> is the value of the object.
<ObjType> is the type of the object.
Example:
Const Pi = 3.14;
Var Age: integer;
Note: Objects of the same type can be grouped together
Example: Var Note1, Note2, Moyenne: real;
14

What is the effect of the declaration on the system?


• Declarations allow the compiler (during execution) to allocate (reserve) memory space for each
object, the size of which varies depending on the type of the declared identifier.

• Every machine has memory (Central memory), generally called Random access Memory (RAM)
• This memory can receive storage instructions. So, each declaration corresponds to an instruction
that tells the memory, "Prepare a space called <ObjId>"
For example:
Const Pi = 3.141592;
Var Age: integer;
Mean: real;
Sect: character;
question: Boolean;

But what are integer, real, character and Boolean?


The are the simple type of data used in algorithmic

You might also like