TD 2
TD 2
NHSM 2024/2025
Exercise 01: Run the algorithms below and write down what they display.
algorithm assignment1;
start
X, Y, Z : integer;
X ← 4;
Y ← 5;
print(X, Y);
X ← (3*Y)+1;
print(X, Y);
Z ← X;
X ← Y;
Y ← Z;
print(X, Y, Z);
end
algorithm assignment2;
start
X, Y, Z: integer;
read(X, Y);
print(X, Y);
if (X > Y) then X ← (3*Y)+1;
else X ← X-Y;
endif
print(X, Y);
Z ← X;
X ← Y;
Y ← Z;
print(X, Y, Z);
end
1
National Higher School of Mathematics
Department of Preparatory Cycle
1st Preparatory Cycle year (PC1) - 2023/2024
Algorithms and Data Structures I (ADS1)
algorithm assignment3;
start
X, Y: integer;
print(X, Y);
read(X, Y);
if (X > Y) then (3*Y)+1 ← X;
else X ← (3*Y)+1;
endif
read(X, Y);
print(X, Y);
X ← Z;
z ← X;
Y ← X;
print(X, Y, Z);
fin
Exercise 03:
Write an algorithm for each of the following questions:
1. Display the absolute value of an integer x. The user should be able to enter the value of x.
2. Display the minimum of two real numbers entered by the user at runtime.
3. Extend the solution of the previous question to three numbers.
4. Perform the permutation of values of two real variables.
5. Compute the average mark of a given student from the CC and the final exam marks. If the
mark of the final exam is larger than that of the CC, then the average of the subject is equal to that of
the final exam. Otherwise, the average mark = (mark of the final paper + CC mark)/2.
Exercise 04:
Write an algorithm “Bill” that displays the total purchase amount knowing the unit price of the item and
the requested quantity. The bill must apply the following discounts:
• If the number of purchased items exceeds 15, a first reduction of 15% is applied.
• After the first discount:
- If the total amount is between 150$ and 200$, a 10$ discount is applied.
- If the total amount is more than 200$, a discount of 15$ is applied.
2
National Higher School of Mathematics
Department of Preparatory Cycle
1st Preparatory Cycle year (PC1) - 2023/2024
Algorithms and Data Structures I (ADS1)
Exercise 05:
Write an algorithm for each of the following formula (n is entered by the user):
• S = 2 + 4 + 6 + ⋯ + 2n.
! " # %
• S= "
+#+$ + ⋯ + %&!
.
• n! = 1 × 2 × 3 × ⋯ × (n − 1) × n.
Exercise 06:
Write an algorithm to solve an equation of first order 𝑎 ∗ 𝑥 = 𝑏, where 𝑎 and b are two real numbers
and 𝑥 is the unknown variable (to be displayed).