1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/*
*
* Le but de cet exercice est de creer une structure et les
* fonctions sur une matrice 2x2.
* Les fonctions seront en particulier :
* - Une fonction somme de deux matrices
* - Une fonction produit de deux matrices
* - Une fonction determinant de la matrice
* - Une procedure d'affichage
*
* Les fonctions devront utiliser le passage par adresse dans tous les
* cas.
*/
#include <stdio.h>
#include <stdlib.h>
/* definition de la structure matruce 2x2*/
struct matrix {
double a,b,c,d ;
} ;
typedef struct matrix matrix ;
/* prototype d'initialisationd de matrice */
void matrix_init(matrix * , double , double, double, double) ;
/* prototype de la somme de deux matrices */
void matrix_sum( matrix , matrix , matrix * ) ;
/* prototype du produit de deux matrices */
void matrix_prod( matrix , matrix , matrix * ) ;
/* prototype du determinant d'une matrice */
double matrix_det( matrix ) ;
/* prototype de la procedure d'affichage d'une matrice */
void matrix_disp( matrix ) ;
int main(void)
{
/* declaration des variables */
matrix M1 ;
matrix M2 ;
matrix M3 ;
double determinant ;
/* initialisation des deux matrices */
matrix_init(&M1, 1, 2, 3, 4 ) ;
matrix_init(&M2, 1, 2, 3, 4 ) ;
/* affichage des deux matrices */
printf("matrice M1\n") ; matrix_disp(M1) ;
printf("matrice M2\n") ; matrix_disp(M2) ;
/* addition des deux matrices */
matrix_sum( M1 , M2 , &M3 ) ;
printf("somme des deux matrices \n") ; matrix_disp( M3 ) ;
/* produit des deux matrices */
matrix_prod( M1 , M2 , &M3 ) ;
printf("produit des deux matrices \n") ; matrix_disp( M3 ) ;
/* determinant de la matrice */
determinant = matrix_det( M1 ) ;
printf("le determinant de M1 est %lf\n", determinant ) ;
/* fin du programme */
return 0 ;
}
void matrix_init( matrix * M1, double a, double b, double c, double d)
{
M1->a = a ;
M1->b = b ;
M1->c = c ;
M1->d = d ;
}
void matrix_sum(matrix M1 , matrix M2 , matrix * M3)
{
M3->a = (M1.a) + (M2.a) ;
M3->b = (M1.b) + (M2.b) ;
M3->c = (M1.c) + (M2.c) ;
M3->d = (M1.d) + (M2.d) ;
}
void matrix_prod(matrix M1 , matrix M2 , matrix * M3)
{
M3->a = (M1.a)*(M2.a) + (M1.b)*(M2.c) ;
M3->b = (M1.a)*(M2.b) + (M1.b)*(M2.d) ;
M3->c = (M1.c)*(M2.a) + (M1.d)*(M2.c) ;
M3->d = (M1.c)*(M2.b) + (M1.d)*(M2.d) ;
}
double matrix_det(matrix M1 )
{
double determinant ;
determinant = (M1.a)*(M1.d) - (M1.b)*(M1.c) ;
return determinant ;
}
void matrix_disp(matrix M1)
{
printf("\n ( %.1lf %.1lf ) \n ( %.1lf %.1lf ) \n\n",
(M1.a) , (M1.b) , (M1.c) , (M1.d) );
} |
Partager