bon j ai fai une amélioration de mon programme . je galere un peu avec le C mai bon c est un projet , soit disan la base de la methode num.(je croi qu on va en chier )
si vous pouviez m aider , il doi y avoir des erreurs (peut etre dans les indices des boucles for meme si j ai verifié et revérifié) merci d avance
si vous pouviez aussi m expliquer la différence si il y en a une entre M.type et M->type par exemple
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int dim1;
int dim2;
int type;
double **tab;
}matrice;
void creation (matrice *M)
{
int i,j;
printf("creation d'une matrice!!!!\n");
printf("entrer la 1e dimension=");
scanf("%d",&M->dim1);
printf("entrer la 2e dimension=");
scanf("%d",&M->dim2);
printf("entrer le type de la matrice=\n");
printf("1-matrice pleine\n");
printf("2-matrice triangulaire inférieure\n");
printf("3-matrice triangulaire supérieure\n");
scanf("%d",&M->type);
if(M->type==1){
M->tab=(double **)malloc(M->dim1*sizeof(double *));
for(i=0;i<M->dim1;i++)
M->tab[i]=(double *)malloc(M->dim2*sizeof(double));
printf("chiffre de la matrice??");
for(i=0;i<M->dim2;i++){
printf("%d e ligne\n",i);
for(j=0;j<M->dim1;j++)scanf("%lf",&M->tab[i][j]);
}
}
if(M->type==2){ /*matrice de type triangulaire inf. */
M->tab=(double **)malloc(M->dim1*sizeof(double *));
for(i=0;i<M->dim1;i++)
M->tab[i]=(double *)malloc(M->dim2*sizeof(double));
/*on detruit la partie supérieure pour liberer de la place */
for(i=0;i<(M->dim2)-1;i++){
for(j=i+1;j<M->dim1;j++)free(M->tab[j]);
}
/*on rempli la partie inferieure */
printf("chiffres de la matrice");
for(i=0;i<M->dim2;i++){
printf("%d e ligne\n",i);
for(j=0;j<=i;j++)scanf("%lf",&M->tab[i][j]);
}
}
if(M->type==3){ /*matrice de type triangulaire sup. */
M->tab=(double **)malloc(M->dim1*sizeof(double *));
for(i=0;i<M->dim1;i++)
M->tab[i]=(double *)malloc(M->dim2*sizeof(double));
for(i=1;i<M->dim2;i++){
for(j=0;j<i;j++)free(M->tab[j]);
}
printf("chiffres de la matrice");
for(i=0;i<M->dim2;i++){
printf("%d e ligne\n",i);
for(j=i;j<M->dim1;j++)scanf("%lf",&M->tab[i][j]);
}
}
}
void affichage(matrice M)
{
int i,j;
if(M.type==1){
for(i=0;i<M.dim2;i++){
for(j=0;j<M.dim1;j++){printf("%lf",M.tab[i][j]);}
printf("\n");
}
}
if(M.type==2){
for(i=0;i<M.dim2;i++){
for(j=0;j<=i;j++)printf("%lf",M.tab[i][j]);
printf("\n");
}
}
if(M.type==3){
for(i=0;i<M.dim2;i++){
for(j=i;j<M.dim1;j++)printf("%lf",M.tab[i][j]);
printf("\n");
}
}
}
void produit(matrice M,matrice N)
{
int i,j,k;
if(M.type==1&&N.type==2){ /* on cree la matrice produit, les 2 matrices ont les meme dimensions */
matrice O;
O.type=M.type;
O.dim1=M.dim1;
O.dim2=M.dim2;
O.tab=(double **)malloc(M.dim1*sizeof(double *));
for(i=0;i<M.dim1;i++)
O.tab[i]=(double *)malloc(M.dim2*sizeof(double));
/*on fait le produit des 2 matrices */
O.tab[0][0]=0;
for(i=0;i<M.dim2;i++)
for(j=0;O.tab[i][j]=0,j<N.dim1;j++)
for(k=0;k<M.dim1;k++)
O.tab[i][j]=M.tab[i][k]*N.tab[k][j];
}
if(M.type==1&&N.type==2){ /* on copie la matrice triangulaire dan une autre matrice
en y ajoutant les zéros ,tout ça pour effectuer le produit */
matrice P;
P.type=M.type;
P.dim1=N.dim1;
P.dim2=N.dim2;
P.tab=(double **)malloc(P.dim1*sizeof(double *));
for(i=0;i<P.dim1;i++)
P.tab[i]=(double *)malloc(P.dim2*sizeof(double));
for(i=0;i<N.dim2;i++){
for(j=0;j<=i;j++)P.tab[j]=N.tab[j];
}
for(i=0;i<(M.dim2)-1;i++){
for(j=i+1;j<M.dim1;j++)P.tab[j]=0;
}
matrice O;
O.type=M.type;
O.dim1=M.dim1;
O.dim2=M.dim2;
O.tab=(double **)malloc(M.dim1*sizeof(double *));
for(i=0;i<M.dim1;i++)
O.tab[i]=(double *)malloc(M.dim2*sizeof(double));
O.tab[0][0]=0;
for(i=0;i<M.dim2;i++)
for(j=0;O.tab[i][j]=0,j<N.dim1;j++)
for(k=0;k<M.dim1;k++)
O.tab[i][j]=M.tab[i][k]*P.tab[k][j];
}
if(M.type==1&&N.type==3){
matrice P;
P.type=M.type;
P.dim1=N.dim1;
P.dim2=N.dim2;
P.tab=(double **)malloc(P.dim1*sizeof(double *));
for(i=0;i<P.dim1;i++)
P.tab[i]=(double *)malloc(P.dim2*sizeof(double));
for(i=0;i<M.dim2;i++){
for(j=i;j<M.dim1;j++)P.tab[j]=N.tab[j];
}
for(i=1;i<M.dim2;i++){
for(j=0;j<i;j++)P.tab[j]=0;
}
matrice O;
O.type=M.type;
O.dim1=M.dim1;
O.dim2=M.dim2;
O.tab=(double **)malloc(M.dim1*sizeof(double *));
for(i=0;i<M.dim1;i++)
O.tab[i]=(double *)malloc(M.dim2*sizeof(double));
O.tab[0][0]=0;
for(i=0;i<M.dim2;i++)
for(j=0;O.tab[i][j]=0,j<N.dim1;j++)
for(k=0;k<M.dim1;k++)
O.tab[i][j]=M.tab[i][k]*P.tab[k][j];
}
}
int main()
{
matrice M1,M2,O;
creation(&M1);
affichage(M1);
creation(&M2);
produit(M1,M2);
affichage(O);
system("pause");
return 0;
}