Advertisement
LosPollos

Home task 2-11

Mar 4th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4.  
  5. void genMatrix(unsigned **matrix, unsigned width, unsigned  height)
  6. {
  7.     unsigned blankW = width;
  8.     unsigned blankH = height - 1;
  9.     unsigned w = 0u;
  10.     unsigned h = 0u;
  11.     unsigned count = 0u;
  12.  
  13.     char route = 'r';   // r - right, d - down, l - left, u - up.
  14.  
  15.     while (blankW + blankH)
  16.     {
  17.         // Go to the right.
  18.         for (unsigned i = 0; i < width - (width - blankW); ++i)
  19.         {
  20.             matrix[h][w] = ++count;
  21.             ++w;
  22.         }
  23.         --w;
  24.         --blankW;
  25.         if (!blankH && !blankW) break;
  26.  
  27.         // Go to the down.
  28.         ++h;
  29.         for (unsigned i = 0; i < height - (height - blankH); ++i)
  30.         {
  31.             matrix[h][w] = ++count;
  32.             ++h;
  33.         }
  34.         --h;
  35.         --blankH;
  36.         if (!blankH && !blankW) break;
  37.  
  38.         // Go to the left.
  39.         --w;
  40.         for (unsigned i = 0; i < width - (width - blankW); ++i)
  41.         {
  42.             matrix[h][w] = ++count;
  43.             --w;
  44.         }
  45.         ++w;
  46.         --blankW;
  47.         if (!blankH && !blankW) break;
  48.  
  49.         // Go to the up.
  50.         --h;
  51.         for (unsigned i = 0; i < height - (height - blankH); ++i)
  52.         {
  53.             matrix[h][w] = ++count;
  54.             --h;
  55.         }
  56.         --blankH;
  57.         if (!blankH && !blankW) break;
  58.         ++h;
  59.         ++w;
  60.     }
  61. }
  62.  
  63.  
  64.  
  65. void printMatrix(unsigned **matrix, unsigned width, unsigned height)
  66. {
  67.     for (unsigned i = 0; i < height; ++i)
  68.     {
  69.         for (unsigned j = 0; j < width; ++j)
  70.         {
  71.             printf("%4d", matrix[i][j]);
  72.         }
  73.         puts("");
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. int main()
  80. {
  81.     unsigned width, height;
  82.  
  83.     // Get user data.
  84.     printf("Input matrix width: ");
  85.     scanf_s("%u", &width);
  86.     printf("Input matrix height: ");
  87.     scanf_s("%u", &height);
  88.  
  89.     // Allocate memory.
  90.     unsigned **matrix = (unsigned **)malloc(height * sizeof(unsigned *));
  91.     for (unsigned i = 0; i < height; ++i)
  92.     {
  93.         matrix[i] = (unsigned *)malloc(width * sizeof(unsigned));
  94.     }
  95.  
  96.     // Generate a matrix.
  97.     genMatrix(matrix, width, height);
  98.  
  99.     // Draw a snail:)
  100.     printMatrix(matrix, width, height);
  101.  
  102.  
  103.     // Free
  104.     for (unsigned i = 0; i < height; ++i)
  105.     {
  106.         free(matrix[i]);
  107.     }
  108.     free(matrix);
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement