Set-9 Sub_Algorithm
Set-9 Sub_Algorithm
Exercise 1: (Lecture)
Write an algorithm that calculates the maximum among 4 integers, using a function and a procedure.
Exercise 2: (Lecture)
1. Write an algorithm that calculates the maximum between the maximums of two vectors of
respective sizes n, m with n ≤ 100, m ≤ 100.
2. Write an algorithm that allows doing the same previous work using two sub-algorithms, the first
one ReadVec allows reading a vector, and the second one SumVec allows calculating the maximum
of a vector.
Exercise 3 (Tutorial)
Write an algorithm that calculates and displays the value of the following expression E:
1+2 1+2+3 1+2+⋯+n
𝐸= + +⋯ n is a given integer.
2! 3! 𝑛!
Use FctSum a function that calculates and returns the sum 𝟏 + 𝟐 + 𝟑 + ⋯ + 𝒌, where k is an integer,
and FctFact a function that calculates the factorial of an integer value k.
Exercise 4 (Tutorial)
Given a vector T of size n (n ≤ 50) of positive integers.
Write an algorithm that displays the largest prime number if it exists and displays the message
"There is no prime number" if all the numbers are not prime.
Exercise 5 (Tutorial)
1. Write a function FctAbsV that returns the absolute value of an integer N.
2. Write a procedure Prc_AbsV that calculates the absolute value of an integer N.
3. Write an algorithm that calculates and displays the average Avg of the absolute values of three
given integers A, B, and C using two methods: using the FctAbsV function and using the PrcAbsV
procedure.
Exercise 6 (Tutorial)
1. Write a sub-algorithm SumElt that calculates the sum of the elements of a vector V of real
numbers of size n (n ≤ 50) from position B to position E.
2. Write an algorithm that reads a vector Tab of real numbers of size N (N ≤ 50) and uses the previous
sub-algorithm SumElt to display:
a. The sum of all elements of T.
b. The sum of the first k elements of T.
c. The sum of the elements of the vector T from a given position p.
Exercise 7 (Tutorial)
1. Write the sub-algorithms:
a. ReadMtx: Reads a matrix M of integers of size n*m (n ≤ 20).
b. Sum: Calculates the sum of the elements of M found from row p1 to row p2, and from
column q1 to column q2.
c. DsplMtx: Displays the matrix M.
1
Saad Dahlab university 1st year / Math
Faculty of Science ADS2
2. Write an algorithm that allows to read a square matrix A of integers of order n (n ≤ 20 with n
even) then use the Sum function to:
a. Calculate and display the sums of the elements of the 2nd and the 3rd quarter of matrix A.
b. Calculate and display the sum of all elements of A.
Exercise 8 (Tutorial)
1. Let ch be a string of length n ≤ 100, and a given character C. Write a sub-algorithm EXISTS that
checks if the character C exists in the string ch or not.
2. Let ch1 and ch2 be two strings of respective sizes n1 ≤ 100 and n2 ≤ 100.
Using the previous sub-algorithm, write an algorithm that calculates the intersection of ch1 and
ch2 (i.e., the characters of ch1 that exist in ch2 but without repetition) and puts them in a third
string ch3, then displays ch3.
Example: ch1="abgtfd" ch2="bdfhjam" ch3="bfd"
Exercise 9 (Tutorial)
1. Define a data type Complex.
A complex number z is fully defined by its real part a and imaginary part b.
𝒛 = 𝒂 + 𝒊𝒃
2. Write a procedure ReadCplx that reads a complex number X.
3. Write a procedure SumCplx that calculates the sum of two complex numbers X: (a + bi) and Y(c+
di) using the following formula: (a + bi) + (c + di) = (a + c) + (b + d)i
4. Write a procedure ProdCplx that calculates the product of two complex numbers X: (a+bi) and
Y(c+ di) using the following formula: (a + bi) * (c + di) = (a*c – b*d) + (a*d + b*c)i
5. Write a procedure DisplCplx that displays a complex number X.
6. write an algorithm that: (Using the previous procedures)
a. Reads two complex numbers A and B,
b. Calculates and displays the sum and the product of A and B.
Exercise 10(Tutorial)
Define a Data type Time that contains the following members: hour, minute, second.
1. Write a function Transform that transforms a time T of type Time into an integer S that expresses
this time in seconds.
Example: for T = 3 hours 15 minutes 57 seconds, S = 11757 seconds.
2. Write a procedure Decompose that decomposes a time S expressed in seconds into a time T of
type TIME.
Example: for S = 4968 seconds, T = 1 hour 22 minutes 48 seconds.
3. Write a procedure Sum that calculates the sum T of two durations T1 and T2 of type Time. (Using
the Transform and Decompose sub-algorithms).
4. Write an algorithm that reads two times Tm1 and Tm2, then calculates and displays the sum of
Tm1 with Tm2.