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

TD 2

Td serie math

Uploaded by

hanaefodil
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

TD 2

Td serie math

Uploaded by

hanaefodil
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

Department of Preparatory Cycle


1st Preparatory Cycle year (PC1) - 2023/2024
Algorithms and Data Structures I (ADS1)

NHSM 2024/2025

Exercises sheet n⁰ 02: Introduction to algorithms

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)

Exercise 02: Check the algorithm below. Is it correct?


What should we change in order to make it working correctly?

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

Exercise 07: Write an algorithm for each of the following questions:


• Return 1 if an integer b is a divisor of a, otherwise, return 0.
• Calculate the sum of all the divisors of an integer a (a>0).
• Given three real numbers a, b, and x, returns a number among a and b that is the closest to x.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy