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. void transpose_squared(int **a, int rows);
  29. void transpose_non_squared(int **a, int **b, int rows, int cols);
  30.  
  31. void main(int argc, char* argv[])
  32. {
  33.   int i, j;
  34.   int rows, cols;
  35.   int **A, **B;
  36.  
  37.   if(argc != 3){
  38.     fprintf(stderr, "Usage: ./ex1-5 [rows]\\n");
  39.     exit(-1);
  40.   }
  41.  
  42.   rows = atoi(argv[1]);
  43.   cols = atoi(argv[2]);
  44.  
  45.   A = make2dArray(rows, cols);
  46.   B = make2dArray(cols, rows);
  47.  
  48.   srand((unsigned)time(NULL));
  49.   printf("Before transpose\\n");
  50.   for(i = 0; i < rows; ++i){
  51.     printf("[ ");
  52.     for(j = 0; j < cols; ++j){
  53.       A[i][j] =rand() % 10;
  54.       printf("%3d ", A[i][j]);
  55.     }
  56.     printf("]\\n");
  57.   }
  58.  
  59.   printf("\\nAfter transpose\\n");
  60.   transpose_non_squared(A, B, rows, cols);
  61.   for(i = 0; i < cols; ++i){
  62.     printf("[ ");
  63.     for(j = 0; j < rows; ++j){
  64.       printf("%3d ", B[i][j]);
  65.     }
  66.     printf("]\\n");
  67.   }
  68. }
  69.  
  70. int** make2dArray(int rows, int cols)
  71. {
  72.   int **x, i;
  73.  
  74.   CALLOC(x, rows, rows * sizeof(*x));
  75.  
  76.   for(i = 0; i < rows; ++i)
  77.     CALLOC(x[i], cols, cols * sizeof(**x));
  78.   return x;
  79. }
  80.  
  81. void add(int **a, int **b, int **c, int rows, int cols)
  82. {
  83.   int i, j;
  84.  
  85.   for(i = 0; i < rows; ++i){
  86.     for(j = 0; j < cols; ++j){
  87.       c[i][j] = a[i][j] + b[i][j];
  88.     }
  89.   }
  90. }
  91.  
  92. void mult(int **a, int **b, int **c, int rows)
  93. {
  94.   int i, j, k;
  95.   for(i = 0; i < rows; i++){
  96.     for(j = 0; j < rows; j++){
  97.       for(k = 0; k < rows; k++){
  98.         c[i][j] += a[i][k] * b[k][j];
  99.       }
  100.     }
  101.   }
  102. }
  103.  
  104. void transpose_squared(int **a, int rows)
  105. {
  106.   int i, j, tmp;
  107.   for(i = 0; i < rows - 1; i++){
  108.     for(j = i + 1; j < rows; j++){
  109.       SWAP(a[i][j], a[j][i],tmp);
  110.     }
  111.   }
  112. }
  113.  
  114. void transpose_non_squared(int **a, int **b, int rows, int cols)
  115. {
  116.   int i,j, tmp;
  117.   for(i = 0; i < cols; i++){
  118.     for(j = 0; j < rows; j++){
  119.       b[i][j] = a[j][i];
  120.     }
  121.   }
  122. }
');