Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Ammar Telidji University

Computer sciences department


1st Year L.M.C.S Algorithms and data structures 2
Series 01

Note: In this series of exercises, for each parameterised action requested, you must specify
whether it is a function or a procedure, naming the input parameters and the output parameters.

Exercise 1

1. Write a parameterized action to check whether a real number is positive or not, and use
it action in the algorithm that calculates the square root of a given value.
2. Write the parameterized action which takes as input two positive integer values A and
B, and calculates the first as a power of the second (AB ).
3. Write the parameterized action that calculates the product of two positive integer values
A and B using the successive addition method.

Exercicse 2

A perfect number is a natural number N such that the sum of its positive integer divisors = 2 *
N . In other words, a natural number is perfect if it is equal to half the sum of its divisors. So 6
is a perfect number because 2 × 6 = 12 = 1 + 2 + 3 + 6, or 6 = 1 + 2 + 3.

We want to write an algorithm that displays all the perfect numbers in an interval of values
given by the user, to do this we use parameterised actions.

1. Write a parameterized action to check whether an integer is prime or not.


2. Use the previous parameterized action in the parameterized action that displays the
divisors of a given number.
3. Use the previous parameterized action in the parameterized action that checks whether
an integer is perfect or not.
4. Use the previous parameterized action in the algorithm that displays all the perfect
numbers in an interval between two values X and Y.

Exercicse 3

Using a parameterized action that calculates the factorial of a positive integer, write the
algorithm that calculates the sum S such that :

S= 1+(1*2)+(1*2*3)+(1*2*3*4)+…..+(1*2*3*4*…*n-1*n)

Exercicse 4

1. Write a parameterized action to find the max and min of a vector.


2. Write a sub-program which checks whether a vector is sorted or not.
Ammar Telidji University
Computer sciences department
1st Year L.M.C.S Algorithms and data structures 2
Exercise 5

Let T be an array of 100 integer elements.

 Write the algorithm that splits this array into two arrays, the first array for positive
values, and the second array for negative values.

We want to do the same job as the previous algorithm, using subprograms. To do this, we use
a subprogram called Split, which splits an array in two.

 Rewrite the previous algorithm using the sub-programme Split.

Exercise 6

Write an algorithm to calculate the number of occurrences of a value given by the user in a
matrix A[M, N] of integers.

We now want to do the same job as the previous algorithm using a parameterized action called
Occurrence.

1. Should the parameterized action used be a function or a procedure? Justify your answer.
2. Write the parameterized action Occurrence, specifying its parameters.
3. Use the parameterized action Occurrence in an algorithm to calculate the number of
occurrences of 3 different values X1, X2, and X3 respectively in 3 integer matrixes: A
[M, N], B [M, N] and C [M, N].

You might also like