-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path07_double_sideed_arrow.cpp
More file actions
165 lines (139 loc) · 4.49 KB
/
07_double_sideed_arrow.cpp
File metadata and controls
165 lines (139 loc) · 4.49 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
/*
Pattern Double Sided Arrow
Take N as input. For a value of N=7, we wish to draw the following pattern :
1
2 1 1 2
3 2 1 1 2 3
4 3 2 1 1 2 3 4
3 2 1 1 2 3
2 1 1 2
1
Input Format: Take N as input.
Constraints: N is odd number.
Output Format: Pattern should be printed with a space between every two values.
Sample Input: 7
Sample Output: 1
2 1 1 2
3 2 1 1 2 3
4 3 2 1 1 2 3 4
3 2 1 1 2 3
2 1 1 2
1
Explanation: Catch the pattern and print it accordingly.
*/
#include<iostream>
using namespace std;
int main() {
int total_rows;
cin >> total_rows;
int nos1 = total_rows-1; // nos - number of space in first row of pattern-1
int nos2 = -1; // nos - number of space in first row of pattern-2
int nop = 1; // nov - number of pattern in first row
for(int row=1; row <= total_rows; row++){
int val;
if(row <= total_rows/2) // till mid rows
val = row;
else // after mid rows
val = (total_rows-row)+1;
// print spaces
for(int cos=1; cos <= nos1; cos++){ // cos - counter of spaces
cout << " ";
}
// print numbers - decreasing
for(int cop=1; cop<=nop; cop++){ // cop - counter of pattern
cout << val << " ";
val--;
}
// print spaces
for(int cos=1; cos<=nos2; cos++){
cout << " ";
}
// print number - increasing
for(int cop=1; cop<=nop; cop++){
if(row==1 || row==total_rows)
continue;
val++;
cout << val << " ";
}
// for next iteration
if(row <= total_rows/2){ // till mid rows
nos1 -= 2;
nop++;
nos2 += 2;
}else{ // after mid rows
nos1 += 2;
nop--;
nos2 -= 2;
}
cout << endl;
}
return 0;
}
/*
#include<iostream>
using namespace std;
int main() {
int total_rows;
cin >> total_rows;
int nsp1 = total_rows-1; // nsp - number of space in first
int nst = 1; // nst - number of values in first
int nsp2 = 2; // nsp - number of space in first
int nst2 = total_rows/2;
int num = 1;
for(int row=1; row <= total_rows; row++){
int csp;
int cst;
if(row <= (total_rows/2)+1){ // till mid value
// print space-1
for(csp=1; csp <= nsp1; csp++){
cout << " " << " ";
}
// print pattern-decreasing
for(cst=1; cst<=nst; cst++){
cout << num << " ";
num--;
}
//print space-2
for(csp=1; csp<=(2*(row-1)-1); csp++){
cout << " " << " ";
}
//print pattern-increasing
for(cst=1; cst<=nst; cst++){
num++;
if(nst > 1){
cout << num << " ";
}
}
nsp1 = nsp1-2;
nst++;
}else{ // after mid value
// print spaces-1
for(csp=1; csp<=nsp2; csp++){
cout << " " << " ";
}
// print pattern-descreasing
num = num-2;
for(cst=1; cst<=nst2; cst++){
cout << num << " ";
num--;
}
// print spaces-2
for(csp=1; csp<=((total_rows*2)-(row*2)-1); csp++){
cout << " " << " ";
}
//print patern-increasing
for(cst=1; cst<=nst2; cst++){
if(row==total_rows)
break;
num++;
cout << num << " ";
}
nsp2 = nsp2+2;
nst2--;
}
num++;
cout << endl;
}
return 0;
}
*/