Exam - ASD1 - 2022 2023
Exam - ASD1 - 2022 2023
First Exam
Duration: 2 hours
Exercise 1 :
Let’s consider the following algorithm:
Algorithm Weird_Sort
start
T : array [10] integer;
k, i, min_i, j, X : integer;
input(k);
for i ← 0 to 9 step 1 do
input(T [i]);
end for
for i ← 0 to 9 step 1 do
min_i ← i;
for j ← i + 1 to 9 step 1 do
if (T [j]%k) < (T [min_i]%k) then
min_i = j;
endif
end for
X ← A[i];
A[i] ← A[min_i];
A[min_i] ← X;
end for
end
1. Run the algorithm above for k = 5 and T = [69, 24, 97, 16, 69, 85, 93, 56, 98, 41],
then give the final values of the array T :
T =
2. If you know that if we run the algorithm with the same T and k = 3 we get
T = 69 24 69 93 97 85 16 56 98 41
and if k = 10 we get
T = 41 93 24 85 16 56 97 98 69 69
Exercise 2:
The algorithm hereafter is supposed to compute and to print the modulo of two integer numbers.
This algorithm is not complete and contains errors. Complete and correct it.
algorithm Modulo;
start
2n : integer;
repeat
output("Enter a positive integer (n):”);
input(n);
until ( );
repeat
output("Enter another positive integer (m):”);
input(n);
until ( );
while (n < m) do
n ¬ n + m;
endwhile
2/3
Exercise 3:
Write a function that determines if a year is a leap year (année bissextile, )ﺳﻧﺔ ﻛﺑﯾﺳﺔ.
We recall that every year that is exactly divisible by four is a leap year, except for years that are
exactly divisible by 100, but these centurial years are leap years, if they are exactly divisible by 400.
Exercise 4:
Write a function whose prototype is the following:
function PalindromeArray(I/ T: array [] char,
I/ n: integer): boolean
This function checks if the array T (that contains n characters) is a palindrome. We recall that a
palindrome is sequence of symbols (here characters) that reads the same backwards as forwards,
such as the word “madam”.
The function returns true if T is a palindrome. It returns false otherwise.
Exercise 5:
Given an array T of integer numbers, write a function or a procedure FindFirstOcc that finds
the first occurrence (the index) of a given number a. If the number a is not present in the
array, the function/procedure should report that as well.
For instance, if T = [2, 5, -1, 0, 6, 10, 8, 9, 6, 9] and a = 6 then the first occurrence of a
within the array T is located at index 4.
3/3