ijijij

Clockwise spiral builder Simplified

Sep 19th, 2020 (edited)
1,797
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.   int n = 2;
  5.   int m = 3;
  6.   int arr[10][10] = {};
  7.  
  8.   int i, s = 1, x = 0, y = 0, w = m-1, h = n-1;
  9.   while(w >= 0 && h >= 0) {
  10.     i = w; while(i--) arr[y][x++] = s++;
  11.     i = h; while(i--) arr[y++][x] = s++;
  12.     i = w; while(h && i--) arr[y][x--] = s++;
  13.     i = h; while(w && i--) arr[y--][x] = s++;
  14.     y++; x++;
  15.     w -= 2; h -= 2;
  16.   }
  17.   if(s == m*n) arr[--y][--x] = s;
  18.  
  19.   for(int i = 0; i < n; i++) {
  20.     for(int j = 0; j < m; j++) {
  21.       printf("%4d", arr[i][j]);
  22.     }
  23.     printf("\n");
  24.   }
  25.  
  26.   return 0;
  27. }
  28.  
RAW Paste Data