ASD1
ASD1
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
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
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 )
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
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
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.
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
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
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
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).
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.
23
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts
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
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
• 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.
• // This program displays a welcome message, placed at the end of the line is a comment.
26
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts
27
Dr. Samir FENANIR Chapter 1 : Algorithmic Concepts
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:
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