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

University of Science and Technology Houari Boumediene

Faculty of computer science


Algorithmics and Data Structures INGENIEUR.INFO: 2023/2024

TD N° 2: Arrays and Strings


(Part n°1 : Vectors)

Exercise 1:

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


1. Determine the minimum, maximum, product of positive values and 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

Exercice 4:
Let T be a vector of N distinct integers (N <=50). Write an algorithm that:
1. For each element T[i] determine the number of values less than or equal to T[i] in the
vector T and put it in another vector V at the same position i.
2. Put each element T[i] into a vector R at the position indicated by V[i].
3. Display the minimum and maximum of the vector values.
T: 17 15 23 41 2 25 12 8
Example
V: 5 4 6 8 1 7 3 2
(N=8)
R: 2 8 12 15 17 23 25 41

Exercice 5:
Let A be a vector of N integers (N<=100). We say that an element X of A is Majority if it
appears strictly more than (N Div 2) times in this vector. Write an algorithm allowing:
1. Fill the table A.
2. Display the Majority element X of A, if it exists.
3. In the case where element X exists, delete the necessary number of occurrences of X
so that it is no longer considered Majority in A after deletion.

A: 4 1 4 4 -5 30 4 8 4 4 4 1 18 4 4
Example
Element X=4 appears 9 times, 9 > (15 div 2 = 7), so 4 is Majority in A
(N = 15 )
A (after deletion): 1 -5 30 4 8 4 4 4 1 18 4 4

Exercise 6:
Let T be a vector of N numbers (N≤50). Write an algorithm that reverses, in T, the first
increasing sequence of numbers.
Example : N =13
T 9 8 2 3 5 7 2 6 8 4 3 1 7
After the reverse :
T 9 8 7 5 3 2 2 6 8 4 3 1 7

Exercise 7:
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 in 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
TD N° 2: Arrays and Strings
(Part n°2 : Matrices)

Exercise 8:
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 9:
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 10:
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 11:
1. Write an algorithm that checks if an integer is prodigious. A number is called
prodigious if it is divisible by the product of its non-zero digits.
(Example: A=2016, 2*1*6=12 and 2016 is divisible by 12)

2. Given a square matrix of integers Write an algorithm allowing the prodigious


elements of the main diagonal to be replaced by the sum of the elements of the
corresponding line, then display the matrix if it has undergone modifications.

Exercise 12:
Let A be a square matrix of order N of integers (N<=20). Write an algorithm to fill this matrix
and check if it is symmetrical. If it is symmetrical, display the non-duplicated values as well
as their respective positions.
Remark: A symmetric matrix is a square matrix that is equal to its own transpose.
TD N° 2: Arrays and Strings
(Part n°3 : Strings)

Exercise 13:
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 14:
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 15:
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 16:
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"

Exercise 17:
Let S be a character string constituting a sequence of words separated by one or more
blanks (spaces). Write an algorithm allowing you to invert the words of S without touching
the blanks.
Example: S = “Algiers is the capital” gives S = “sreiglA si eht latipac”

Exercise 18:
Write an algorithm to check if two words are ANAGRAMS. Knowing that a word is said to be
an anagram of another word if they are made up of the same letters.
Examples: LISTEN is an anagram of SILENT.
RACE is an anagram of CARE
GOOGLE is not an anagram of GLOBAL.

You might also like