TD2 Algo 2023 Cne2
TD2 Algo 2023 Cne2
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.
3/4
Introduction to algorithms Abdelhamid Mehri – Constantine 2 University
1st year (2023-2024) Common Core – MI, NTIC Faculty
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.
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