Larbi Ben M'hidi -Oum El Bouaghi- University
Faculty of Exact Sciences and Natural and Life Sciences
Mathematics and Computer Science Department
Academic year: 2023-2024
Level: 1st year “Computer Science & Mathematics”
Module: Algorithmic and Data Structures 2
TD n°2
Pedagogic objectives
Manipulate sub-algorithms (subroutines): procedures & functions;
Understand the difference between them;
Understand the concepts: local variable , global variable, formal parameter, effective
parameter, passing parameters by value and by address.
Exercise n°1
A procedure is declared as follows:
Procedure P_Test (A, B, C: integer);
Variable S: integer;
Begin
S A+B+C;
Write (S);
End;
1. Write a main algorithm that calls this procedure.
2. Specify local and global variables, formal and effective parameters.
3. Replace the procedure P_Test with a function F_Test.
4. Call this function in the main algorithm.
Exercise n° 2
Consider the following algorithm:
1
Algorithm exo2;
Variables x, y, z, t: integer;
Procedure my_procedure (a, b, var c, d: integer)
Begin
c←a+b;
d ← a * b;
End;
Begin
read (x);
read (y);
my_procedure (x, y, z, t);
write (z);
write ( t);
END
1. Identify the real ( effective ) parameters and the formal parameters .
2. What does this program display assuming the user enters 2 in x and 3 in y ? Modify the
algorithm to obtain a more logical result .
Exercise n°3
Consider the following algorithm:
Algorithm exo3;
Variables T: array [1…100] integer;
i,N :integer ;
Procedure P1 (T: array [1.. N ] integer);
Variables i, a: integer;
Begin
a ←0 ;
For i ← 1 to N do
a←a+ T [i] ;
Endfor
Write (a);
End;
Procedure P2 (T: array [1.. N ] integer);
Variables i , b: integer;
Begin
b← 1 ;
For i ← 1 to N do
b←b *T[i] ;
Endfor
Write (b);
End;
Begin
Repeat
2
Read(N);
Until (N>=1 and N<100)
For i ← 1 to N do
Read (T[i]);
Endfor
P1(T);
P2(T);
END
1. Run the algorithm with the following array:
1 0 2 4 3 1 2 1 2 3
2. What is the role of the procedure P1?
3. What is the role of the procedure P2?
4. In the main algorithm, is it possible to do the following calculation:
C1 = a/2 and C2 = b/2? Justify your answer.
5. Replace both procedures with functions. In this case is it possible to calculate C1and C2
in the main algorithm? Justify your answer.
Exercise n°4
1. Write a FindVal sub-algorithm that indicates whether a value is contained in a one-
dimensional array (with the size N). If so, the sub-algorithm must indicate in which cell the
value was found.
2. Write a sub-algorithm which takes as parameters two arrays of real numbers and which
returns the value true if they are identical, false otherwise.
3. Design local and global variables, formal and effective parameters.
Exercise n°5
A positive integer is perfect if it is equal to the sum of its divisors (except itself). For example
6 is perfect, because 6 = 1+2+3; similarly 28 is perfect, because 28 = 1 + 2 + 4 + 7 + 14.
1. Write a function Som_Div which calculates the sum of the divisors of n .
2. Write a P_perfect_procedure which uses the Som_Div function and indicates whether n
is perfect or not.
3. Transform this procedure into a Boolean function F_ perfect.
4. Use the previous three sub-algorithms in an algorithm.
5. Design local and global variables, formal and effective parameters as well as sub-algorithms
calls.