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

Subject One Algorithme 1

ji
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)
9 views4 pages

Subject One Algorithme 1

ji
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

Bouchibane Mohamed Abdelwahab Subject 1


Subject 01
Exercise 01
• Write an algorithm that verifies if an integer n, given by the user, is a perfect cube

– example : √
27 is perfect cube because√3 27 = 3
8 is perfect cube because 3 8 =
√ 2
3
9 is not perfect cube because 9 ≈ 2.082

Exercise 02
• Write algorithm that calculates this sum :
n
X 1
(−1)i
i=1
ii

• (n) is variable given by the user

• Don’t use any function for calculates power

Exercise 03
• Write an algorithm to check if a matrix of nn is an upper triangular matrix

– is a square matrix in which all the elements below the main diagonal are zero. Formally:

aij = 0 for all i > j

– example :
 
1 3 4 5  
0 1 4 6
 2 7 9
 0 2 9
0 0 3 0
0 0 3 3×3
0 0 0 1 4×4

1
Bouchibane Mohamed Abdelwahab Subject 1

 

Solution 
Exercise 01
Algorithme ex01;
Var
n, i : integer;
perfectC : bool;
Begin
Read(n);
perfectC ← false;
i ← 0;
While (i ∗ i ∗ i <= n and not perfectC) do
if (i ∗ i ∗ i = n) then perfectC ← true
else i ← i + 1;

if (perfectC) then Write(n, ”is a perfect cube”)


else Write(n, ”is not a perfect cube”);
End.

Exercise 02
i1 1 1 1 1

Pn
i=1 (−1) i = −1 + 2
− 3 + 4 + ··· + n
i 2 3 4 n

Algorithme ex02;
Var
n, i, j, m : integer;
s, p : real;
Begin
Read(n);
s ← 0 ; m ← −1 ;

For i from 1 to n do
Begin
p←1;
For j from 1 to i do
p ← p ∗ (1/i) ;
s ← s + m ∗ p;
m ← m ∗ −1;
End

2
Bouchibane Mohamed Abdelwahab Subject 1

Write(s);
End.

Exercise 03
Solution 01 (Optimal)

Algorithme ex03;
Var
A : array[50][50] of integers;
n, i, j : integer;
isUpperTriangular : bool;
Begin
Read(n);
For i from 0 to n − 1 do
For j from 0 to n − 1 do
Read(A[i][j]);

isUpperTriangular ← true;
For i from 0 to n − 1 do
For j from 0 to i − 1 do
if (A[i][j] <> 0) then
isUpperTriangular ← false;
End
if (isUpperTriangular) then
Write(”the matrix is an upper triangular matrix”);
else
Write(”the matrix is not an upper triangular matrix”);
End.

Solution 02 (Iterative)

Algorithme ex03;
Var
A : array[50][50] of integers;
n, i, j : integer;
isUpperTriangular : bool;
Begin
Read(n);

3
Bouchibane Mohamed Abdelwahab Subject 1

For i from 0 to n − 1 do
For j from 0 to n − 1 do
Read(A[i][j]);

isUpperTriangular ← true;
For i from 0 to n − 1 do
For j from 0 to n − 1 do
if (i > j and A[i][j] <> 0) then
isUpperTriangular ← false;
End
if (isUpperTriangular) then
Write(”The matrix is an upper triangular matrix”);
else
Write(”The matrix is not an upper triangular matrix”);
End.

You might also like