-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arrow Pattern.cpp
64 lines (26 loc) · 979 Bytes
/
Arrow 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
vector<string> printArrowPattern(int n) {
// Create an array of string for storing the pattern
vector<string> answer;
// Create 2 strings 'str1' for ' ' and 'str2' for '*'
string str1, str2;
// Run a loop from 1 to 2'N' - 1
for (int i = 1; i <= (2 * n - 1); i++) {
// Check condition for increse length or decrese length
if (i <= n) {
// Append ' ' to 'str1'
str1.push_back(' ');
// Append '*' to 'str2'
str2.push_back('*');
// Append 'str1' + 'str2' to 'answer'
answer.push_back(str1 + str2);
} else {
// Remove last characters from both strings
str1.pop_back();
str2.pop_back();
// Append concatation of both strings to 'answer' array
answer.push_back(str1 + str2);
}
}
//Return 'answer' array that contains pattern
return answer;
}