Académique Documents
Professionnel Documents
Culture Documents
TP N°: 4
Solution de TP N°:4
Exercice 1 :
#include<stdio.h>
#include<stdlib.h>
int** allouer(int L,int C)
{
int i;
int**A;
A=(int**)malloc(L*sizeof(int*));
if(A==NULL) {printf("Erreur\n");exit(0);}
for(i=0;i<L;i++)
{
A[i]=(int*)malloc(C*sizeof(int));
if(A[i]==NULL) {printf("Erreur\n");exit(0);}
}
return A;
}
void liberation(int **A,int L,int C)
{
int i;
for(i=0;i<L;i++)
free(A[i]);
free(A);
printf("Libération effctuée\n");
}
void lecture(int **A,int L,int C)
{
int i,j;
for(i=0;i<L;i++)
{
for(j=0;j<C;j++)
{
printf("Entrer élément (%d,%d): \n",i,j);
//scanf("%d",(A[i]+j));
scanf("%d",(*(A+i)+j));
2019/2020
Université Sultan Moulay Slimane Département d’Informatique
FST – Béni Mellal Module : Alg & prog 2
Filière : MIPC_GE_GM (S3)
}
}
}
void affichage(int **A,int L,int C)
{
int i,j;
for(i=0;i<L;i++)
{
for(j=0;j<C;j++)
{
//printf(" %d ",*(A[i]+j));
printf(" %d ",*(*(A+i)+j));
}
printf("\n");
}}
int produit(int **A,int **B,int L,int LC,int C)
{
int i,j,k,p;
int **S;
S=allouer(L,C);
if(S==NULL)
{
printf("Erreur de réservation\n");
exit(0);
}
for(i=0;i<L;i++)
{
for(j=0;j<C;j++)
{
p=0;
for(k=0;k<LC;k++)
{
p=p+A[i][k]*B[k][j];
}
S[i][j]=p;
} }
2019/2020
Université Sultan Moulay Slimane Département d’Informatique
FST – Béni Mellal Module : Alg & prog 2
Filière : MIPC_GE_GM (S3)
return(S);}
main()
{
int **M1,**M2,**M3;
int Lig=3,LC=2,Col=3;
M1=allouer(Lig,Col);
M2=allouer(Lig,Col);
M3=allouer(Lig,Col);
if(M1==NULL || M2==NULL || M3==NULL)
{
printf("Erreur de réervation\n");
exit(0);
}
printf("---Matrice M1---\n");
lecture(M1,Lig,LC);
affichage(M1,Lig,LC);
printf("---Matrice M2---\n");
lecture(M2,LC,Col);
affichage(M2,LC,Col);
printf("---Matrice M3---\n");
M3=produit(M1,M2,Lig,LC,Col);
affichage(M3,Lig,Col);
liberation(M1,Lig,Col);
liberation(M2,Lig,Col);
liberation(M3,Lig,Col);
system("pause");}
Exercice 2 :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 30
void lecture(char chaine[max])
{
2019/2020
Université Sultan Moulay Slimane Département d’Informatique
FST – Béni Mellal Module : Alg & prog 2
Filière : MIPC_GE_GM (S3)
2019/2020