/* Calculs matriciels */
#include
/* Saisie d'une matrice carree */
void saisir(double Mat[100][100], int nb);
/* Affichage d'une matrice carree */
void afficher(double Mat[100][100], int nb);
/* Addition de deux matrices */
void additionner(double Mat1[100][100], double Mat2[100][100],
double Mat3[100][100], int nb);
/* Multiplication de deux matrices */
void multiplier(double Mat1[100][100], double Mat2[100][100],
double Mat3[100][100], int nb);
/* Affichage d'un menu et retour du choix de l'utilisateur */
char menu();
int main ()
{
double M[100][100], P[100][100], R[100][100];
int nb_val;
char rep;
rep = menu();
while (rep == 'A' || rep == 'M')
{
printf ("taille de la matrice carree :\n");
scanf ("%d", &nb_val);
printf("saisie de la 1ere matrice\n");
saisir(M, nb_val);
printf("saisie de la 2eme matrice\n");
saisir(P, nb_val);
switch(rep)
{
case 'A':
additionner(M, P, R, nb_val);
break;
case 'M':
multiplier(M, P, R, nb_val);
break;
}
printf("Matrice M:\n"); afficher(M, nb_val);
printf("Matrice P:\n"); afficher(P, nb_val);
printf("Matrice R:\n"); afficher(R, nb_val);
rep = menu();
}
return 0;
}
char menu(void)
{
char rep;
printf("A...........Addition\n");
printf("M...........Multiplication\n");
printf("(autres)....Quitter\n");
printf("Choix : ");
scanf(" %c", &rep);
if (rep > 'Z')
{
rep = rep - 'a' + 'A';
}
return rep;
}
void saisir(double Mat[100][100], int nb)
{
int i, j;
printf("entrer les valeurs de la matrice\n");
for (i=0; i<nb; i++)
{
for (j=0; j<nb; j++)
{
printf ("M[%d][%d] = ", i+1, j+1);
scanf ("%lf", &(Mat[i][j]));
}
}
}
void afficher(double Mat[100][100], int nb)
{
int i, j;
printf("\t");
for (i=0; i<nb; i++)
{
printf("%d\t\t", i+1);
}
printf("\n");
for (i=0; i<nb; i++)
{
printf("----------------");
}
printf("\n");
for (i=0; i<nb; i++)
{
printf("%d:", i+1);
for (j=0; j<nb; j++)
{
printf ("\t%f", Mat[i][j]);
}
printf("\n");
}
for (i=0; i<nb; i++)
{
printf("----------------");
}
printf("\n\n");
}
void additionner(double Mat1[100][100], double Mat2[100][100],
double Mat3[100][100], int nb)
{
int i, j;
for (i=0; i<nb; i++)
{
for (j=0; j<nb; j++)
{
Mat3[i][j] = Mat1[i][j] + Mat2[i][j];
}
}
}
void multiplier(double Mat1[100][100], double Mat2[100][100],
double Mat3[100][100], int nb)
{
int i, j, k;
for (i=0; i<nb; i++)
{
for (j=0; j<nb; j++)
{
Mat3[i][j] = 0;
for (k=0; k<nb; k++)
{
Mat3[i][j] = Mat3[i][j] + Mat1[i][k]*Mat2[k][j];
}
}
}
}