0% found this document useful (0 votes)
26 views3 pages

Home 5thse Algorithm1

The document contains a series of exercises for first-year students in Mathematics and Computer Science at the University of BATNA 2 for the 2024-2025 academic year. It includes algorithms for various tasks such as calculating sums, scalar products, sorting arrays, and determining word properties in C programs. Additionally, it requires students to rewrite certain algorithms using different programming constructs and to perform specific operations on arrays and vectors.

Uploaded by

larbiamar063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Home 5thse Algorithm1

The document contains a series of exercises for first-year students in Mathematics and Computer Science at the University of BATNA 2 for the 2024-2025 academic year. It includes algorithms for various tasks such as calculating sums, scalar products, sorting arrays, and determining word properties in C programs. Additionally, it requires students to rewrite certain algorithms using different programming constructs and to perform specific operations on arrays and vectors.

Uploaded by

larbiamar063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

University of BATNA 2

Faculty: Mathematics and Computer


Department: Common Core in Mathematics and Computer Science
1st Year CC-MCS 2024-2025 academic year
Home exercises

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:

1. Trace and say what each of the above algorithms does.


2. Choose one of the previous algorithms and rewrite its Repeat loop with the while loop.
3. Choose one of the above algorithms and rewrite its For loops as while loops.

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

TAB 5 1 8 2 2 3 Its inverse is: 3 2 2 8 1 5

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].

2- Assume that V1 and V2 are sorted in ascending order:


 Write an algorithm that fuses these two vectors into a vector V3 sorted in descending order.
 Modify the previous algorithm to remove any duplicates.

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.

A word is said to be palindrome if it is identical to its mirror word.

Exercise 7

Let TAB be an array of N real numbers (N does not exceed 120).


1- Write an algorithm that calculates the frequency (number of repetitions) of the first number in the
array, as well as all its positions.
Example :
1.2 8.3 0 1.2 1.2 4.5 8 0 9 1.2 0.5 10 3 1.2 1.8

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

TAB 4.1 1.7 3 7 3 1.7 7 9.5 TAB2 4.1 1.7 3 7 9.5

Page 3/3

You might also like