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

ASD1

Uploaded by

kboudjemline05
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 views

ASD1

Uploaded by

kboudjemline05
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/ 23

Dr.

Samir FENANIR Chapter 1 : Algorithmic Concepts

Comp_Note, Math_Note, Phys_Note, Avg: real


Redoub: boolean

4.1.5 Representation of variables and constants

For each variable or constant, the computer reserves a place in CM. The size of this place
depends on the type of variable or constant.

NB: In some programming languages, constants do not occupy memory space and are
evaluated by the compiler.

4.2 Expressions
An expression is a series of operations on constants/variables already declared.
Syntax : x op y
Such that: the 2 operands x and y are constants or variables; and op: is an arithmetic or
logical operator.
Examples:
A + B Arithmetic Expression,
A > B Logical Expression,
A + B > C Arithmetic and Logic Expression.

10
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

4.2.1 Arithmetic Operators

Mod: rest of the division, example: 12 mod 5 = 2.


Div: Division quotient, example: 14 div 3 = 4.

4.2.2 Logical operators

Examples :
(x > 4) and (x ≤ 9), (for example, with x = 7, the result is true).
(x > 4) or (x ≤ 9), (for example, with x = 2, the result is true).
not((x > 4) and (x ≤ 9)) equivalent to (x ≤ 4) or (x > 9).
not((x > 4) or (x ≤ 9)) equivalent to (x ≤ 4) and (x > 9).

11
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

4.2.3 Operator priority

In an expression, the order of precedence of operators is important. Indeed, multiplication


and division take priority over addition and subtraction. For example, 9 + 5 * 3 gives the
result 24.
If we want to modify this order of priority, we will have to use parentheses. For example,
(9 + 5) * 3 gives the result 42.
The table below presents the order of priority of the different operators classified in
ascending order of priority:

Exercise 1: give the following expression evaluation schemes:

1) 5 − 14 ∗ 4/13 ∗ (z + 1 − (n + 5))

2) a + b ∗ c ∗ d ∗ e − f /(g + h) − i

3) (a > b) or (c > a + b ∗ d − e/ f )

4) not(a > 0) and (c > b)

12
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Solution:

Exercise 2: Correct if necessary then evaluate the following expressions with I=3 and
J=10:

1) 2 ∗ I ≤ J

2) 1 + I ∗ 2 < J

3) I ≤ 3 and J ≤ 3

4) (I > 0) or (I ≤ 10)

5) I > 0 or J < 0

6) not I > 1

Solution:

1) 2 ∗ I ≤ J ⇒ 6 ≤ 10 ⇒ true

13
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

2) 1 + I ∗ 2 < J ⇒ 7 < 10 ⇒ true

3) I ≤ 3 and J ≤ 3 ⇒ Error ⇒ correction : (I ≤ 3) and (J ≤ 3) ⇒ true and f alse =


false

4) (I > 0) or (I ≤ 10) ⇒ true or true = true

5) I > 0 or J < 0 ⇒ Error ⇒ correction : (I > 0) or (J < 0) ⇒ true or f alse = true

6) Not I > 1 ⇒ Error ⇒ correction: not(I > 1) ⇒ not(true) = false

4.3 Basic instructions


An instruction is an elementary action commanding a machine to perform a calculation, an
input operation or an output operation. There are three basic instructions in an algorithm:
assignment, read and write.

4.3.1 Assignment instruction

The assignment statement allows you to assign a value to a variable. It is symbolized


algorithmically by the sign "←" which specifies the direction of the assignment.
Syntax :

The assigned value can be a variable, a constant, or an arithmetic or logical expression.


In the case of an expression, we must first evaluate it before assigning it.
Examples :

4.3.2 Reading instruction (Read)

The input (read) instruction allows the user to enter a value using an input device (usually the
keyboard). The entered value will be assigned to a variable.
Syntax :

14
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Read (V) « Read a value for the variable V»


Read (V1, V2,. . . ) « Read values for the variables V1, V2, ...»
Example :

Upon encountering an input instruction, execution pauses (stops) and waits for the user
to enter a value. This value will be stored in central memory in the box reserved for the
corresponding variable.

4.3.3 Writing instruction (Write)

The output (write) instruction displays the results produced by a program on an output device
(usually the screen).
Syntax:
Write (E) « Write the value of E on the screen »
The displayed value "E" can be a constant, a variable or an expression.
Example :

Remarks :

• You can replace several Write instructions with a single Write instruction. (E1, E2, ...).

• Before reading the value of a variable, it is advisable to display a message to the user
asking them what they should enter.

Example:
Write ("Give the value of A: ")
Read (A)

15
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

4.4 Algorithm execution trace


In order to verify the results provided by an algorithm, we make its execution trace, which
consists of a table containing the values of the variables during the execution of this algorithm.
To do this, the instructions must be numbered to facilitate explanation.

16
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Example :

Exercise 1: Write an algorithm allowing you to exchange the values of two integer
variables A and B.

Solution:
The first idea that comes to mind is to use only 2 variables A and B, and 2 assignment
instructions, then the algorithm is written as follows:

After carrying out the execution trace of this algorithm, we see that the result provided
by this solution is erroneous. Indeed, instruction n°2 (A ← B) overwrote the initial value of
A with a new value, and at the end of the execution, we obtain the same value for the two
variables. To correct this error, we must save the value of A in a third variable C.

17
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Exercise 2: What result does the following algorithm produce?

Solution:
To know the result produced by this algorithm, we must perform its execution trace.

Exercise 3: What will be the values of the variables after execution of the following
instructions?
Solution:

18
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Exercise 4: Write an algorithm that allows you to calculate a student’s average for three
subjects: Computer Science (Coefficient = 4), Mathematics (Coefficient = 4) and Physics
(Coefficient = 2).

Solution:

Algorithm Moyenne
Constants CINF = 4, CMAT = 4, CPH = 2
Variables NINF, NMAT, NPH, MOY: real
Begin
Read (NINF, NMAT, NPH)
MOY ← (NINF*CINF+NMAT*CMAT+ NPH* CPH) / (CINF+CMAT +CPH)
Write (MOY)
End

Exercise 5: Write an algorithm that allows you to calculate the perimeter of a circle.

Solution:

Algorithm Perimeter
Constant pi = 3.14
Variables Perim, radius : real
Begin
Write ("give the radius of the circle: ")
Read (radius)
Perim ← radius * pi * 2
Write (Perim)
End

Exercise 6: Write the algorithm that allows you to calculate the delta discriminant of a
quadratic equation. Create the execution trace by giving 2, 5, 1 for the three parameters of
the equation.

Solution:

19
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Exercise 7: Write an algorithm that allows currency conversion between the Dinar and the
Euro, such as: 1 Euro = 155 DA. Make the execution trace with: E = 100.

Solution:

Exercise 8: Write an algorithm that calculates the coordinates (x, y) of the middle of a
segment [AB] in a plane.

20
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Solution:

4.5 Comments
Comments are used to explain an algorithm in order to better understand and remember what
has been programmed, they are noted between (* *) or { }. The use of comments is optional.
Example:
Algorithm circle (* This algo calculates the perimeter of a circle*)
Constant pi = 3.14 (*Declaration of the constant*)
Variables R, P : integer (*Declaration of the variables *)
Begin (* Start of the algorithm*)
Read (R) (*Enter the radius value*)
P = R*R*pi (*Perimeter calculation*)
Write (p) (*Result display *)
End

5 Graphical representation (flowchart)

The graphical representation allows easy reading of the algorithms, but it consumes a lot
of space. Operations in a flowchart are represented by symbols that are linked together by
arrow lines that indicate the path. The table below shows the symbols corresponding to basic
operations and instructions.

21
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Exercise: Write an algorithm and its graphical presentation that asks the user for a number,
then calculates and displays the square of this number.

Solution:

22
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

6 The C language
C is a compiled language, which means that in order to execute a program, it is necessary to
translate it from the advanced language to machine language. This translation is carried out
by a program called a compiler. The program written by the programmer is called source
code (or source program), and what the computer executes is called executable code, object
code, or machine code (figure 1.5).

6.1 Compilers and interpreters


There are two types of programs that programmers use to perform translation: compilers and
interpreters.

Compiler: is a program that translates instructions written in a high-level language into a


separate machine language program. We can then run the program in machine language
at any time. After translation, there is no need to run the compiler again unless we
make changes to the high-level language program.

Interpreter: is a program that simultaneously translates and executes instructions written


in a high-level language. As the interpreter reads each individual instruction in the
high-level language program, it translates it into machine language code and then
executes it directly. This process is repeated for each instruction in the program.

Instructions that the programmer writes in a high-level language are called source code or
simply code. The programmer first writes the source code in a program called code editor,
then uses either a compiler to translate it into a machine language program or an interpreter
to translate it and execute it at the same time.

high-level language: is a language understandable by programmers (human beings); also


called advanced language.

Machine language: is a machine-readable language (a sequence of 0s and 1s); also called


low-level language.

23
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Fig. 1.5 Compilation and execution of a C program

6.2 Correspondence tables (Algorithmic / C)


6.2.1 Elementary types

Note: In C language there is no boolean type, so we can use int type or define a new type
using "typedef" command and set the values of TRUE to 1 and FALSE to 0. The same for the
string type, where we must use an array of characters.

24
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

6.2.2 Arithmetic operators

6.2.3 Logical operators

6.2.4 Basic Instructions

The printf function allows you to display a message or the value of one or more variables:
To display a message: printf ("Welcome");
To display the value of a variable: printf ("The height is: %f meters", T);
To display several values: printf ("%d hours and %d minutes", h, m);
The scanf function allows the user to enter the value of one or more variables:
To enter the value of one or more variables: scanf ("%d", &A);
To enter the values of several variables: scanf ("%d %d", &A, &B);

25
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

The following table shows the main formats that can be used with the printf and scanf
functions.
%d An integer data of type int
%ld An integer data of type long
%f A decimal data of type float.
%lf A decimal data of type double.
%c A character data type.
%s A string data type.

6.2.5 Comments

6.3 Example of a C program


Here is an example of a C program that displays a « Welcome » message.

• The #include directive asks the computer to make accessible (include) the functionality
contained in a file named "stdio.h". This file is a library of functions which allows us
to use in particular the "scanf" and "printf" functions in our program.

• A C program always starts with a main function called main.

• All C statements end with a semicolon (;).

• Character strings are delimited by quotation marks ("). "Welcome" is therefore a


character string.

• The printf function displays messages on the screen.

• // This program displays a welcome message, placed at the end of the line is a comment.

26
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

6.4 Development environment


The development environment (or IDE for Integrated Development Environment) allows you
to develop programs, from entering the source code to execution. There are many IDEs for
the C/C++ language. We can cite: Visual C++, Builder, Qt Creator, Code::Blocks, DevC++,
eclipse, etc.

27
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

6.5 Predefined functions



To do complex calculations such as x2 , log(x), x, etc. we use the predefined (standard)
functions.
Example: the square root of an integer: Root(x), where the word Root is the name of the
function and x is the parameter or argument.
Remarks

• The argument x can be a constant, variable or generally an expression,

• A function can have zero, one or more arguments.

Examples of C functions: Let E be an expression, here are some functions from the C
standard “math.h” library. To use them, we must add the following line at the start of the
program: #include<math.h>.

28
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

29
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Example: The following program calculates the square root of a real number.

6.6 Exercises

Exercise 1:
1- Translate the following algorithm to C
2- What does this program do?

Solution:

Exercise 2: Translate the following algorithm to C.

Solution:

30
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

Exercise 3: Write a program that reads a duration in seconds, then converts it to hours,
minutes and seconds, example: 5670 seconds to 1 hour, 34 minutes and 30 seconds.

Solution:

Exercise 4: Write a program that allows you to solve a 1st degree equation: Ax + B = 0

Solution:

31
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts

If A = 0, there is a runtime error (division by 0). To avoid this problem, we use the test
instruction, which we will discuss in the next chapter.

32

You might also like