home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / alb_c10 / chap_09 / ch09_12.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-08  |  2.3 KB  |  104 lines

  1. /*********************************************************************
  2. *  CH09_12.C                              Addition de deux matrices  *
  3. *                  En utilisant des tableaux de pointeurs *        
  4. *********************************************************************/
  5.  
  6. #include<stdio.h>
  7. #include<alloc.h>
  8.  
  9. #define max_lignes 2  /* Le nombre maximum de lignes doit Ωtre connu.*/
  10.  
  11. void LitInt( int*);
  12. void Initialise( int *t[], int, int);
  13. void Affiche( int *t[], int, int);
  14. void Addition( int *t0[], int *t1[], int *t2[], int, int);
  15.  
  16. main( void)
  17. {
  18.     int m= max_lignes, n= 3, i, /* m, n: dimensions des matrices */
  19.     *tab0[max_lignes], *tab1[max_lignes], *tab2[max_lignes];
  20.  
  21.         for( i= 0; i< m; i++)       /* allocation dynamique          */ 
  22.           {
  23.          tab0[i]= ( int*) malloc( n* sizeof(int));
  24.          tab1[i]= ( int*) malloc( n* sizeof(int));
  25.          tab2[i]= ( int*) malloc( n* sizeof(int));
  26.           }
  27.  
  28.     Initialise( tab1, m, n);
  29.     Initialise( tab2, m, n);
  30.  
  31.     Addition( tab0, tab1, tab2, m, n);
  32.  
  33.     Affiche( tab0, m, n);
  34.  
  35.     for( i= 0; i< m; i++)        /* libΘration de la mΘmoire    */
  36.           {
  37.          free( tab0[i]);
  38.          free( tab1[i]);
  39.          free( tab2[i]);
  40.       }
  41. }
  42.  
  43. void Addition( int *t0[max_lignes],
  44.            int *t1[max_lignes], int *t2[max_lignes], int p, int q)
  45. {
  46.       int i, j;
  47.  
  48.       for( i= 0; i< p; i++)
  49.       {
  50.           for( j= 0; j< q; j++)
  51.       {
  52.          *( t0[i]+ j)=  *( t1[i]+ j) + *( t2[i]+ j);
  53.       }
  54.       }
  55. }
  56.  
  57. void Initialise( int *t[max_lignes], int p, int q)
  58. {
  59.       int i, j;
  60.  
  61.       printf("\n Initialisation d'une matrice [%d][%d]\n", p, q);
  62.  
  63.       for( i= 0; i< p; i++)
  64.       {
  65.           for( j= 0; j< q; j++)
  66.       {
  67.          printf(" tab[%d][%d]=  ", i, j);
  68.          LitInt( t[i]+ j);
  69.       }
  70.       }
  71. }
  72.  
  73. void Affiche( int *t[max_lignes], int p, int q)
  74. {
  75.       int i, j;
  76.  
  77.       for( i= 0; i< p; i++)
  78.       {
  79.       for( j= 0; j< q; j++)
  80.       {
  81.          printf("\n tab[%d][%d]=  %d", i, j, *(t[i]+ j));
  82.       }
  83.       }
  84. }
  85.  
  86. void LitInt( int *entier)
  87. {
  88.     double dble;
  89.     do
  90.         {
  91.        while( scanf("%lf", &dble)!= 1)
  92.         while( getchar() != '\n');
  93.        while( getchar() != '\n');
  94.     }
  95.     while( dble< -32768.0 || dble> 32767.0);
  96.         *entier= (int) dble;
  97. }
  98.  
  99. /*
  100. Nous obtenons le mΩme affichage que celui du programme CH09_11.C
  101.  
  102. */
  103.  
  104.