0% found this document useful (0 votes)
45 views4 pages

TD2 Algo 2023 Cne2

The document contains a series of exercises related to conditional and logical expressions in algorithms: 1. The exercises involve writing algorithms to solve problems that include calculating results based on conditional expressions, identifying syntax errors, evaluating logical expressions for different values, calculating BMI based on weight and height, finding the largest of several values, and more. 2. Additional exercises involve writing algorithms to solve linear and quadratic equations, calculate hotel pricing based on room types and numbers, and determine the number of days in a given month for a given year. 3. The exercises are meant to practice writing algorithms using basic conditional and logical expressions and operations.

Uploaded by

Halima DOUIBI
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)
45 views4 pages

TD2 Algo 2023 Cne2

The document contains a series of exercises related to conditional and logical expressions in algorithms: 1. The exercises involve writing algorithms to solve problems that include calculating results based on conditional expressions, identifying syntax errors, evaluating logical expressions for different values, calculating BMI based on weight and height, finding the largest of several values, and more. 2. Additional exercises involve writing algorithms to solve linear and quadratic equations, calculate hotel pricing based on room types and numbers, and determine the number of days in a given month for a given year. 3. The exercises are meant to practice writing algorithms using basic conditional and logical expressions and operations.

Uploaded by

Halima DOUIBI
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/ 4

Introduction to algorithms Abdelhamid Mehri – Constantine 2 University

1st year (2023-2024) Common Core – MI, NTIC Faculty

Exercise series No. 2 : The conditional (alternative) structure


Do not consider possible user error cases in this series

Exercise 2.01:
What is the final result of running each of the following algorithms?
Algo Exo2_1_a Algo Exo2_1_b The mod operation gives the
Var Var remainder of the division
A, B, C : Integer N, P : Integer (13 mod 5 = 3)
Begin Begin
Read (A,B) For A=3 and B=6, C= Read (N) For N = 6, P =
If A < 5 or B < 8 Then For A=3 and B=10, C= If N mod 2 = 0 Then For N = 9, P =
C A+B*2 N N+4
Else For A=5 and B=10, C= EndIf
C A*2+B P 1+N*3
EndIf Write(P)
Write (C) End
End
Algo Exo2_1_c Algo Exo2_1_d
Var Var
N, R : Integer A, B, C, D : Real
Begin Begin
Read(N) For N = 5, R = Read(A,B,C) For A=2, B=15 and C=9, D =
If N < 0 Then For N = -13, R = D A For A=0, B=-3 and C=12, D =
R 0 If B > D Then
Else For N = 21, R = D B For A=27,B=10 and C=19, D=
If N < 10 Then EndIf
R 1 If C > D Then
Else D C - What does this algorithm do?
R 2 EndIf
EndIf Write (D)
EndIf End
Write(R)
End

Exercise 2.02:
The algorithm in the 1st box is syntactically correct.
a- Identify the syntax errors in each of the following pseudo codes (from 2 to 9) if there are any.
1 2 3 4 5
Algo Exo2_2_1 Algo Exo2_2_2 Algo Exo2_2_3 Algo Exo2_2_4 Algo Exo2_2_5
Var Var Var Var Var
N : Integer N : Integer N : Integer N : Integer N : Integer
Begin Begin Begin Begin Begin
Read(N) Read(N) Read(N) Read(N) Read(N)
If N < 10 Then If N < 10 If N < 10 : If N < 10 Then If N < 10 Then
Write("digit") Write("digit") Write("digit") Write("digit") Write("digit")
Else Else Else Else EndIf
Write("number") Write('number') Write("number") Write("number") End
EndIf EndIf EndIf
End End End End

1/4
Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
1st year (2023-2024) Common Core – MI, NTIC Faculty

6 7 8 9
Algo Exo2_2_6 Algo Exo2_2_7 Algo Exo2_2_8 Algo Exo2_2_9
Var Var Var Var
N : Integer N : Integer N : Integer N : Integer
Begin Begin Begin Begin
Read(N) Read(N) Read(N) Read(N)
If N < 10 Then If N < 10 Then If 0 ≤ N < 10 Then If (N < 10) and (N ≥ 0) Then
Write("digit") Write("digit") Write("digit") Write("digit")
Else If N < 2 Then Else Else
EndIf Write("binary") Write("number") Write("number")
End EndIf EndIf EndIf
Else End End
Write("number")
EndIf
End
Exercise 2.03:
a- Which of the following logical expressions are syntactically correct?

X≤3 X <= 3 X =< 3 0 ≤ X < 10 (X ≥ 0) and (X < 10) X=100 or X ≥0 and X <10 If (X ≥0 and X < 10 or X =100)
and X ≠ 1 Then
Not(X ≥ 0 and X < 10) If X = 1 Then If X == 1 Then ...

b- Consider the following logical expression: X = 100 or X ≥ 0 and X < 10. Say for each value of X, if it is
True or False:
X 5 10 100 -14
Logical value

Exercise 2.04 :
The body mass index (BMI) is a numeric measure of an adult's body size. It is calculated as follows:
(kg/m ) =
The adopted classification is:
BMI < 18.5 Thinness 18.5 ≤ BMI ≤ 25 Normal range BMI > 25 Overweight
- Write two variants of an algorithm (with and without nested conditional structures) that allows you to estimate
a person’s body size based on their weight and height.

Exercise 2.05 :
Write an algorithm that asks for five (5) real values and displays the largest of them. If there are two or more
equal values, it should not be specified.
Exercise 2.06 :
a- Write an algorithm that checks if an integer Nb is even or odd (whether positive or negative), by displaying
“Even” or “Odd” message. (Use mod operation)
b- Write an algorithm that asks the user to give three integers, and displays those that are multiples of 5.

Exercise 2.07 (additional) :


Write an algorithm that asks the user to enter four (4) strictly positive integers, and displays the largest number
multiple of 6 among the numbers entered.
Example: In the case where the user enters 12, 38, 24, 20, the algorithm must display: 24
2/4
Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
1st year (2023-2024) Common Core – MI, NTIC Faculty

Exercise 2.08 (additional) :


a- Write an algorithm that solves a first degree equation (aX+b=0), where (a, b) ϵ R2 and a ≠ 0.
b- Write an algorithm that solves a first degree equation (aX+b=0), where (a, b) ϵ R2.
c- Write an algorithm that solves a quadratic equation in R (aX2+bX+c=0), where (a, b, c) ϵ R3 and a ≠ 0.
d- Write an algorithm that solves a quadratic equation in R (aX2+bX+c=0), where (a, b, c) ϵ R3.
- R : the set of real numbers.
- Mathematical writing : (a, b) ϵ R2 is the equivalent of : a ϵ R and b ϵ R
Note : the square root operation √ is written in algorithmic as follows : sqrt(x)
Exercise 2.09 (additional):
Write a hotel pricing algorithm. The prices are as follows:
7500 Dinars for an adult in a single room;
6000 Dinars per room for three or more single rooms reserved;
9500 Dinars for two adults in a double room;
8000 Dinars per room for four or more double rooms reserved;
Free for the first child;
3000 Dinars per child from the second;
Breakfast is obligatory and set at 300 Dinars per person regardless of age.
Note : A double room must accommodate two and only two adults. Children, whatever their number, can
accompany adults in a double or single room.
To calculate the total price to pay, you must therefore enter the following information: number of single rooms,
number of double rooms, number of children and number of nights.

Exercise 2.10 (additional):


The calendar year is divided into 12 months numbered 1 to 12 with the following principles:
- months 1, 3, 5, 7, 8, 10 and 12 consist of 31 days.
- months 4, 6, 9 and 11 consist of 30 days.
- month 2 consists of 29 days if the year is a leap year and 28 days otherwise.
Note: a year (AD : After Christ) is a leap year if :
- it is divisible by 4 and not divisible by 100, or
- it is divisible by 400.
- Write an algorithm which has a month and a year as data and which displays the number of days in this
month.
Exercise 2.11 (additional):
a- Write an algorithm intended to read the hours and minutes from the keyboard to display the time it will be
one minute later. For example, if the user types 21 then 32, the algorithm should respond:
“In a minute it will be 21:33”. Write two variants: without and with nested conditional structure.
b- Rewrite the algorithm to display the time it will be 8 minutes later.

3/4
Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
1st year (2023-2024) Common Core – MI, NTIC Faculty

Exercise 2.12 (additional):


Consider coefficients and credits of the different courses of the 1st semester of the MI common core.
Unit Unit1 Unit2 Unit3 Unit4
Matière Algo1 Anal1 Alg1 Terminology Bureautique CRI Compo Ang1
Coefficient
4 4 2 1 1 2 2 1
(17)
Credit
6 5 6 4 3 2 2 2
(30)
a- Write an algorithm which, based on the marks of the 8 courses of a given student:
- calculates his GA1 (General Average of the 1st semester).
- calculates the Sum of his credits (SC1) as follows:
If the student has obtained a semester GA ≥ 10, we grant him 30 credits, otherwise if he has obtained a unit
GA ≥ 10, we grant him the sum of the unit credits, otherwise we grant him the sum of credits for courses for
which he obtained a mark ≥ 10.

b- Knowing that the sum of the coefficients of the second semester is 16, modify the algorithm of question 'a' so
that, in addition, from GA2 (General Average of the 2nd semester) and SC2 (Sum of Credits of the 2nd
semester), we display the final result of a given student.

At the end of the year, a student is considered to be:


Admitted: if he has an AGA (Annual General Average) ≥ 10/20.
Admitted with debt: if his AGA < 10/20 and has more than 45 credits in the year.
Adjourned: otherwise.

Exercise 2.13:
Check the equivalent algorithms among the following:

Algo Ex_2_13_a Algo Ex_2_13_b Algo Ex_2_13_c Algo Ex_2_13_d Algo Ex_2_13_e
Var Var Var Var Var
A, B, C : Real A, B, C : Real A, B, C : Real A, B, C : Real A, B, C : Real
Begin Begin Begin Begin Begin
Read (A, B) Read (A, B) Read (A, B) Read (A, B) Read (A, B)
C A / B*B C A / (B*B) C A / B*B C A / B*B C A / B*B
If C < 10 Then If C ≤ 10 Then If C < 10 Then If C > 20 Then If C ≤ 20 Then
Write (A) Write (A) Write (A) Write (B) Write (C)
EndIf EndIf Else Else Else
If C≥10 and C≤20 Then If C>10 and C≤20 Then If C ≤ 20 Then If C < 10 Then If C < 10 Then
Write (C) Write (C) Write (C) Write (A) Write (A)
EndIf EndIf Else Else Else
If C > 20 Then If C > 20 Then Write (B) Write (C) Write (B)
Write (B) Write (B) EndIf EndIf EndIf
EndIf EndIf EndIf EndIf EndIf
End End End End End

4/4

You might also like