Вы находитесь на странице: 1из 13

ALGORITHMIQUE

Chapitre II

INTRODUCTION À LA PROGRAMMATION EN PASCAL


EXERCICES DE RÉVISIONS D’ALGORITHMIQUE : CHAPITRE II

Structure Générale d’un Programme Pascal

program nom du programme ;


uses wincrt;
label numéros des étiquettes ;
const nom de la variable = valeur de la variable ;
type nom de la variable = type de la variable;
var noms des variables : integer;
noms des variables : real;
noms des variables : boolean;
noms des variables : char;
noms des variables : string;
noms des variables : array [1..100] of nature des éléments ;
noms des variables : array [1..50,1..50] of nature des éléments ;
procedure nom de la procédure ( paramètres de la procédure ) ;
var variables internes ;
begin
Corps de la procédure ;
end ;
function nom de la fonction ( paramètres de la fonction ) : nature du résultat de la fonction ;
var variables internes ;
begin
Corps de la fonction ;
end ;
begin
Corps du programme ;
end.
Instructions du Traitement Alternatif
if Conditions then if Conditions then if Conditions then
begin begin begin
Instructions; Instructions; Instructions;
Instructions; Instructions; Instructions;
end; end end
else begin else if Conditions then
Instructions; begin
Instructions; Instructions;
end; Instructions;
end;

Instructions du Traitement Répétitif


Boucle For Boucle While Boucle Repeat
for i : = 1 to N do while Conditions do repeat
begin begin Instructions;
Instructions; Instructions; Instructions;
Instructions; Instructions; until Conditions;
end; end;

N.B Dans ce chapitre on s’est limité aux fonctions et aux procédures.


Les enregistrements et les …chiers requirent un chapitre supplémentaire.

F. HAMMAD http://exerev.yolasite.com - http://sites.google.com/site/exerev


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 2
1) DÉROULEMENT D’ALGORITHMES

1.1 Traduire l’algorithme suivant en


un programme Pascal. Solution

Algorithme Exercice1_1 Program Exercice1_1;


Variables i,n : entiers Var i,n : integer;
a,b,S : réels a,b,S : real;
Début label 1, 2, 3;
Écrire (’Donner a,b,n’) begin
Lire (a,b,n) writeln (’Donner a,b,n’);
Si a>b Alors i 1 read (a,b,n);
TantQue i 6 n Faire if a>b then begin
a a b i:=1;
b a+b while i <= n do
Si a=b Alors Aller à 1 begin
FinSi a:=a b;
i i+1 b:=a+b;
FinTantQue if a=b then goto 1;
1: S a+b i i+1;
Aller à 3 end;
Sinon Si a<b Alors i 1 1: S:=a+b;
Répéter goto 3;
a b+i end
b 2 a 6 else if a<b then begin
Si a=b Alors Aller à 2 i:=1;
FinSi Repeat
i i+1 a:=b+i;
Jusqu’à i>n b:=2 a 6;
FinSi if a=b then goto 2;
2: S a b i:=i+1;
3: Écrire (’S=’,S) until i>n;
Fin. end;
2: S:=a b;
3: write (’S=’,S);
end.

1.2 Dérouler l’algorithme précédent pour chacun des cas suivants


1. (a=5, b=5, n=3.) 2. (a=2, b=5, n=3.) 3. (a=2, b=1, n=3.) 4. (a=2, b=3, n=3.)

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.

Algorithme a Algorithme b Algorithme c


Variables i, N : entiers Variables i, N, k : entiers Variables i, N, k : entiers
r, S : réels r, S : réels r, S : réels
Début Début Début
Écrire (’Donner r et N’) Écrire (’Donner r et N’) Écrire (’Donner r et N’)
Lire (r,N) Lire (r,N) Lire (r,N)
S 0 S 0; k 1; i 1 S 0; k 1; i 1
Pour i 1 à N Faire TantQue i 6 N Faire Répéter
r r i r r i r r i i
S S+1/r S S+k r S S+k r
FinPour k k k k
Écrire (’S= ’, S) i i+1 i i+1
Fin. FinTantQue Jusqu’à i>N
Écrire (’S= ’, S) Écrire (’S= ’, S)
Fin. Fin.

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

2) TABLEAUX A UNE DIMENSION


2.1 Écrire un algorithme (puis le programme) qui lit les N éléments réels d’un tableau T
puis les range dans l’ordre inverse dans un tableau V et l’a¢ che.

Solution :

Algorithme Programme

Algorithme Exercice2_1 program Exercice2_1;


Variables i, N: Entiers uses wincrt;
T, V: tableau [1..100] de réels var i, N: integer;
Début T, V: array [1..100] of real;
Écrire (’Donner le nombre N d”éléments’) begin
Lire (N) write (’Donner le nombre N d”éléments’);
Écrire (’Donner les éléments de T’) readln (N);
Pour i 1 à N Faire writeln (’Donner les éléments de T’);
Lire (T[i]) for i := 1 to N do
FinPour readln (T[i]);
Pour i 1 à N Faire for i := 1 to N do
V[i] T[N+1 i] begin
Écrire (V[i]) V[i] := T[N+1 i];
FinPour writeln (V[i]);
Fin. end;
end.

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

Algorithme Exercice2_2 program Exercice2_2;


Variables i, N, k: entiers ; nom: chaîne de caractères uses wincrt;
T: tableau [1..100] de chaînes de caractères var i, N, k: integer;
Début nom: string;
Écrire (’Donner le nombre N d”étudiants’) T: array [1..100] of string;
Lire (N) begin
Écrire (’Donner les noms des étudiants’) write (’Donner le nombre N d”étudiants’);
Pour i 1 à N Faire readln (N);
Lire (T[i]) writeln (’Donner les noms des étudiants’);
FinPour for i := 1 to N do
Écrire (’Donner la position k’) readln (T[i]);
Lire (k) write (’Donner la position k’);
Pour i k à N 1 Faire read (k);
T[i] T[i+1] for i := k to N 1 do
FinPour T[i] := T[i+1];
Fin. 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 5

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

Algorithme Exercice2_3 program Exercice2_3;


Variables i, N, nbr: entiers uses wincrt;
nom: chaîne de caractères var i, N, nbr: integer;
T: tableau [1..100] de chaînes de caractères nom: string;
Début T: array [1..100] of string;
Écrire (’Donner le nombre de personnes’) begin
Lire (N) write (’Donner le nombre de personnes’);
Écrire (’Donner les noms des personnes’) readln (N);
Pour i 1 à N Faire writeln (’Donner les noms des personnes’);
Lire (T[i]) for i := 1 to N do
FinPour readln (T[i]);
Écrire (’Donner le nom à chercher’) writeln (’Donner le nom à chercher’);
Lire (nom) readln (nom);
nbr 0 nbr := 0;
Pour i 1 à N Faire for i := 1 to N do
Si nom = T[i] Alors if nom = T[i] then
nbr nbr+1 begin
Écrire (’Le nom existe en position ’, i); nbr := nbr+1;
FinSi writeln (’Le nom existe en position ’, i);
FinPour end;
Si nbr<>0 Alors if nbr<>0 then
Écrire (’Le nombre d”apparition du nom est ’, nbr) write (’Le nombre d”apparition du nom est ’, nbr)
Sinon else
Écrire (’Le nom n”existe pas’) write (’Le nom n”existe pas’);
FinSi end.
Fin.

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

Algorithme Exercice2_4 Program Exercice2_4;


Variables i, N, K, L: entiers ; s: réel uses wincrt;
T: tableau [1..100] de réels var i, N, K, L: integer ; s: real;
Début T: array [1..100] of real;
Écrire (’Donner le nombre N d”éléments’) begin
Lire (N) write (’Donner le nombre N d”éléments’);
Écrire (’Donner les éléments de T’) readln (N);
Pour i 1 à N Faire writeln (’Donner les éléments de T’);
Lire (T[i]) for i := 1 to N do
FinPour readln (T[i]);
Écrire (’Donner les positions à permuter’) writeln (’Donner les positions à permuter’);
Lire (K, L) read (K, L);
s T[K] s := T[K];
T[K] T[L] T[K] := T[L];
T[L] s T[L] := s;
Fin. end.

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

Algorithme Exercice2_5 program Exercice2_5;


Variables i, j, N: entiers ; s: réel uses wincrt;
T: tableau [1..100] de réels var i, j, N: integer ; s: real;
Début T: array [1..100] of real;
Écrire (’Donner le nombre N d”éléments’) begin
Lire (N) write (’Donner le nombre N d”éléments’);
Écrire (’Donner les éléments de T’) read (N);
Pour i 1 à N Faire write (’Donner les éléments de T’);
Lire (T[i]) for i := 1 to N do
FinPour read (T[i]);
Pour i 1 à N 1 Faire for i := 1 to N 1 do
Pour j i+1 à N Faire for j := i+1 to N do
Si T[i]<T[j] Alors if T[i]<T[j] then
s T[i] begin
T[i] T[j] s := T[i];
T[j] s T[i] := T[j];
FinSi T[j] := s;
FinPour end;
FinPour 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 7

3) TABLEAUX A DEUX DIMENSIONS.


3.1 Ecrire un algorithme (puis le programme) qui réalise le produit d’une matrice A d’ordre
(N M) par un vecteur U d’ordre M, puis a¢ che le vecteur résultant V d’ordre N.
P
M
(Rappel: Vi = Aij Uj =Ai1 U1 +Ai2 U2 +:::+AiM UM :)
j=1

Solution :

Algorithme Programme

Algorithme Exercice3_1 program Exercice3_1;


Variables i, j, N, M: entiers uses wincrt;
A: tableau [1..50,1..50] de réels; var i, j, N, M: integer;
U, V: tableau [1..50] de réels A: array [1..50,1..50] of real;
Début U, V: array [1..50] of real;
Écrire (’Donner l”ordre N M de A’) begin
Lire (N, M) writeln (’Donner l”ordre N M de A’);
Écrire (’Donner les éléments de la matrice A’) readln (N, M);
Pour i 1 à N Faire writeln (’Donner les éléments de la matrice A’);
Pour j 1 à M Faire for i := 1 to N do
Lire (A[i,j]) for j := 1 to M do
FinPour read (A[i,j]);
FinPour writeln (’Donner les éléments du vecteur U’);
Écrire (’Donner les éléments du vecteur U’) for i := 1 to M do
Pour i 1 à M Faire read (U[i]);
Lire (U[i]) for i := 1 to N do
FinPour begin
Pour i 1 à N Faire V[i] := 0;
V[i] 0 for j := 1 to M do
Pour j 1 à M Faire V[i] := V[i]+A[i,j] U[j];
V[i] V[i]+A[i,j] U[j] end;
FinPour for i := 1 to N do
FinPour writeln (V[i]);
Pour i 1 à N Faire end.
Écrire (V[i]);
FinPour
Fin.

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

Algorithme Exercice3_2 program Exercice3_2;


Variables i, j, N, M, L1, L2: entiers uses wincrt;
s : réel var i, j, N, M, L1, L2: integer;
A: tableau [1..50,1..50] de réels s : real;
Début A: array [1..50,1..50] of real;
Écrire (’Donner l”ordre de la matrice A’) begin
Lire (N, M) write (’Donner l”ordre de la matrice A’);
Écrire (’Donner les éléments de la matrice A’) readln (N, M);
Pour i 1 à N Faire writeln (’Donner les éléments de la matrice A’);
Pour j 1 à M Faire for i := 1 to N do
Lire (A[i,j]) for j := 1 to M do
FinPour read (A[i,j]);
FinPour writeln (’Donner les rangs des lignes à permuter’);
Écrire (’Donner les rangs des lignes à permuter’) readln (L1, L2);
Lire (L1, L2) for j := 1 to M do
Pour j 1 à M Faire begin
s A[L1, j] s := A[L1, j];
A[L1, j] A[L2, j] A[L1, j] := A[L2, j];
A[L2, j] s A[L2, j] := s;
FinPour end;
Pour i 1 à N Faire for i := 1 to N do
Saut de ligne begin
Pour j 1 à M Faire writeln;
Écrire (A[i,j], ’ ’) for j := 1 to M do
FinPour write (A[i,j], ’ ’);
FinPour end;
Fin. end.

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

Algorithme Exercice4_2 program Exercice4_2;


variables N, i, j, k, f : entiers uses wincrt;
x, S, r : réel var N, i, j, k, f :integer;
Début x, S, r :real;
Écrire (’donner un chi¤re compris entre -1 et 1’) begin
Lire (x); write (’donner un chi¤re compris entre -1 et 1’);
TantQue abs(x)>1 Faire readln (x);
Écrire (’Erreur: le chi¤re est incorrect, répéter’) while abs(x)>1 do
Lire (x) begin
FinTantQue writeln (’Erreur: le chi¤re est incorrect, répéter’);
Écrire (’Donner un entier impaire et positif N’) readln (x);
Lire (N) end;
TantQue (N<0) ou ((N mod 2)=0) Faire writeln (’Donner un entier impaire et positif N’);
Écrire (’Erreur: le chi¤re est incorrect, répéter’) readln (N);
Lire (N) while (N<0) or ((N mod 2)=0) do
FinTantQue begin
S x writeln (’Erreur: le chi¤re est incorrect, répéter’);
r x readln (N);
k 1 end;
i 3 S :=x;
TantQue i <= N Faire r :=x;
r r x x k := 1;
f 1 i := 3;
Pour j 1 à i Faire while i <= N do
f f j; begin
FinPour r := r sqr(x);
S S+ k r/f f := 1;
k k for j := 1 to i do
i i+2 f := f j;
FinTantQue S := S+ k r/f;
Écrire (’S =’, S); k := k;
Fin. i := i+2;
end;
write (’S =’, S);
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 11
4.3 Réécrire le programme de l’exercice 4.1 sous forme d’un programme principal qui fait
appel à une Procédure pour calculer la somme des vecteurs U et V , à une Fonction pour
calculer le produit scalaire U:V et une autre Fonction pour calculer le module de U .

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.

Вам также может понравиться