document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #define SWAP(x, y, t)   ((t) = (x), (x) = (y), (y) = (t))
  7.  
  8. #define MALLOC(ptr, size)  \\
  9.   if(!((ptr) = malloc(size))) { \\
  10.     fprintf(stderr, "Insufficient memory"); \\
  11.     exit(EXIT_FAILURE);\\
  12.   }
  13.  
  14. #define CALLOC(ptr, n, size) \\
  15.   if(!((ptr) = calloc(n, size))){\\
  16.     fprintf(stderr, "Insufficient memory"); \\
  17.     exit(EXIT_FAILURE);\\
  18.   }
  19.  
  20. #define REALLOC(ptr, size) \\
  21.   if(!((ptr) = realloc(ptr, size))){\\
  22.     fprintf(stderr, "Insufficient memory"); \\
  23.   }
  24.  
  25. int** make2dArray(int rows, int cols);
  26. void add(int **a, int **b, int **c, int rows, int cols);
  27. void mult(int **a, int **b, int **c, int rows);
  28.  
  29. void main(int argc, char* argv[])
  30. {
  31.   int i, j;
  32.   int n;
  33.   int **A, **B, **C;
  34.  
  35.   n = atoi(argv[1]);
  36.  
  37.   A = make2dArray(n, n);
  38.   B = make2dArray(n, n);
  39.   C = make2dArray(n, n);
  40.  
  41.   srand((unsigned)time(NULL));
  42.   for(i = 0; i < n; ++i){
  43.     for(j = 0; j < n; ++j){
  44.       A[i][j] =rand() % 10;
  45.       B[i][j] =rand() % 10;
  46.     }
  47.   }
  48.  
  49.   mult(A, B, C, n);
  50.  
  51.   for(i = 0; i < n; ++i){
  52.     printf("[ ");
  53.     for(j = 0; j < n; ++j){
  54.       printf("%3d ", A[i][j]);
  55.     }
  56.     printf("] ");
  57.  
  58.     if(i == ((int)n/2)) printf("   +   ");
  59.     else printf("       ");
  60.  
  61.     printf("[ ");
  62.     for(j = 0; j < n; ++j){
  63.       printf("%3d ", B[i][j]);
  64.     }
  65.     printf("]");
  66.  
  67.     if(i == ((int)n/2)) printf("   =   ");
  68.     else printf("       ");
  69.  
  70.     printf("[ ");
  71.     for(j = 0; j < n; ++j){
  72.       printf("%3d ", C[i][j]);
  73.     }
  74.     printf("]\\n");
  75.   }
  76. }
  77.  
  78. int** make2dArray(int rows, int cols)
  79. {
  80.   int **x, i;
  81.  
  82.   CALLOC(x, rows, rows * sizeof(*x));
  83.  
  84.   for(i = 0; i < rows; ++i)
  85.     CALLOC(x[i], cols, cols * sizeof(**x));
  86.   return x;
  87. }
  88.  
  89. void add(int **a, int **b, int **c, int rows, int cols)
  90. {
  91.   int i, j;
  92.  
  93.   for(i = 0; i < rows; ++i){
  94.     for(j = 0; j < cols; ++j){
  95.       c[i][j] = a[i][j] + b[i][j];
  96.     }
  97.   }
  98. }
  99.  
  100. void mult(int **a, int **b, int **c, int rows)
  101. {
  102.   int i, j, k;
  103.   for(i = 0; i < rows; i++){
  104.     for(j = 0; j < rows; j++){
  105.       for(k = 0; k < rows; k++){
  106.         c[i][j] += a[i][k] * b[k][j];
  107.       }
  108.     }
  109.   }
  110. }
');