-
Notifications
You must be signed in to change notification settings - Fork 1
/
spiral matrix.cpp
111 lines (91 loc) · 2.18 KB
/
spiral matrix.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*PROGRAM TO PRINT A SPIRAL ARRAY*/
/*
Copyright (c) 2013 GODLY T.ALIAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
//This is program for spiral matrix. Spiral Matrix is a matrix with
//the middle element as 0 and will be incremented by 1 as it goes out spirally
//eg: n=3
// 4 3 2
// 5 0 1
// 6 7 8
#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;
int n;
//returns the direction in which the next element to be filled up in the matrix
int direction(int i, int j, int key)
{
if((abs((n/2)-i)==key) && (abs((n/2)-j)==key))
{
if(i<(n/2) && j>(n/2))
return 1;//left
else if(i<(n/2) && j<(n/2))
return 2;//down
else if(i>(n/2) && j<(n/2))
return 3;//right
else return -1;
}
else if((abs((n/2)-i)==key) && (abs((n/2)-j)>key) && i>(n/2) && j>(n/2))
return 0;//up
else return -1;
}
int main(){
int i,j,count=1,temp;
cout<<"Enter n (<50): ";
cin>>n;
if(n%2==0)
n++; //making n odd
int sp[50][50],key,dir;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
sp[i][j]=0;
key=1;
i=n/2;
j=(n/2)+1;
temp=0;
while(true)
{
//giving values in the array
sp[i][j]=count;
if(temp!=-1)
dir=temp;
switch(dir){
case 0://up
i--;
break;
case 1://left
j--;
break;
case 2://down
i++;
break;
case 3://right
j++;
break;
default:
break;
}
if(key>n || i<0 || j<0)
break;
//getting the direction
temp = direction(i,j,key);
if((j-(n/2))>key)
key++;
count++;
}
//outputing
for(i=0;i<n;i++){
for(j=0;j<n;j++)
cout<<std::setw(4)<<sp[i][j];
cout<<"\n";}
return 0;
}