Home 5thse Algorithm1
Home 5thse Algorithm1
Exercise 1
Algorithm Algo_1 ; Algorithm Algo_2 ; Algorithm Algo_3 ;
Var i,N : integer ; Var i,N,S : integer; Var i,N : integer;
Nb : array of [1..50] of integer; Nb : array of [1..50] of integer; T : array of [1..80] of integer;
Begin Begin Begin
Repeat Repeat Repeat
Read(N); Read (N); Read (N);
Until ((N>0) et (N≤50)) ; Until ((N>0) et (N≤50)) ; Until ((N>0) et (N≤80)) ;
T[1]←100 ;
For (i from 1 to N ) do S←0; For (i from 2 to N ) do
Nb[i]←i * i ; For (i from 1 to N ) do T[i] ← T[i-1] + 2 ;
Endfor Nb[i]←i * i ; Endfor
S←S +Nb[i] ;
For (i from 1 to N ) do Endfor For (i from 1 to N ) do
Write(Nb[i]) ; Write (T[i]) ;
Endfor Write (S) ; Endfor
End. End. End.
Algorithm Algo_4 ; Algorithm Algo_5 ; Algorithm Algo_6 ;
Var i,N : integer; Var i,N,S,P : integer; Var i,N,j,k,X : integer;
T : array of [1..150] of integer; T : array of [1..70] of integer; T1 : array of [1..40] of integer;
Begin Begin T2 : array of [1..40] of integer;
Repeat Repeat Begin
Read (N); Read (N); Repeat
Until ((N>0) et (N≤150)) ; Until ((N>0) et (N≤70)) ; Read (N);
Until ((N>0) et (N≤40)) ;
T[1]←8 ; For (i from 1 to N ) do j← 0 ;k ← 0 ;
For (i from 2 to N ) do Read (T[i]) ; For (i from 1 to N ) do
T[i] ← T[i-1] *3 ; Endfor Read (X) ;
Endfor S←0 ; If(X mod 5 =0 ) Then
P ←1 ; j← j+1 ;
For (i from 1 to N ) do For (i from 1 to N ) do T1[j]←X ;
Write (T[i]) ; If(T[i] mod 2 =0 ) Then Else
Endfor S ← S + T[i] ; k← k+1 ;
Else T2[k]←X ;
End. P ← P*T[i] ; Endif
Endif Endfor
Endfor For (i from 1 to j ) do
Write (S,P) ; Write (T1[i]) ;
End. Endfor
For (i from 1 to k ) do
Write (T2[i]) ;
Endfor
End.
Page 1/3
Consider the preceding algorithms:
Exercise 2
4.
Let V1and V2 be two vectors (one-dimensional arrays) of N integers for each (N≤150).
Write the algorithm that determines the sum and scalar product of V1 and V2.
The sum of V1 and V2 is V3 such that : V3[i]=V1[i]+V2[i]
The scalar product of V1 and V2 = ∑ (𝑉1[𝑖 ] ∗ 𝑉2[𝑖 ]).
Exercise 3
Let TAB be an array of N real numbers (N does not exceed 120).
1- Write an algorithm to check whether the array TAB is sorted or not.
2- Write an algorithm to sort the array TAB in descending order.
3- Write an algorithm to invert the elements of an array.
Example
4- Write an algorithm to insert a VAL value at the kth position in the TAB array. Note: in this
question (N<120).
5- Assume that the array TAB is sorted in ascending order and (N<120). Write an algorithm that
inserts a real number X into this array while maintaining the sorted array.
Exercise 4
Let V1and V2 be two vectors (one-dimensional arrays) of N integers for each (N≤150).
1- Write an algorithm that constructs, from the previous vectors, two vectors MAX and MIN defined
as follows:
MAX[i] = maximum between V1[i] and V2[i].
MIN[i] = minimum between V1[i] and V2[i].
Exercise 5
Let T be an array of characters containing a sentence ending in a point. Write a C program that
determines :
The number of words in the sentence
The longest word in the sentence
Exercise 6
Let Tab be an array of N characters containing a single word.
Page 2/3
1- Write a C program that determines the mirror word of a given word in the Tab array.
The "mirror" word of a given word is obtained by reading the word from the end to the
beginning.
Example :
Tab B a T n A Its mirror is : A n T a B
2- Write a C program that checks whether the word given in the Tab array is a palindrome.
Exercise 7
First element "1.2", its frequency = 5 (it exists 5 times in the table)
Its positions are : 1, 4, 5, 10, 14.
2- Write an algorithm that allows the table TAB to be copied into another table TAB2 while
removing duplicates.
Exemple
Page 3/3