forked from rakeshcusat/Code4Reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclockwiseiterator.cpp
More file actions
168 lines (130 loc) · 4.02 KB
/
clockwiseiterator.cpp
File metadata and controls
168 lines (130 loc) · 4.02 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <memory>
#include <cstdlib>
#include <vector>
using namespace std;
#define R_SUCCESS 0
#define TOP_ROW 1
#define RIGHT_COLUMN 2
#define BOTTOM_ROW 3
#define LEFT_COLUMN 4
class ClockwiseIterator{
private:
int rows;
int columns;
vector<vector<char> > two_d_array; /*vectors inside vector will act as an 2D array*/
public:
ClockwiseIterator()
{
rows = 0;
columns = 0;
}
/**
* Read input through the console and initializes the object variable
*/
void read_input(){
char value = 0;
vector<char> tempvector;
cout<<"Please enter #rows & #columns : ";
cin>>rows>>columns; /*Read row and Column */
cout<<"Please enter the values(only char) in row-wise order"<<endl;
/*Reading the array elements value*/
for(int outer_index = 0;outer_index<rows; outer_index++){
tempvector.clear(); /*cleaning the vector before inserting new value for the row*/
for(int inner_index = 0; inner_index <columns; inner_index++){
cin>>value;
tempvector.push_back(value); /*Putting the values in the row*/
}
two_d_array.push_back(tempvector); /*putting the particular row in the vector*/
}
}
/**
* This method display the 2d Array,(Here 2d array taken as vectors inside another vector)
*/
void display_2d_array(){
vector<char> tempvector;
cout<<"========2D Array======="<<endl;
for(int outer_index = 0;outer_index<rows; outer_index++){
tempvector = two_d_array.at(outer_index);
for(int inner_index = 0; inner_index <columns; inner_index++){
cout<<tempvector[inner_index]<<" ";
}
cout<<endl;
}
}
/**
* This method prints the 2-d array in clockwise inward spiral
*
*/
void print_inwardSpiral(){
int direction = TOP_ROW;
/*x,y will represent the current co-ordinate*/
int x = 0;
int y = 0;
/*high_x,high_y,low_x,low_y will decide about the ranges up to which value will be printed in particular rows or columns */
int high_x = rows;
int high_y = columns;
int low_x = 0;
int low_y = 0;
/*Total number of elements present in the 2D array*/
int element_count = rows * columns;
cout<<"========clockwise inward spiral output======="<<endl;
while(0 != element_count){
cout<<two_d_array[x][y]<<" ";
switch(direction){
case TOP_ROW :
if(y+1 < high_y){ /*check if y remains in the boundary for top row, otherwise change direction*/
y++;
}else{
direction = RIGHT_COLUMN;
low_x++;
x++;
}
break;
case RIGHT_COLUMN:
if(x+1 < high_x){ /*check if x remains in the boundary for right column, otherwise change direction*/
x++;
}else{
direction = BOTTOM_ROW;
high_y--;
y--;
}
break;
case BOTTOM_ROW :
if(y-1 >= low_y){ /*check if y remains in the boundary for bottom row,otherwise change direction*/
y--;
}
else{
direction = LEFT_COLUMN;
high_x--;
x--;
}
break;
case LEFT_COLUMN: /*check if x remains in the boundary for left column, otherwise change direction*/
if(x-1 >= low_x){
x--;
}
else{
direction = TOP_ROW;
low_y++;
y++;
}
break;
default:
cout<<"[ERROR]:This state shouldn't be reached"<<endl;
}
element_count--; /*decrement the count*/
}
cout<<endl;
}
};
/**
* Main method to create object and call the memember methods
*/
int main(int argc, char **argv){
ClockwiseIterator obj;
obj.read_input();
obj.display_2d_array();
obj.print_inwardSpiral();
return R_SUCCESS;
}