TD 3-2025

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

University of Science and Technology Houari Boumediene

Faculty of computer science


Algorithmics and Data Structures INGENIEUR.INFO

TD N° 3: Arrays

Exercise 1:

Let T be a vector of N integers (N <=100) Write an algorithm allowing:


1. Determine the minimum, maximum, and product of positive values and the average of
negative values.
2. Calculate the number of occurrences of a given value VAL in T.
3. Display the elements of vector T in reverse order (starting with the last element).
4. Insert a given value VAL at the ith position.
5. Remove all null values.
6. Move negative values to the beginning using only the T vector.
7. Show the longest-ordered sequence.
8. Calculate the minimum distance between two distinct elements belonging to T.

Exercise 2:
Let V1 and V2 be two integer vectors containing N and M elements respectively (N<=100
and M<=200) Write an algorithm allowing:
1. Check if the two vectors are identical.
2. Show elements of V1 that do not belong to V2.
3. Calculate the sum and the scalar product of the two vectors (V1 and V2).
4. Merge the two vectors knowing that the latter are sorted in ascending order. The created
vector must be sorted in the same order.

Exercise 3:
The goal of this exercise is to display prime numbers less than a value N using the
Eratosthenes method by following the following steps:
1. Fill a vector T with N integers such that each element is equal to the value of its index.
2. For each non-zero element of T with index i>1, find the multiples of this value and
replace them with the zero value (0)
3. Remove all values less than 2 from vector T.
4. Show the remaining values of T.
Write an algorithm allowing you to carry out the previous tasks using a single vector T of size
N (N <=100).
T: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

T: 1 2 3 0 5 0 7 0 9 0 11 0 13 0 15 0 17 0 19 0

Example …
(N = 20) T: 1 2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0

T: 2 3 5 7 11 13 17 19
Exercise 4:
Let V1 be a vector of N integers (2≤N≤100). Write an algorithm allowing to:
1. Fill the array V1 such that each element contains a value between 0 and 9.
2. Determine and display the largest sum Smax of 2 contiguous (adjoined) elements of V1.
3. Create a vector V2 containing the contiguous elements of V1 whose sum is equal to Smax
(the elements of V1 copied into V2 must have the same number in V1 and V2) and display
V2.
4. Delete from V2 all sequences of successive repetitive values (keep only the first
occurrence) and display V2. →Do not use a vector other than V2 in this question.
Example : N =13
V1 6 8 6 3 7 7 7 6 8 2 7 7 7
The largest sum of 2 consecutive elements of V1 is 14 because:
SMax = V1[1] + V1[2] = 14 as well as: V1[2] + V1[3], V1[5] + V1[6], V1[6] + V1[7], , V1[8] +
V1[9], V1[11] + V1[12] and V1[12] + V1[13]
The creation of V2 will give
V2 6 8 6 7 7 7 6 8 7 7 7
Removing repeating values from V2 will give:
V2 6 8 6 7 6 8 7

Exercise 5:
Let A(N, M) be a matrix of characters (N <= 20 and M <= 30). Write an algorithm allowing:
1. Calculate the number of vowels belonging to matrix A.
2. Find a character in matrix A.
3. Determine the transpose of the matrix A.
4. Rotate the columns of matrix A.
5. Insert a vector V in the Jth column.

Exercise 6:
Let A(N, M) be a matrix of integers (N <= 20 and M <= 30). Write an algorithm allowing:
1. Calculate and save the sum of each column.
2. Determine the position Jmin of the minimum sum and the position Jmax of the
maximum sum.
3. Permute the two columns of indices Jmin and Jmax of matrix A if Jmin > Jmax.

Exercise 7:
Let A(N, N) be a square matrix of integers (N<=25). Write an algorithm allowing:
1. Fill the matrix with numbers between 0 and 9.
2. Calculate the trace of the matrix A. The trace is the sum of the elements of the main
diagonal.
3. Determine the maximum and its position of the values of the two diagonals (main and
secondary).
4. Swap the elements of the main diagonal with the elements of the secondary diagonal.
5. Determine and display the inverse of the number formed by the digits of the main
diagonal.
6. Check if matrix A is lower triangular. A matrix is lower triangular if it has only zeros
above the diagonal.
Exercise 8:
Let CH be a character string. Write an algorithm allowing:
1. Check if this string contains all vowels.
2. Calculate the number of vowels contained in this string.
3. Calculate the frequency of a substring SCH in this string.
4. Calculate the number of characters, words, and sentences contained in this string.
Knowing that words are separated by spaces “ “ and sentences by periods “.”.
5. Check if this string contains a tautogram sentence. A tautogram sentence is a sentence
in which all the words begin with the same letter.

Exercise 9:
Write an algorithm to determine whether a word is a palindrome. Knowing that a palindrome
is read from left to right and from right to left. Example: RADAR, LOL, BOB.

Exercise 10:
Let S be a character string. Write an algorithm to check whether S is a square or not. A
character string is a square if it consists of 2 identical strings.
Example: “ByeBye” and “Nana” are squares.

Exercise 11:
Let S be a character string. Write an algorithm to clean the string S of all non-alphabetic
characters but leaving spaces (the white character).
Example: S="/ Th(e Al!@gor>ithmic m&odule is °$ inte?resting."
gives S ="The Algorithmic module is interesting"

You might also like