-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathdiamond_pattern.cpp
78 lines (60 loc) · 2.41 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
/*
Approach:
1. We use two nested loops to iterate through each row and each column of the diamond pattern.
2. The first loop is used to iterate through the rows of the diamond. It starts from 0 and goes up to n-1 rows for the upper half of the diamond.
3. Inside the first loop, we use a nested loop to print spaces before the pattern of each row. The number of spaces decreases by 1 as we move down the rows.
4. Again inside the first loop, we use another nested loop to print the pattern of asterisks (*) for each row. The number of asterisks increases by 2 as we move down the rows.
5. After printing each row, we move to the next line using `cout << endl;`.
6. Now, we need to print the lower half of the diamond. For this, we use another loop that starts from n-2 (as we have already printed the top-most row in the first loop) and moves down to 0 rows.
7. Inside this loop, we print the spaces and pattern the same way as in the first loop.
8. Finally, we have the main function where we take input from the user for the number of rows and call the `printDiamondPattern` function.
Time Complexity: O(n^2) - as we use nested loops to iterate through each row and each column of the diamond pattern.
Space Complexity: O(1) - as we only use a constant amount of additional space for variables.
Sample Input:
Enter the number of rows: 5
Sample Output:
*
***
*****
*******
*********
*******
*****
***
*
*/
#include <iostream>
using namespace std;
void printDiamondPattern(int n) {
// Print the upper half of the diamond
for (int i = 0; i < n; i++) {
// Print spaces before the pattern for each row
for (int j = 0; j < n - i - 1; j++) {
cout << " ";
}
// Print the pattern for each row
for (int j = 0; j < 2 * i + 1; j++) {
cout << "*";
}
cout << endl; // Move to the next line
}
// Print the lower half of the diamond
for (int i = n-2; i >= 0; i--) {
// Print spaces before the pattern for each row
for (int j = 0; j < n - i - 1; j++) {
cout << " ";
}
// Print the pattern for each row
for (int j = 0; j < 2 * i + 1; j++) {
cout << "*";
}
cout << endl; // Move to the next line
}
}
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
printDiamondPattern(n);
return 0;
}