-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpascalTriangle.cpp
62 lines (59 loc) · 1.27 KB
/
pascalTriangle.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
/* Pascal Triangle using 2d vector
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
#include <iostream>
#include <vector>
using namespace std;
// Create pascal triangle
vector<vector<int>> pascalTriangle(int &n)
{
// declare n size of temp vector <pascal>
vector<vector<int>> pascal(n);
for (int i = 0; i < n; i++)
{
pascal[i].resize(i + 1);
for (int j = 0; j < i + 1; j++)
{
if (j == 0 || j == i)
{
pascal[i][j] = 1;
}
else
{
pascal[i][j] = pascal[i - 1][j] + pascal[i - 1][j - 1];
}
}
}
return pascal;
}
int main()
{
int n;
cout << "Enter the size of pascal triangle: \n";
// cin >> n;
n = 5;
// Declare 2d vector
vector<vector<int>> result;
// Invoked pascalTriangle()
result = pascalTriangle(n);
// Display result
for (int i = 0; i < result.size(); i++)
{
// print space for pyramid
for (int k = result.size() - (i + 1); k > 0; k--)
{
cout << " ";
}
// print elements
for (int j = 0; j < result[i].size(); j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}