0% found this document useful (0 votes)
12 views8 pages

The Algorithmic Language

Uploaded by

mimouni2am
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)
12 views8 pages

The Algorithmic Language

Uploaded by

mimouni2am
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/ 8

Course

Algorithms and Data Structures I

ENSM
2023 – 2024

Chapter II: The algorithmic language


I. Introduction
Problem of drawing
ð L1 language
ð Solution S1
Problem of moving a robot in a grid
ð L2 language
ð Solution S2

Question: How to define a language which makes it possible to express the solution of
any problem? This is the objective of the algorithmic language!

II. The algorithmic language


- Any algorithm can be expressed in a language that is close to natural language. This
language is called the algorithmic language. It describes in a complete and clear manner
the objects manipulated by the algorithm as well as all the instructions to be executed
on these objects to obtain the target results. It allows a good structuring of an algorithm
and also the use of comments.
- Comments are intended to make it easier for a reader to understand an algorithm but
they are ignored by the compiler and hence, also by processor at runtime.
- It is also called pseudo-code.
- Pseudo means that it is not a real language based on the definition of a formal grammar.
It is a way of describing the treatment that expresses the solution of a problem:
• In a less ambiguous way than natural language, and,
• A less formal way than a mathematical language.
- The algorithmic language must have the property of allowing an easy translation into a
programming language.

Some other properties:


It must have a rigorous writing.
You must have a neat writing: Respect the indentation.
It is necessary to comment on the algorithms.
There are several algorithmic solutions to a given problem:
=> We have to seek the effectiveness of what we write.

1
III. The general structure of an algorithm
- An algorithm is structured in two parts contained between the keywords start and end.
Each algorithm has a name. The structure appears as follows:

algorithm <AlgoName>;

start
<part 1: declaration of variables>

<part 2: actions on variables>

end

An example: solving the quadratic equation.

algorithm QuadraticEquationSol;

start
a, b, c, x, x1, x2, delta: real;
read (a, b, c);
delta ¬ (b * b) – (4 * a * c);

if (delta < 0) then print(“there is no real solution”);


endif

if (delta = 0) then x ¬ -b/(2*a);


print(x);
endif

if (delta > 0) then x1 ¬ (-b - SQRT(delta))/(2*a);


x2 ¬ (-b + SQRT(delta))/(2*a);
print(x1, x2);
endif
end

2
Explanations:
• The name of this algorithm is “QuadraticEquationSol”.
• The line “a, b, c, x, x1, x2, delta: real;” defines six
variables. These are real numbers.
• The order “read (a, b, c);” is assumed to be known by the machine. It
allows to read the values of the variables “a, b and c”. At runtime, the user
has to provide (i.e. to enter) values of the variables. For instance, if the user
would like to solve the equation “3 * x2 + 5*x + 14 = 0”, she/he has to enter the
values 3, 5 and 14 to the variables a, b and c, respectively.
• The sequence of orders “if … then … endif” allows us to check a
condition (here, testing the value of variable “delta”).
• If we would like to solve a set of quadratic equations, we have just to repeat
the same treatment as many times as the number of equations we have.

IV. The syntax of the language

As we have just seen in the example, the algorithmic language must allow at least to:
- Declare a variable or a set of variables;
- Read data;
- Display (print) calculated results or messages;
- Calculate arithmetic and logical expressions;
- Assign an expression value to a variable;
- Test the value of a variable;
- A treatment according to a condition;
- Repeat a treatment.

V. Declaration of a variable

Declaring a variable consists of giving a name to a variable


and associating it with a “type”. The supposedly known types are the mathematical numeric
types "integer", " real" and also the type of logical values (false, true) which is named "boolean".
To be able to solve problems dealing with words and texts, we will manipulate the types
“character” and “string”.
A variable is declared like this:

< Variable Name >: < Type >;

A sequence of variables x1, ..., xk of the same type can be declared in a single declaration:

x1 , x2, ..., xk : <Type>;

3
VI. Simple Types

A data “type” is a set of values on which it is possible to apply a set of operations.


• The “integer” type represents the set of relative integers. The operations that we may
apply on the integers are: +, -, *, /, **, % (addition, subtraction, product,
division, power, modulo).
We can also compare two integers using the comparison operators: £, <, =, >,
³ , ¹.
An integer constant is a sequence of digits. For example, -3, 145, 0, are
"integer" type constants.

• The “real” type is the set of real numbers. This set also supports arithmetic operations
(which are also applied to integers) and which are: +, -, *, /, **. It also
supports comparison: £, <, =, >, ³, ¹.

In a real constant, the decimal point separates the integer part from the decimal part. For
example: -3.1459, 145.5, 0, 0.75 are real constants.

The “boolean” type represents the ordered set with two values false and true. The
comparison of two integers or reals provides a “boolean” type result.

• The "character" type contains all the constants that are written with a single letter. This
letter can be for example what we type on a keyboard. It represents lowercase and
uppercase alphabetic letters ( 'a', ..., 'z', 'A', ...,'Z' ), numeric
characters ( '0', ..., '9' ) , the special characters ( '+', '*', '?', '$',
'''', ... ), and the space (or blank) character.

A constant character is written between two apostrophes, example '!'.

• The “string” type represents the set of character sequences that we can form by
juxtaposing a set of “character” type elements.
A "string" constant is written between two quotes, for example: "Bonjour!"
We can apply functions to a variable of type “string” such as:
length(s): which gives an integer result corresponding to the number of
characters that make up the string s.
For instance:
length("Hello!") returns the integer value 6.

concat(s1, s2) : which concatenates two strings and returns the result of
the concatenation. For instance:
concat("Hello ", "dear students") returns the string
"Hello dear students".

4
VII. Basic actions

We define three actions as fundamental:


1. Reading data from the user;
2. Displaying results;
3. Assigning values to a variable.

• Reading data
It is expressed by the read order with the parameters which represent the variables to
be read.
If x1, ..., xk are k given variables, their reading can be done using the
following order:

read(x1, ..., xk);

Example (the quadratic equation): read(a, b, c);

• Displaying results
In a symmetrical way with respect to reading, we can perform the visualization of
results by the order:

print(var1, var2, ...);

For instance (in the quadratic equation):

print(x1, x2, delta);

or displaying a text message by putting it in quotes:

print(”hello dear students”);

• Assignment

This order is used to give a value or the result of an expression to a variable x:

x ¬ 3.14;

x ¬ <arithmetic/logical expression>;

5
VIII. Structured actions
There are two types of structured actions:
1. Those that allow the execution of a series of actions depending on a condition to be
checked (like the test on the value of delta in the example of the quadratic equation);
2. Those which make it possible to repeat a process and which we call “iterations” or
“loop”.

• Conditional Statements

They respect the writing syntax according to one of the two following forms.
1. The first form expresses a processing choice: Either we execute the sequence of
actions ActionA1, ..., ActionAm if <condition> is true, otherwise
we execute the sequence ActionB1, ..., ActionBn if the condition is
false.

if (<condition>) then ActionA1, ..., ActionAm;


else ActionB1, ..., ActionBn
endif

Or, when processing is only done if a condition is true:

if (<condition>) then ActionA1, ..., ActionAm;


endif

Example: If we consider that SQRT(N) computes the square root of a number


N > 0, then we will have the following algorithm:

algorithm QuadraticEquationSol;

start
a, b, c, x, x1, x2, delta: real;
read(a, b, c);
delta ¬ (b * b) – (4 * a * c);

if (delta < 0) then print(“There is no real solutions!”);


else if (delta = 0) then x = -b/(2*a);
print(x);
else x1 ¬ (-b - SQRT(delta))/(2*a);
x2 ¬ (-b + SQRT(delta))/(2*a);
print(x1, x2);
endif
endif

end

6
• Iterative statements

There are three forms of iterative statements:


1. The “for” loop:

for var = valInit to valFin step s do


Action1;
...
ActionN;
endfor

Example 1:

for counter = 1 to 100 step 1 do


print(counter);
endfor

Example 2:

s ¬ 0;
for i = 0 to 9 step 1 do
s ¬ s+1;
endwhile

2. “while … do” loop :

while (<condition>) do
Action1;
...
ActionN;
endwhile

Example 1:
counter ¬ 1;
while (counter <= 100) do
print(counter);
counter ¬ counter + 1;
endwhile

7
Example 2:
s ¬ 0;
i ¬ 0;
while (i < 10) do
s ¬ s+1;
i ¬ i+1;
endwhile

3. The “repeat … until” loop:

repeat
Action1;
...
ActionN;
until (<condition>);

Example 1:
counter ¬ 1;
repeat
print(counter);
counter ¬ counter + 1;
until (counter > 100);

Example 2:
s ¬ 0;
i ¬ 0;
repeat
s ¬ s+1;
i ¬ i+1;
until (i = 10);

IX. The parameterized actions

We can associate a group of actions that performs a given process with a name and a set of
parameters.
(See: The example of line in the drawing of stars)
We have two types of parameterized actions: Functions and procedures.

You might also like