forked from codescoop/Play-with-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_hollow_diamond_pattern.cpp
64 lines (54 loc) · 1.46 KB
/
10_hollow_diamond_pattern.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
/*
Hollow Diamond Pattern
Take N (number of rows), print the following pattern (for N = 5).
* * * * *
* * * *
* *
* * * *
* * * * *
Constraints: 0 < N < 10 (where N is an odd number)
Sample Input: 5
Sample Output: * * * * *
* * * *
* *
* * * *
* * * * *
Explanation: Each '*' is separated from other by a tab.
*/
#include<iostream>
using namespace std;
int main() {
int total_rows;
cin >> total_rows;
int mid = (total_rows/2)+1; // middle row
int nop = (total_rows/2)+1; // number of pattern in first row
int nos = -1; // number of space in first row
for(int row=1; row<=total_rows; row++){
int cop; // counter of pattern
int cos; // counter of spaces
//printing pattern
for(cop=1; cop<=nop; cop++){
cout << "*" << "\t";
}
// printing spaces
for(cos=1; cos<=nos; cos++){
cout << " " << "\t";
}
// printing pattern
for(cop=1; cop<=nop; cop++){
if((cop==1 && row == 1) || (cop==1 && row == total_rows))
continue; // to handle extra star
cout << "*" << "\t";
}
// for next iterations
if(row < mid){
nop--;
nos += 2;
}else{
nop++;
nos -= 2;
}
cout << endl;
}
return 0;
}