Академический Документы
Профессиональный Документы
Культура Документы
Chapitre II
Solution :
1. 2.
a=b a<b
i ←1
a ←6
b←6
a = b?
2 : S ← 36 Affiche S=36
2 : S ← 25 Affiche S=25
E X E R C I C E S D E R É V I S I O N S D ’A L G O R I T H M I Q U E C H A P . I I ( F . H A M M A D ) U N IV E R S IT É A .M IR A D E B E J A IA 2 0 0 9 -2 0 1 0 3
3. 4.
a>b
a<b
i ←1
i ←1 a←5
i ≤ 3? i ≤ 3? i ≤ 3? i ≤ 3? a←4
b←4
a←4 b←2
a←2 a ←6 a ← 54
b←3 b←9 b ← 63 b←2 a = b?
a = b?
a = b? a = b? a = b? a = b? i ←3 i ←4
i←2
i←3 i ←4
i←2 i > 3? i > 3?
1 : S ← 117
Aller a 3
i > 3?
2 : S ← 20
Affiche S=117
Affiche S=20
1.3 Dérouler chacun des algorithmes suivants (avec trace d’exécution) pour r=2, N=3.
Solution :
a) i r S b) r S k i c) r S k i
1 2 0+1/2=1/2 2*1=2 0+2=2 -1 2 2*1=2 0+2=-2 1 2
2 4 1/2+1/4=3/4 2*2=4 2-4=-2 1 3 2*4=8 -2+8=6 -1 3
3 12 3/4+1/12=10/12 4*3=12 -2+12=10 -1 4 8*9=72 6-72=-66 1 4
A¢ che S=0,83. A¢ che S=10. A¢ che S=-66.
E X E R C I C E S D E R É V I S I O N S D ’A L G O R I T H M I Q U E C H A P . I I ( F . H A M M A D ) U N IV E R S IT É A .M IR A D E B E J A IA 2 0 0 9 -2 0 1 0 4
Solution :
Algorithme Programme
2.2 En supposant qu’un tableau T contient N noms d’étudiants, écrire un algorithme (puis
le programme) qui demande d’introduire une position k, puis supprime le nom à la position
k dans le tableau T. (Sans a¢ cher le résultat)
Solution :
Algorithme Programme
2.3 Ecrire un algorithme (puis le programme) qui range N noms de personnes dans un tableau
T, puis demande d’introduire un nom et véri…e si le nom existe et a¢ che, s’il existe, en
quelles positions dans T et le nombre de ses apparitions.
Solution :
Algorithme Programme
2.4 Ecrire un algorithme (puis le programme) qui range N chi¤res réels dans un tableau,
puis demande d’introduire deux positions K et L et permute les deux chi¤res en ces
deux positions. (Sans a¢ cher le résultat)
0 1 0 1
1 9
B 5 C B 5 C
Exemple: Pour K=1, L=3: B C B C
@ 9 A !@ 1 A
0 0
E X E R C I C E S D E R É V I S I O N S D ’A L G O R I T H M I Q U E C H A P . I I ( F . H A M M A D ) U N IV E R S IT É A .M IR A D E B E J A IA 2 0 0 9 -2 0 1 0 6
Solution :
Algorithme Programme
2.5 Ecrire un algorithme (puis le programme) qui range N chi¤res réels dans un tableau,
puis réalise le tri de ses éléments dans l’ordre décroissant. (Sans a¢ cher le résultat. )
Exemple: 0 2 1 5 ! 5 2 0 1
Solution :
Algorithme Programme
Solution :
Algorithme Programme
3.2 Ecrire un algorithme (puis le programme) qui lit une matrice A d’ordre (N M), demande
d’introduire les numéros des lignes à permuter L1 et L2, puis permute les éléments des
deux lignes L1 et L2 et a¢ che la matrice
0 ainsi transformée.
1 0 1
1 0 4 3 5 2 9 1
Exemple: Pour L1=1, L2=3: @ 3 5 7 2 A ! @ 3 5 7 2 A
5 2 9 1 1 0 4 3
E X E R C I C E S D E R É V I S I O N S D ’A L G O R I T H M I Q U E C H A P . I I ( F . H A M M A D ) U N IV E R S IT É A .M IR A D E B E J A IA 2 0 0 9 -2 0 1 0 8
Solution :
Algorithme Programme
3.3 Écrire un algorithme (puis le programme) qui demande d’introduire une matrice carrée
T: (N N) contenant des réels, puis calcule la moyenne des éléments sur la diagonale.
Demande le numéro d’une colonne C puis calcule la moyenne d’éléments de cette colonne.
Solution :
Algorithme
Programme
Algorithme Exercice3_3
program Exercice3_3;
Variables i, j, N: entiers
uses wincrt;
moyD, moyC: réel
var i, j, N: integer;
T: tableau [1..50,1..50] de réels
moyD, moyC: real;
Début
T: array [1..50,1..50] of real;
Écrire (’Donner l”ordre N de la matrice T’)
begin
Lire (N)
writeln (’Donner l”ordre N de la matrice T’);
Écrire (’Donner les composantes de T’)
readln (N);
Pour i 1 à N Faire
writeln (’Donner les composantes de T’);
Pour j 1 à N Faire
for i := 1 to N do
Lire (T[i,j])
for j := 1 to N do
FinPour
read (T[i,j]);
FinPour
E X E R C I C E S D E R É V I S I O N S D ’A L G O R I T H M I Q U E C H A P . I I ( F . H A M M A D ) U N IV E R S IT É A .M IR A D E B E J A IA 2 0 0 9 -2 0 1 0 9
moyD 0
moyD := 0;
Pour i 1 à N Faire
for i := 1 to N do
moyD moyD+T[i,i]/N
moyD := moyD+T[i,i]/N;
FinPour
write (’Donner le numéro de la colonne’)
Écrire (’Donner le numéro de la colonne’)
read (C); moyC:=0;
Lire (C); moyC 0
for i := 1 to N do
Pour i 1 à N faire
moyC := moyC+T[i,C]/N;
moyC moyC+T[i,C]/N
end.
FinPour
Fin.
4) PROBLÈMES DIVERS
!
4.1 Écrire un algorithme (et le programme) qui demande les N composantes de deux vecteurs U
!
et V, puis calcule et a¢ che:
! ! !
a. La somme: W = U + V: q
! P
b. Le module du vecteur U : Mod_U = U21 + U22 + ::: + U2N = ( U2i )1=2 :
!! P
c. Le produit scalaire : Sca_UV = U. V = U1 V1 +:::+UN VN = Ui Vi .
Solution :
Algorithme
Programme
Algorithme Exercice4_1
program Exercice4_1;
Variables i, N: entiers ; S, Mod_U, Sca_UV : réels
uses wincrt;
U, V, W: tableau [1..100] de réels
var i, N: integer; S, Mod_U, Sca_UV : real;
Début
U, V, W: array [1..100] of real;
Écrire (’Donner le nombre de composantes’)
begin
Lire (N)
writeln (’Donner le nombre de composantes’);
Écrire (’Donner les composantes de U’)
readln (N);
Pour i 1 à N Faire
writeln (’Donner les composantes de U’);
Lire (U[i])
for i := 1 to N do
FinPour
read (U[i]);
Écrire (’Donner les composantes de V’)
writeln (’Donner les composantes de V’);
Pour i 1 à N Faire
for i := 1 to N do
Lire (V[i])
read (V[i]);
FinPour
S := 0; Sca_UV := 0;
S 0 ; Sca_UV 0
for i := 1 to N do
Pour i 1 à N Faire
begin
W[i] U[i] + V[i]
W[i] := U[i] + V[i];
S S + sqr (U[i])
S := S + sqr (U[i]);
Sca_UV Sca_UV + U[i] V[i]
Sca_UV := Sca_UV + U[i] V[i];
FinPour
end;
Mod_U sqrt(S)
Mod_U := sqrt(S);
Écrire (’Le vecteur W est:’)
writeln (’Le vecteur W est:’);
Pour i 1 à N Faire
for i := 1 to N do
Écrire (W[i])
writeln (W[i]);
FinPour
writeln (’Le module de U est: ’ , Mod_U);
Écrire (’Le module de U est: ’ , Mod_U)
writeln (’Le produit scalaire est: ’ , Sca_UV);
Écrire (’Le produit scalaire est: ’, Sca_UV)
end.
Fin.
E X E R C I C E S D E R É V I S I O N S D ’A L G O R I T H M I Q U E C H A P . I I ( F . H A M M A D ) U N IV E R S IT É A .M IR A D E B E J A IA 2 0 0 9 -2 0 1 0 10
4.2 Ecrire un algorithme (puis le programme) qui demande un entier positif impaire N et un
chi¤re réel x compris entre -1 et 1. Si le chi¤re réel introduit n’est pas dans l’intervalle
[-1,1] ou si l’entier N est négatif ou paire, il a¢ che le message:
(Erreur: le chi¤ re est incorrect, répéter ) jusqu’à ce que le chi¤re réel soit dans l’intervalle
3 5 N
voulu et l’entier soit positif et impaire, puis calcule et a¢ che la somme S=x x3! + x5! ::: xN ! :
Solution :
Algorithme Programme
Solution :
program Exercice4_3;
uses wincrt;
type vect = array [1..100] of real;
var i, N: integer ; S : real;
U, V: vect;
procedure Somme(var X,Y : vect; k: integer);
var p: integer; S:vect;
begin
for p := 1 to k do begin
S[p] := X[p]+Y[p];
writeln (S[p]);
end;
end;
function Module(var Z: vect; h: integer): real;
var q: integer; c: real;
begin
for q := 1 to h do
c := c + sqr(Z[q]);
Module := sqrt(c);
end;
function PScalaire(var A,B: vect; m: integer): real;
var j: integer; PS: real;
begin
for j:= 1 to m do
PS := PS + A[j] B[j];
PScalaire := PS;
end;
begin
writeln (’Donner le nombre de composantes’);
readln (N);
writeln (’Donner les composantes de U’);
for i := 1 to N do
read (U[i]);
writeln (’Donner les composantes de V’);
for i := 1 to N do
read (V[i]);
writeln (’La somme U+V est:’);
Somme(U,V, N);
writeln (’Le module de U est: ’ , Module (U, N));
writeln (’Le produit scalaire U.V est: ’ , PScalaire(U,V, N));
end.
E X E R C I C E S D E R É V I S I O N S D ’A L G O R I T H M I Q U E C H A P . I I ( F . H A M M A D ) U N IV E R S IT É A .M IR A D E B E J A IA 2 0 0 9 -2 0 1 0 12
4.4 Réécrire le programme de l’exercice 4.2 sous forme d’un programme principal qui fait
appel à une Procédure pour contrôler la validité du chi¤re réel et du nombre entier
introduits, et fait appel à une Fonction pour calculer les factoriels des dénominateurs.
Solution :
program Exercice4_4;
uses wincrt;
var N, i, j, k :integer; x, S, r :real;
Function Factoriel(var q: integer): integer;
var f, p :integer;
begin
f := 1;
for p := 1 to q do
f := f p;
Factoriel := f;
end;
Procedure ControlChi¤re(var c: real);
begin
while abs(c)>1 do begin
writeln (’Erreur: le chi¤re est incorrect, répéter’);
readln (c);
end;
end;
Procedure ControlEntier(var M: integer);
begin
while (M<0) or ((M mod 2)=0) do begin
writeln (’Erreur: le chi¤re est incorrect, répéter’);
readln (M);
end;
end;
begin
write (’Donner un chi¤re compris entre -1 et 1’);
readln (x);
ControlChi¤re(x);
writeln (’Donner un entier impaire et positif N’);
readln (N);
ControlEntier(N);
S :=x;
r :=x;
k := 1;
i :=3;
while i <= N do begin
r := r sqr(x);
S := S+k r/Factoriel(i);
k := k;
i := i+2;
end;
write (’S =’, S);
end.