Repository navigation
Expand file tree
/
Copy pathspiralInput.c
More file actions
59 lines (56 loc) · 1.64 KB
/
Copy pathspiralInput.c
File metadata and controls
59 lines (56 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Spiral Order Matrix Input in C
This program reads elements of an n x n matrix from the user **in spiral order** (starting from the top-left, moving right, down, left, and up in layers).
After all elements are entered in spiral order, the program prints the matrix in standard row-wise format.
Example:
For n = 3 and input: 1 2 3 4 5 6 7 8 9 (entered in spiral order)
The output will be:
-> -> ->
1 2 3 |
8 9 4 |
7 6 5 |
<- <- <-
This is useful for understanding spiral traversal and for problems where spiral input is required.
Author: [Soham Bhattacharya]
Date: [23.06.2025]
*/
#include<stdio.h>
int main(){
int n;
printf("Enter the row/column of the matrix: ");
scanf("%d", &n);
int arr[n][n];
int tne = n*n;
int count = 0;
int minr=0 , minc=0, maxr=n-1, maxc=n-1;
printf("Enter your elements: ");
while(count<tne){
for(int j=minc;j<=maxc && count<tne;j++){
scanf("%d", &arr[minr][j]);
count++;
}
minr++;
for(int i=minr;i<=maxr && count<tne;i++){
scanf("%d", &arr[i][maxc]);
count++;
}
maxc--;
for(int j=maxc;j>=minc && count<tne;j--){
scanf("%d", &arr[maxr][j]);
count++;
}
maxr--;
for(int i=maxr;i>=minr && count<tne;i--){
scanf("%d", &arr[i][minc]);
count++;
}
minc++;
}
for(int i=0;i<n;i++){
for(int j=0; j<n;j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}