From ba562614b28fbb68f58b7284804af8ce9f507cc8 Mon Sep 17 00:00:00 2001 From: SWAPNANIL RAY Date: Sat, 3 Oct 2020 21:16:32 +0530 Subject: [PATCH] Added Spiral Matrix Algo --- algorithms/spiralmatrix.c | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 algorithms/spiralmatrix.c diff --git a/algorithms/spiralmatrix.c b/algorithms/spiralmatrix.c new file mode 100644 index 00000000..8fa38a87 --- /dev/null +++ b/algorithms/spiralmatrix.c @@ -0,0 +1,64 @@ +#include +#define MAX 100 +void vibecheck(int arr[MAX][MAX], int m, int n, int k) +{ + int x = 0, y = 0, ctr = 0; + int i = 0; + while (x < m && y < n) + { + for (i = y; i < n; i++) + { + ctr++; + + if (ctr == k) + printf("%d ", arr[x][i]); + } + x++; + + for (i = x; i < m; ++i) { + ctr++; + + if (ctr == k) + printf("%d ", arr[i][n - 1]); + } + n--; + + if (x < m) { + for (i = n - 1; i >= y; --i) { + ctr++; + + if (ctr == k) + printf("%d ", arr[m - 1][i]); + } + m--; + } + + if (y < n) { + for (i = m - 1; i >= x; --i) { + ctr++; + + if (ctr == k) + printf("%d ", arr[i][y]); + } + y++; + } + + } +} +void main() +{ + int arr[MAX][MAX], m = 0, n = 0, k = 0; + printf("Enter the number of rows and columns space separated:"); + scanf_s("%d %d", &m, &n); + printf("\nEnter the elements of the of the matrix (2D)(%dx%d):", m, n); + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + scanf_s("%d", &arr[i][j]); + } + } + printf("\nEnter the position value:"); + scanf_s("%d", &k); + vibecheck(arr, m, n, k); +} \ No newline at end of file