forked from NiharRanjan53/HacktoberFest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2d_Matrix_Spiral_form.c
38 lines (38 loc) · 1.42 KB
/
2d_Matrix_Spiral_form.c
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
#include <iostream>
using namespace std;
int main()
{
int row_end,col_end,a[200][200],row_start=0,col_start=0;
cout<<"Enter the number of rows:";
cin>>row_end;
cout<<"Enter the number of columns:";
cin>>col_end;
cout<<"Enter the array\n";
for(int i=0;i<row_end;i++)
{
for(int j=0;j<col_end;j++)
cin>>a[i][j];
}
cout<<"Spiral form\n";
while(row_start<row_end&&col_start<col_end)
{
for(int j=col_start;j<col_end;j++)
cout<<a[row_start][j]<<" ";
row_start++;
for(int j=row_start;j<row_end;j++)
cout<<a[j][col_end-1]<<" ";
col_end--;
if(row_start<row_end)
{
for(int j=col_end-1;j>=col_start;j--)
cout<<a[row_end-1][j]<<" ";
row_end--;
}
if(col_start<col_end)
{
for(int j=row_end-1;j>=row_start;j--)
cout<<a[j][col_start]<<" ";
col_start++;
}
}
}