KAR98S

squareMatrix_to_spiral_sequence(MoreReadable).c

Jan 14th, 2021 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include<stdio.h>
  2. int main() {
  3.     int i, j, array[99][99], n, tmp, directionI = 1, directionJ = 1, flip = 0, length;
  4.  
  5.     printf("Enter n of a square matrix: ");
  6.     scanf("%d", &n);
  7.     tmp = length = n;
  8.  
  9.     printf("\n");
  10.     for (i = 0; i < n; i++)
  11.         for (j = 0; j < n; j++)
  12.             array[i][j] = i * n + j + 1;
  13.  
  14.     for (i = 0; i < n; i++) {
  15.         for (j = 0; j < n; j++)
  16.             printf("%d\t", array[i][j]);
  17.         printf("\n");
  18.     }
  19.  
  20.     printf("\nsequence: ");
  21.     for (i = j = 0; length; --tmp ? 0 : (directionI *= 1 - 2 * flip, directionJ *= 2 * flip - 1, tmp = length -= flip = !flip), i += directionI * flip, j += directionJ * !flip)
  22.         printf("%d,", array[i][j]);
  23.     return 0;
  24. }
Add Comment
Please, Sign In to add comment