0% found this document useful (0 votes)
41 views

Exam - ASD1 - 2022 2023

The document describes a first exam for a mathematics course. It contains 5 exercises: 1. Analyze an algorithm that sorts an array based on the modulo of elements with a given number k. Students are asked to run the algorithm and determine what it does. 2. Complete an algorithm to compute the modulo of two numbers. 3. Write a function to determine if a year is a leap year based on divisibility rules. 4. Write a function that checks if an array contains a palindrome. 5. Write a function to find the index of the first occurrence of a number in an array. Rewrite it assuming the array is sorted.

Uploaded by

Ikram Bbezzit
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)
41 views

Exam - ASD1 - 2022 2023

The document describes a first exam for a mathematics course. It contains 5 exercises: 1. Analyze an algorithm that sorts an array based on the modulo of elements with a given number k. Students are asked to run the algorithm and determine what it does. 2. Complete an algorithm to compute the modulo of two numbers. 3. Write a function to determine if a year is a leap year based on divisibility rules. 4. Write a function that checks if an array contains a palindrome. 5. Write a function to find the index of the first occurrence of a number in an array. Rewrite it assuming the array is sorted.

Uploaded by

Ikram Bbezzit
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

National Higher School of Mathematics

Year: 2022/2023 ASD I

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

What does the algorithm do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.............................................................................
.............................................................................
3. What will happen if k = 100?
.............................................................................
.............................................................................
First name:
Last name:
Section:
Group:

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

output("n modulo m = ”, m);


end

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.

Rewrite the function FindFirstOcc if we consider that T is sorted in ascending order.

3/3

You might also like