Advertisement
Guest User

matrice.c

a guest
Jun 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. /* Putain de matrice des mes couilles */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7.  
  8. #define NMAX 100
  9.  
  10. typedef struct {
  11.     int M[NMAX][NMAX];
  12.     int N;
  13. } TMatrice;
  14.  
  15.  
  16. void afficherMatrice( TMatrice const m )
  17. {
  18.     int i, j;
  19.  
  20.     for( i = 0; i < m.N; ++i ) {
  21.         for( j = 0; j < m.N; ++j )
  22.             printf("%2d ", m.M[i][j]);
  23.  
  24.         printf("\n");
  25.     }
  26. }
  27.  
  28. void mulMat(
  29.     TMatrice const m1 ,
  30.     TMatrice const m2 ,
  31.     TMatrice * m3 )
  32. {
  33.     int i, j, k, s;
  34.  
  35.     if ( m3 == NULL || m1.N != m2.N )
  36.         return;
  37.  
  38.     for( i = 0; i < m1.N; ++i ) {
  39.         for( j = 0; j < m1.N; ++j ) {
  40.             for( s = 0, k = 0; k < m1.N; ++k )
  41.                 s += ( m1.M[i][k] * m2.M[k][j] );
  42.  
  43.             m3->M[i][j] = s;
  44.         }
  45.     }
  46.  
  47.     m3->N = m1.N;
  48. }
  49.  
  50. int main()
  51. {
  52.     TMatrice m3;
  53.     TMatrice m1 = {
  54.         { { 1, 2, 1 },
  55.           { 3, 2, 2 },
  56.           { 0, 5, 1 } },
  57.         3 // 3*3
  58.     };
  59.     TMatrice m2 = {
  60.         { { 1, 3, 1 },
  61.           { 2, 2, 0 },
  62.           { 3, 1, 1 } },
  63.         3 // 3*3
  64.     };
  65.  
  66.     printf("M1:\n");
  67.     afficherMatrice(m1);
  68.     printf("M2:\n");
  69.     afficherMatrice(m2);
  70.  
  71.     printf("\nM3 = M1 * M2:\n");
  72.     mulMat(m1, m2, &m3);
  73.     afficherMatrice(m3);
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement