Solutions en Langage C - Série de TD N°4
Exercice 1
Énoncé : Soit T un tableau de vingt éléments de type réel. Écrire l’algo-
rithme qui permet de saisir les éléments de ce tableau et de calculer la somme
de ses éléments.
Solution en C :
1 # include < stdio .h >
2
3 int main () {
4 float T [20];
5 float somme = 0;
6 int i ;
7
8 // Saisie des lments
9 for ( i = 0; i < 20; i ++) {
10 printf ( " Entrez ␣ l ’ lment ␣ T [% d ]: ␣ " , i ) ;
11 scanf ( " % f " , & T [ i ]) ;
12 somme += T [ i ];
13 }
14
15 // Affichage de la somme
16 printf ( " La ␣ somme ␣ des ␣ lments ␣ est ␣ : ␣ %.2 f \ n " , somme
);
17
18 return 0;
19 }
Exercice 2
Énoncé : Soit T un tableau de 30 entiers. Écrire l’algorithme qui détermine
le plus petit élément de ce tableau et sa position.
Solution en C :
1
1 # include < stdio .h >
2
3 int main () {
4 int T [30] , i , min , position ;
5
6 // Saisie des lments
7 for ( i = 0; i < 30; i ++) {
8 printf ( " Entrez ␣ l ’ lment ␣ T [% d ]: ␣ " , i ) ;
9 scanf ( " % d " , & T [ i ]) ;
10 }
11
12 // Recherche du minimum
13 min = T [0];
14 position = 0;
15 for ( i = 1; i < 30; i ++) {
16 if ( T [ i ] < min ) {
17 min = T [ i ];
18 position = i ;
19 }
20 }
21
22 printf ( " Le ␣ plus ␣ petit ␣ lment ␣ est ␣ % d ␣ ␣ la ␣
position ␣ % d .\ n " , min , position ) ;
23
24 return 0;
25 }
Exercice 3
Énoncé : Écrire un algorithme qui saisit 10 caractères dans un tableau.
Puis, calcule le nombre de majuscules et le nombre de lettres dans ce tableau.
Solution en C :
1 # include < stdio .h >
2 # include < ctype .h >
3
4 int main () {
5 char T [10];
6 int i , majuscules = 0 , lettres = 0;
7
8 // Saisie des c a r a c t r e s
2
9 for ( i = 0; i < 10; i ++) {
10 printf ( " Entrez ␣ le ␣ c a r a c t r e ␣ T [% d ]: ␣ " , i ) ;
11 scanf ( " ␣ % c " , & T [ i ]) ;
12
13 if ( isalpha ( T [ i ]) ) lettres ++;
14 if ( isupper ( T [ i ]) ) majuscules ++;
15 }
16
17 printf ( " Nombre ␣ de ␣ lettres ␣ : ␣ % d \ n " , lettres ) ;
18 printf ( " Nombre ␣ de ␣ majuscules ␣ : ␣ % d \ n " , majuscules ) ;
19
20 return 0;
21 }
Exercice 4
Énoncé : Écrire un algorithme qui permet à l’utilisateur de saisir les notes
d’une classe de 40 élèves. Ensuite, renvoie le nombre des notes strictement
supérieures à la moyenne de la classe et enfin calcule le nombre d’occurrences
de la note maximale.
Solution en C :
1 # include < stdio .h >
2
3 int main () {
4 float notes [40] , somme = 0 , moyenne ;
5 int i , countAboveAvg = 0 , maxOccurrences = 0;
6 float max ;
7
8 // Saisie des notes
9 for ( i = 0; i < 40; i ++) {
10 printf ( " Entrez ␣ la ␣ note ␣ de ␣ l ’ lve ␣%d:␣", i +
1) ;
11 scanf ( " % f " , & notes [ i ]) ;
12 somme += notes [ i ];
13 }
14
15 moyenne = somme / 40;
16 max = notes [0];
17
18 // Calcul du nombre de notes s u p r i e u r e s la
moyenne
3
19 for ( i = 0; i < 40; i ++) {
20 if ( notes [ i ] > moyenne )
21 countAboveAvg ++;
22 if ( notes [ i ] > max )
23 max = notes [ i ];
24 }
25
26 // Calcul des occurrences de la note maximale
27 for ( i = 0; i < 40; i ++) {
28 if ( notes [ i ] == max )
29 maxOccurrences ++;
30 }
31
32 printf ( " Nombre ␣ de ␣ notes ␣ s u p r i e u r e s ␣ ␣ la ␣ moyenne ␣ :
␣ % d \ n " , countAboveAvg ) ;
33 printf ( " La ␣ note ␣ maximale ␣ est ␣ %.2 f ␣ et ␣ a p p a r a t ␣ % d ␣
fois .\ n " , max , maxOccurrences ) ;
34
35 return 0;
36 }
Exercice 5
Énoncé : Écrire un algorithme qui permet de lire un tableau d’entiers de
taille 10 et à l’aide d’une seule boucle for, affiche le minimum de la première
moitié et le maximum de la seconde moitié.
Solution en C :
1 # include < stdio .h >
2
3 int main () {
4 int T [10] , i , min , max ;
5
6 // Saisie des lments
7 for ( i = 0; i < 10; i ++) {
8 printf ( " Entrez ␣ l ’ lment ␣ T [% d ]: ␣ " , i ) ;
9 scanf ( " % d " , & T [ i ]) ;
10 }
11
12 // Initialisation
13 min = T [0];
14 max = T [5];
4
15
16 for ( i = 0; i < 5; i ++) {
17 if ( T [ i ] < min )
18 min = T [ i ];
19 }
20
21 for ( i = 5; i < 10; i ++) {
22 if ( T [ i ] > max )
23 max = T [ i ];
24 }
25
26 printf ( " Minimum ␣ de ␣ la ␣ 1 re ␣ m o i t i ␣ : ␣ % d \ n " , min ) ;
27 printf ( " Maximum ␣ de ␣ la ␣ 2 me ␣ m o i t i ␣ : ␣ % d \ n " , max ) ;
28
29 return 0;
30 }
Exercice 6
Énoncé : Écrire un algorithme qui cherche dans un tableau non trié si
un nombre x existe au moins une fois.
Solution en C :
1 # include < stdio .h >
2
3 int main () {
4 int T [10] , i , x , found = 0;
5
6 // Saisie des lments
7 for ( i = 0; i < 10; i ++) {
8 printf ( " Entrez ␣ l ’ lment ␣ T [% d ]: ␣ " , i ) ;
9 scanf ( " % d " , & T [ i ]) ;
10 }
11
12 // Saisie de x
13 printf ( " Entrez ␣ le ␣ nombre ␣ ␣ chercher ␣ : ␣ " ) ;
14 scanf ( " % d " , & x ) ;
15
16 // Recherche de x
17 for ( i = 0; i < 10; i ++) {
18 if ( T [ i ] == x) {
19 found = 1;
5
20 break ;
21 }
22 }
23
24 if ( found )
25 printf ( " Le ␣ nombre ␣ % d ␣ existe ␣ dans ␣ le ␣ tableau .\ n " ,
x);
26 else
27 printf ( " Le ␣ nombre ␣ % d ␣ n ’ existe ␣ pas ␣ dans ␣ le ␣
tableau .\ n " , x ) ;
28
29 return 0;
30 }
Exercice 7
Énoncé : Soient deux vecteurs U et V ayant N composantes entières
saisies au clavier. Écrire un algorithme permettant de :
— Calculer le vecteur somme de U et V .
— Calculer le produit cartésien de U et V .
— Calculer le produit scalaire de U et V .
Solution en C :
1 # include < stdio .h >
2
3 int main () {
4 int N , i , j ;
5 printf ( " Entrez ␣ la ␣ taille ␣ des ␣ vecteurs ␣ : ␣ " ) ;
6 scanf ( " % d " , & N ) ;
7
8 int U [ N ] , V [ N ] , somme [ N ] , produitCartesien [ N ][ N ];
9 int produitScalaire = 0;
10
11 // Saisie des vecteurs
12 printf ( " Entrez ␣ les ␣ lments ␣ du ␣ vecteur ␣ U ␣ :\ n " ) ;
13 for ( i = 0; i < N ; i ++) {
14 scanf ( " % d " , & U [ i ]) ;
15 }
16
17 printf ( " Entrez ␣ les ␣ lments ␣ du ␣ vecteur ␣ V ␣ :\ n " ) ;
18 for ( i = 0; i < N ; i ++) {
19 scanf ( " % d " , & V [ i ]) ;
6
20 }
21
22 // Calcul du vecteur somme
23 for ( i = 0; i < N ; i ++) {
24 somme [ i ] = U [ i ] + V [ i ];
25 }
26
27 // Calcul du produit scalaire
28 for ( i = 0; i < N ; i ++) {
29 produitScalaire += U [ i ] * V [ i ];
30 }
31
32 // Calcul du produit c a r t s i e n
33 for ( i = 0; i < N ; i ++) {
34 for ( j = 0; j < N ; j ++) {
35 produitCartesien [ i ][ j ] = U [ i ] * V [ j ];
36 }
37 }
38
39 // Affichage des r s u l t a t s
40 printf ( " Vecteur ␣ somme ␣ : ␣ " ) ;
41 for ( i = 0; i < N ; i ++) {
42 printf ( " % d ␣ " , somme [ i ]) ;
43 }
44 printf ( " \ nProduit ␣ scalaire ␣ : ␣ % d \ n " , produitScalaire )
;
45
46 printf ( " Produit ␣ c a r t s i e n ␣ :\ n " ) ;
47 for ( i = 0; i < N ; i ++) {
48 for ( j = 0; j < N ; j ++) {
49 printf ( " % d ␣ " , produitCartesien [ i ][ j ]) ;
50 }
51 printf ( " \ n " ) ;
52 }
53
54 return 0;
55 }
Exercice 8
Énoncé : Étant donné un tableau T de n entiers, écrire un algorithme
permettant d’inverser le tableau (le premier élément devient le dernier, le
7
deuxième devient l’avant-dernier, et ainsi de suite).
Solution en C :
1 # include < stdio .h >
2
3 int main () {
4 int n , i , temp ;
5
6 printf ( " Entrez ␣ la ␣ taille ␣ du ␣ tableau ␣ : ␣ " ) ;
7 scanf ( " % d " , & n ) ;
8
9 int T [ n ];
10
11 // Saisie des lments
12 for ( i = 0; i < n ; i ++) {
13 printf ( " Entrez ␣ l ’ lment ␣ T [% d ]: ␣ " , i ) ;
14 scanf ( " % d " , & T [ i ]) ;
15 }
16
17 // Inversion du tableau
18 for ( i = 0; i < n / 2; i ++) {
19 temp = T [ i ];
20 T [ i ] = T [ n - i - 1];
21 T [ n - i - 1] = temp ;
22 }
23
24 // Affichage du tableau i n v e r s
25 printf ( " Tableau ␣ i n v e r s ␣ : ␣ " ) ;
26 for ( i = 0; i < n ; i ++) {
27 printf ( " % d ␣ " , T [ i ]) ;
28 }
29 printf ( " \ n " ) ;
30
31 return 0;
32 }
Exercice 9
Énoncé : Soit T un tableau à deux dimensions de vingt lignes et quinze
colonnes. Écrire un algorithme qui permet de :
— Calculer la somme de tous les éléments de la matrice.
— Compter le nombre des éléments strictement positifs.
8
— Calculer la somme des éléments positifs (SomP) et la somme des éléments
négatifs (SomN).
— Déterminer le plus grand élément de la matrice et sa position.
— Déterminer le plus petit élément de la matrice et sa position.
Solution en C :
1 # include < stdio .h >
2
3 int main () {
4 int T [20][15] , i , j ;
5 int somme = 0 , countPos = 0 , max , min ;
6 int sommePos = 0 , sommeNeg = 0;
7 int maxRow , maxCol , minRow , minCol ;
8
9 // Saisie des lments
10 for ( i = 0; i < 20; i ++) {
11 for ( j = 0; j < 15; j ++) {
12 printf ( " Entrez ␣ l ’ lment ␣ T [% d ][% d ]: ␣ " , i ,
j);
13 scanf ( " % d " , & T [ i ][ j ]) ;
14 }
15 }
16
17 // Initialisation
18 max = min = T [0][0];
19 maxRow = maxCol = minRow = minCol = 0;
20
21 for ( i = 0; i < 20; i ++) {
22 for ( j = 0; j < 15; j ++) {
23 somme += T [ i ][ j ];
24
25 if ( T [ i ][ j ] > 0) {
26 countPos ++;
27 sommePos += T [ i ][ j ];
28 } else {
29 sommeNeg += T [ i ][ j ];
30 }
31
32 if ( T [ i ][ j ] > max ) {
33 max = T [ i ][ j ];
34 maxRow = i ;
35 maxCol = j ;
36 }
37
9
38 if ( T [ i ][ j ] < min ) {
39 min = T [ i ][ j ];
40 minRow = i ;
41 minCol = j ;
42 }
43 }
44 }
45
46 printf ( " Somme ␣ des ␣ lments ␣ : ␣ % d \ n " , somme ) ;
47 printf ( " Nombre ␣ d ’ lments ␣ positifs ␣ : ␣ % d \ n " ,
countPos ) ;
48 printf ( " Somme ␣ des ␣ lments ␣ positifs ␣ : ␣ % d \ n " ,
sommePos ) ;
49 printf ( " Somme ␣ des ␣ lments ␣ n g a t i f s ␣:␣%d\n",
sommeNeg ) ;
50 printf ( " Le ␣ plus ␣ grand ␣ lment ␣ est ␣ % d ␣ ␣ la ␣
position ␣ [% d ][% d ]\ n " , max , maxRow , maxCol ) ;
51 printf ( " Le ␣ plus ␣ petit ␣ lment ␣ est ␣ % d ␣ ␣ la ␣
position ␣ [% d ][% d ]\ n " , min , minRow , minCol ) ;
52
53 return 0;
54 }
10