Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de...

21

Transcript of Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de...

Page 1: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

1

Page 2: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

TPN1 :structures de contrôle et les tableaux en

C

Responsable

A.SELLALI

Assistant

M.LICHOURI

02 Decembre 2013

Dernière mise à jour : 2014/02/06 à 10:38:31

1 Exercice 1 :(révisions)

Ecrire un programme c qui permet de resoudre l'equation ax�b � 0 (etudiertous les cas)

1.1 Solution Exercice 1

1 #include<s td i o . h>2 #include<conio . h>3 void main ( )4 {5 f loat a , b , x ;6 p r i n t f ("donner la valeur de a" ) ;7 s can f ("%f" ,&a ) ;8 p r i n t f ("donner la valeur de b" ) ;9 s can f ("%f" ,&b ) ;

10 i f ( a==0 && b==0)11 p r i n t f ("la solution est l'infinie" ) ;12 else i f ( a==0 && b!=0)13 p r i n t f ("impossible" ) ;14 else

15 {16 x=�b/a ;17 p r i n t f ("la solution est x=%f" , x ) ;18 }19 getch ( ) ;20 }

M.LICHOURI TPINFO3 :Page 2/21

2014/02/06

Page 3: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

2 Exercise 2 : (revisions)

Ecrire un programme c qui calcule la valeur de E de�nie par :

E �°n

i�1

�9i�

°nj�1

�4j2 � 3

sachant que n est un entier positif

2.1 Solution Exercice 2

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int i , j , n ,E, S ;7 p r i n t f ("donner la valeur de n" ) ;8 s can f ("%d" ,&n ) ;9 E=0;

10 for ( i =1; i<=n ; i++)11 {12 S=0;13 for ( j =1; j<=i ; j++)14 {15 S=S+4∗pow( j ,2)�3;16 }17 E=E+9∗ i+S ;18 }19 p r i n t f ("E=%d" ,E ) ;20

21 getch ( ) ;22 }

M.LICHOURI TPINFO3 :Page 3/21

2014/02/06

Page 4: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

3 Exercice 3

Ecrire un programme qui calcule la somme des composantes d'un vecteurdonne.

3.1 Solution Exercice 3

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int V[ 5 ] , i , S ;7 p r i n t f ("donner les valeurs du tableau V" ) ;8 for ( i =0; i <5; i++)9 {

10 s can f ("%d" ,&V[ i ] ) ;11 }12 p r i n t f ("la somme vaut: " ) ;13

14

15 S=0;16 for ( i =0; i <5; i++)17 {18 S=S+V[ i ] ;19 }20

21

22 p r i n t f ("%d" , S ) ;23

24 getch ( ) ;25 }

M.LICHOURI TPINFO3 :Page 4/21

2014/02/06

Page 5: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

4 Exercice 4

Ecrire un programme c qui permet de calculer la moyenne arithmetique deselements ai d'un vecteur veri�ant la condition : 0 <= ai <= d, d etant unevaleur donnee.

Exemple : [12 -1 4 6 8 7 9 2] et d=7moyenne = (4+6+7+2)/4

4.1 Solution Exercice 4

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int V[ 5 ] , i , S , d , cpt ;7 f loat moy ;8 p r i n t f ("donner la valeur de d" ) ;9 s can f ("%d" ,&d ) ;

10 p r i n t f ("donner les valeurs du tableau V" ) ;11 for ( i =0; i <5; i++)12 {13 s can f ("%d" ,&V[ i ] ) ;14 }15 p r i n t f ("la somme des elements compris entre 0 et d vaut: " ) ;16

17

18 S=0; cpt=0;19 for ( i =0; i <5; i++)20 {21 i f (V[ i ]>=0 && V[ i ]<=d)22 {23 S=S+V[ i ] ;24 cpt=cpt+1;25 }26 }27 p r i n t f ("%d\n" , S ) ;28 i f ( cpt !=0)29 {30 moy=S/ cpt ;31 p r i n t f ("leur moyenne vaux:%f" ,moy ) ;32 }33 else p r i n t f ("erreur!!!!" ) ;34

35 getch ( ) ;36 }

M.LICHOURI TPINFO3 :Page 5/21

2014/02/06

Page 6: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

5 Exercice 5

Ecrire un programme qui calcule le produit scalaire de deux vecteurs d'entiersU et V de n composantes.

5.1 Solution Exercice 5

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int V[ 5 ] , i , S ,U [ 5 ] ;7

8

9 p r i n t f ("donner les valeurs du tableau V" ) ;10 for ( i =0; i <5; i++)11 {12 s can f ("%d" ,&V[ i ] ) ;13 }14

15 p r i n t f ("donner les valeurs du tableau U" ) ;16 for ( i =0; i <5; i++)17 {18 s can f ("%d" ,&U[ i ] ) ;19 }20

21

22 p r i n t f ("le produit scalaire des deux vecteurs est: " ) ;23

24

25 S=0;26 for ( i =0; i <5; i++)27 {28

29 S=S+V[ i ]∗U[ i ] ;30

31 }32 p r i n t f ("%d\n" , S ) ;33

34

35 getch ( ) ;36 }

M.LICHOURI TPINFO3 :Page 6/21

2014/02/06

Page 7: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

6 Exercice 6

Ecrire un programme c qui a�che un vecteur dans l'ordre inverse.

6.1 Solution Exercice 6

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int V[ 5 ] , i ,TP[ 5 ] ,TN[ 5 ] ;7

8

9 p r i n t f ("donner les valeurs du tableau V" ) ;10 for ( i =0; i <5; i++)11 {12 s can f ("%d" ,&V[ i ] ) ;13 }14

15

16 for ( i =4; i >=0; i��)17 {18 p r i n t f ("%d\t" ,V[ i ] ) ;19 }20

21

22

23 getch ( ) ;24 }

M.LICHOURI TPINFO3 :Page 7/21

2014/02/06

Page 8: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

7 Exercice 7(à faire en devoir)

Ecrire un programme c qui permet de ranger des elements positifs d'unvecteur T dans un tableau TP, les elements negatifs dans un tableau TN. Pourles elements nuls, a�cher un message d'erreur.

7.1 Solution Exercice 7

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 int main ( )5 {6 int V[ 5 ] , i , j , k , nb ,TP[ 5 ] ,TN[ 5 ] ;7

8

9 p r i n t f ("donner les valeurs du tableau V" ) ;10 for ( i =0; i <5; i++)11 {12 s can f ("%d" ,&V[ i ] ) ;13 }14 // Tr i e r l e s va l eu r s du tab leau V dans deux t ab l e s15 // p o s i t i v e et negat ive s ' i l e x i s t e n t16 p r i n t f ("les valeurs positives sont (s'ils existent): \n" ) ;17 j =0;18 for ( i =0; i <5; i++)19 {20 i f (V[ i ]>0)21 {22 TP[ j ]=V[ i ] ;23 p r i n t f ("%d\t" ,TP[ j ] ) ;24 j++;25 }26

27

28 }29 p r i n t f ("\n les valeurs negatives sont (s'ils existent): \n" ) ;30 k=0;31 for ( i =0; i <5; i++)32 {33 i f (V[ i ]<0)34 {35 TN[ k]=V[ i ] ;36 p r i n t f ("%d\t" ,TN[ k ] ) ;37 k++;38 }39

40 }41 p r i n t f ("\n les valeurs nulles (s'ils existent): \n" ) ;

M.LICHOURI TPINFO3 :Page 8/21

2014/02/06

Page 9: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

42 nb=1;43 for ( i =0; i <5; i++)44 {45 i f (V[ i ]==0)46 {47 p r i n t f ("Erreur!!! y en a %d valeur nulle\n " , nb ) ;48 nb++;49 }50 }51

52 getch ( ) ;53 }

8 Exercice 8

Ecrire un programme qui calcule le schtroumpf de deux tableaux donnes.Pour calculer le schtroumpf, il faut multiplier chaque element du tableau 1 parchaque element du tableau 2, et additionner le tout.

Exemple :Tableau 1 : 4 � 8 � 7 - 12Tableau 2 : 3 � 6Le Schtroumpf : 3*4 + 3*8 + 3*7 + 3*12 + 6*4 + 6*8 + 6*7 + 6*12 = 279

8.1 Solution Exercice 8

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int V[ 5 ] , i , S ,U[ 2 ] , j ;7

8

9 p r i n t f ("donner les valeurs du tableau V" ) ;10 for ( i =0; i <5; i++)11 {12 s can f ("%d" ,&V[ i ] ) ;13 }14

15 p r i n t f ("donner les valeurs du tableau U" ) ;16 for ( j =0; j <2; j++)17 {18 s can f ("%d" ,&U[ j ] ) ;19 }20

21

22 p r i n t f ("le schtroumpf des deux vecteurs est: " ) ;

M.LICHOURI TPINFO3 :Page 9/21

2014/02/06

Page 10: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

23

24

25 S=0;26 for ( i =0; i <5; i++)27 {28 for ( j =0; j <2; j++)29 {30 S=S+V[ i ]∗U[ j ] ;31 }32 }33 p r i n t f ("%d\n" , S ) ;34

35

36 getch ( ) ;37 }

9 Exercice 9(a faire en devoir)

Ecrire un programme c qui recherche du plus grand et du plus petit elementd'un vecteur et leur position respective.

9.1 Solution Exercice 9

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 int main ( )5 {6 int V[ 5 ] , i ,max , min , pmax , pmin ;7

8

9 p r i n t f ("donner les valeurs du tableau V" ) ;10 for ( i =0; i <5; i++)11 {12 s can f ("%d" ,&V[ i ] ) ;13 }14

15 max=V[ 0 ] ;16 min=V[ 0 ] ;17 for ( i =0; i <5; i++)18 {19 i f (V[ i ]>=max)20 {21 max=V[ i ] ;22 pmax=i +1;23 }24 i f (V[ i ]<=min )

M.LICHOURI TPINFO3 :Page 10/21

2014/02/06

Page 11: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

25 {26 min=V[ i ] ;27 pmin=i +1;28 }29

30 }31 p r i n t f ("la valeur maximale est %d sa position est %d\n" ,max , pmax ) ;32 p r i n t f ("la valeur minimale est %d sa position est %d" ,min , pmin ) ;33

34

35 getch ( ) ;36 }

10 Exercice 10

Ecrire un programme c qui fait la recherche de la valeur et de la position dupremier element pair d'un vecteur et a�che un message au cas où le vecteur necontient aucun element pair.

10.1 Solution Exercice 10

1ere Methode :

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int V[ 5 ] , i , val , pos ,num;7

8

9 p r i n t f ("donner les valeurs du tableau V" ) ;10 for ( i =0; i <5; i++)11 {12 s can f ("%d" ,&V[ i ] ) ;13 }14

15 i =0,num=0;16 while ( i <5 && num==0)17 {18 i f (V[ i ]%2==0)19 {20 va l=V[ i ] ;21 pos=i +1;22 num=1;23 p r i n t f ("OK!! la valeur paire est %d sa position est %d\n" , val , pos ) ;24 }

M.LICHOURI TPINFO3 :Page 11/21

2014/02/06

Page 12: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

25 i++;26 }27

28 i f (num==0)29 p r i n t f ("pas d'element pair\n" ) ;30

31 getch ( ) ;32 }

2eme Methode :

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int V[ 5 ] , i , val , pos ,num=0;7

8

9 p r i n t f ("donner les valeurs du tableau V" ) ;10 for ( i =0; i <5; i++)11 {12 s can f ("%d" ,&V[ i ] ) ;13 }14

15

16 for ( i =0; i <5; i++)17 {18 i f (V[ i ]%2==0)19 {20 va l=V[ i ] ;21 pos=i +1;22 p r i n t f ("OK!! la valeur paire est %d sa position est %d\n" , val , pos ) ;23 num=1;24 i =5; /∗ou bien break ∗/25 }26 }27 i f (num==0)28 p r i n t f ("pas d'element pair\n" ) ;29

30 getch ( ) ;31 }

M.LICHOURI TPINFO3 :Page 12/21

2014/02/06

Page 13: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

11 Exercice 11

Ecrire un programme c qui lit et a�che une matrice ligne par ligne.

11.1 Solution Exercice 11

1

2 #include<s td i o . h>3 #include<conio . h>4 #include<math . h>5 void main ( )6 {7 int W[ 3 ] [ 3 ] , i , j ;8

9

10 p r i n t f ("donner les valeurs du matrice W" ) ;11 for ( i =0; i <3; i++)12 {13 for ( j =0; j <3; j++)14 {15 s can f ("%d" ,&W[ i ] [ j ] ) ;16 }17 }18

19 for ( i =0; i <3; i++)20 {21 for ( j =0; j <3; j++)22 {23 p r i n t f ("%d\t" ,W[ i ] [ j ] ) ;24 }25 p r i n t f ("\n" ) ;26 }27

28

29

30 getch ( ) ;31 }

M.LICHOURI TPINFO3 :Page 13/21

2014/02/06

Page 14: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

12 Exercice 12

Ecrire un programme c qui calcule le produit des elements d'une matrice.

12.1 Solution Exercice 12

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6

7 int W[ 3 ] [ 3 ] , i , j , p ;8

9

10 p r i n t f ("donner les valeurs du matrice W" ) ;11 for ( i =0; i <3; i++)12 {13 for ( j =0; j <3; j++)14 {15 s can f ("%d" ,&W[ i ] [ j ] ) ;16 }17 }18 p=1;19 for ( i =0; i <3; i++)20 {21 for ( j =0; j <3; j++)22 {23 p=p∗W[ i ] [ j ] ;24 }25

26 }27 p r i n t f ("le produit est egale a %d" , p ) ;28

29

30

31 getch ( ) ;32 }

M.LICHOURI TPINFO3 :Page 14/21

2014/02/06

Page 15: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

13 Exercice 13

Ecrire un programme c qui fait la somme de deux matrices triangulairesinferieures.

13.1 Solution Exercice 13

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int W[ 3 ] [ 3 ] ,V [ 3 ] [ 3 ] , Som [ 3 ] [ 3 ] , i , j ;7

8 // Lecture de l a matr ice W9 p r i n t f ("donner les valeurs du matrice W" ) ;

10 for ( i =0; i <3; i++)11 for ( j =0; j <3; j++)12 s can f ("%d" ,&W[ i ] [ j ] ) ;13

14 // Lecture de l a matr ice V15 p r i n t f ("donner les valeurs du matrice V" ) ;16 for ( i =0; i <3; i++)17 for ( j =0; j <3; j++)18 s can f ("%d" ,&V[ i ] [ j ] ) ;19

20 // Calcu l de l a somme21 for ( i =0; i <3; i++)22 for ( j =0; j <3; j++)23 Som[ i ] [ j ]=V[ i ] [ j ]+W[ i ] [ j ] ;24

25

26 // Af f i chage de l a matr ice r e s u l t a n t e27 for ( i =0; i <3; i++)28 {29 for ( j =0; j <3; j++)30 {31 p r i n t f ("%d\t" ,Som [ i ] [ j ] ) ;32 }33 p r i n t f ("\n" ) ;34 }35

36

37

38 getch ( ) ;39 }

M.LICHOURI TPINFO3 :Page 15/21

2014/02/06

Page 16: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

14 Exercice 14(a faire en devoir)

Ecrire un programme c qui calcule les maxima des colonnes d'une matriceet les range dans un vecteur vmax et les minima dans un vecteur vmin.

14.1 Solution Exercice 14

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 int main ( )5 {6 int W[ 3 ] [ 3 ] ,Vmax [ 3 ] , Vmin [ 3 ] , i , j ,max , min ;7

8 // Lecture de l a matr ice W9 p r i n t f ("donner les valeurs du matrice W" ) ;

10 for ( i =0; i <3; i++)11 for ( j =0; j <3; j++)12 s can f ("%d" ,&W[ i ] [ j ] ) ;13

14

15 // La recherche des va l eu r s maximum dans l e s co lonnes16 p r i n t f ("Recherche des valeurs maximale...\n" ) ;17 for ( j =0; j <3; j++)18 {19 max=W[ 0 ] [ j ] ;20 for ( i =0; i <3; i++)21 {22 i f (W[ i ] [ j ]>=max)23 max=W[ i ] [ j ] ;24 }25 Vmax[ j ]=max ;26 }27

28 // Af f i chage des deux vec t eu r s Vmax29 p r i n t f ("vecteur Vmax:\n" ) ;30 for ( i =0; i <3; i++)31 {32 p r i n t f ("%d\t" ,Vmax[ i ] ) ;33

34 }35

36 // La recherche des va l eu r s minimum dans l e s co lonnes37 p r i n t f ("\nRecherche des valeurs minimale...\n" ) ;38 for ( j =0; j <3; j++)39 {40 min=W[ 0 ] [ j ] ;41 for ( i =0; i <3; i++)42 {

M.LICHOURI TPINFO3 :Page 16/21

2014/02/06

Page 17: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

43 i f (W[ i ] [ j ]<=min)44 min=W[ i ] [ j ] ;45 }46 Vmin [ j ]=min ;47 }48 // Af f i chage des deux vec t eu r s Vmin49

50 p r i n t f ("vecteur Vmin:\n" ) ;51 for ( i =0; i <3; i++)52 {53 p r i n t f ("%d\t" ,Vmin [ i ] ) ;54

55 }56

57

58 getch ( ) ;59 }

M.LICHOURI TPINFO3 :Page 17/21

2014/02/06

Page 18: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

15 Exercice 15

Ecrire un programme c qui met a zero la diagonale principale d'une matrice.

15.1 Solution Exercice 15

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int W[ 3 ] [ 3 ] , i , j ;7

8 // Lecture de l a matr ice W9 p r i n t f ("donner les valeurs du matrice W" ) ;

10 for ( i =0; i <3; i++)11 for ( j =0; j <3; j++)12 s can f ("%d" ,&W[ i ] [ j ] ) ;13

14

15 // Mettre a zero l a d iagona l e d ' une matr ice16 for ( i =0; i <3; i++)17 W[ i ] [ i ]=0;18

19 // Af f i chage de l a matr ice r e s u l t a n t e20 p r i n t f ("Matrice trouve:\n" ) ;21 for ( i =0; i <3; i++)22 {23 for ( j =0; j <3; j++)24 {25 p r i n t f ("%d\t" ,W[ i ] [ j ] ) ;26 }27 p r i n t f ("\n" ) ;28 }29

30

31 getch ( ) ;32 }

M.LICHOURI TPINFO3 :Page 18/21

2014/02/06

Page 19: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

16 Exercice 16

Ecrire des programmes c qui permettent de :� calculer le produit d'une matrice par un vecteur� calculer le produit de deux matrices

16.1 Solution Exercice 16-1

1 #include<s td i o . h>2 #include<conio . h>3 #include<math . h>4 void main ( )5 {6 int W[ 3 ] [ 3 ] ,V[ 3 ] ,P [ 3 ] , S , i , j ;7

8 // Lecture de l a matr ice W9 p r i n t f ("donner les valeurs du matrice W:\n" ) ;

10 for ( i =0; i <3; i++)11 for ( j =0; j <3; j++)12 s can f ("%d" ,&W[ i ] [ j ] ) ;13

14

15 // l e c t u r e du vecteur V16 p r i n t f ("donner les valeurs du vecteur V:\n" ) ;17 for ( i =0; i <3; i++)18 s can f ("%d" ,&V[ i ] ) ;19

20 // Calcu l de produ i t P=W∗V21 p r i n t f ("Calcul du produit...\n" ) ;22 for ( i =0; i <3; i++)23 {24 S=0;25 for ( j =0; j <3; j++)26 {27 S=S+ W[ i ] [ j ]∗V[ j ] ;28 }29 P[ i ]=S ;30 }31

32 // 2eme methode33 // f o r ( i =0; i <3; i++)34 //{35 // P[ i ]=0;36 // f o r ( j =0; j <3; j++)37 // P[ i ]=P[ i ]+ W[ i ] [ j ]∗V[ j ] ;38 //}39

40 // Af f i chage du vecteur r e s u l t a n t41 // W(3∗3) ∗ V(3∗1)= P(3∗1)

M.LICHOURI TPINFO3 :Page 19/21

2014/02/06

Page 20: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

42 p r i n t f ("Le vecteur produit resultant est:\n" ) ;43 for ( i =0; i <3; i++)44 p r i n t f ("%d\t" ,P [ i ] ) ;45

46

47 getch ( ) ;48 }

16.2 Solution Exercice 16-2

1

2 #include<s td i o . h>3 #include<conio . h>4 #include<math . h>5 void main ( )6 {7 int W[ 3 ] [ 3 ] ,V [ 3 ] [ 3 ] , P [ 3 ] [ 3 ] , S , i , j , k ;8

9 // Lecture de l a matr ice W10 p r i n t f ("donner les valeurs du matrice W:\n" ) ;11 for ( i =0; i <3; i++)12 for ( j =0; j <3; j++)13 s can f ("%d" ,&W[ i ] [ j ] ) ;14

15

16 // Lecture de l a matr ice W17 p r i n t f ("donner les valeurs du matrice W:\n" ) ;18 for ( i =0; i <3; i++)19 for ( j =0; j <3; j++)20 s can f ("%d" ,&V[ i ] [ j ] ) ;21

22 // Calcu l de produ i t P=W∗V23 p r i n t f ("Calcul du produit...\n" ) ;24 for ( i =0; i <3; i++)25 {26 for ( j =0; j <3; j++)27 {28 S=0;29 for ( k=0;k<3;k++)30 {31 S=S+ W[ i ] [ k ]∗V[ k ] [ j ] ;32 }33 P[ i ] [ j ]=S ;34 }35 }36

37 // Af f i chage du vecteur r e s u l t a n t38 // W(3∗3) ∗ V(3∗3)= P(3∗3)

M.LICHOURI TPINFO3 :Page 20/21

2014/02/06

Page 21: Université de Blida - Sharing Knowledge and Experience. · 2014-02-06 · TPN1 :structures de contrôle et les tableaux en C Responsable A.SELLALI Assistant M.LICHOURI 02 Decembre

Université de BlidaFaculté des Sciences

2 année STSemestre 3

39 p r i n t f ("La matrice resultante est:\n" ) ;40 for ( i =0; i <3; i++)41 {42 for ( j =0; j <3; j++)43 {44 p r i n t f ("%d\t" ,P [ i ] [ j ] ) ;45 }46 p r i n t f ("\n" ) ;47 }48

49

50

51 getch ( ) ;52 }

M.LICHOURI TPINFO3 :Page 21/21

2014/02/06