-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascals_triangle
More file actions
41 lines (38 loc) · 964 Bytes
/
pascals_triangle
File metadata and controls
41 lines (38 loc) · 964 Bytes
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
#include <iostream>
#include <vector>
#include <ranges>
std::vector<std::vector<int>> generate(int numRows) {
std::vector<std::vector<int>> rows{};
for (int i = 1; i < numRows + 1; i++) {
rows.push_back(std::vector<int>(i, 1));
}
// now we have go to go through
for (int j = 2; j < numRows; j++) {
for (int i = 1; i < rows[j].size() - 1; i++) {
rows[j][i] = rows[j - 1][i - 1] + rows[j - 1][i];
}
}
return rows;
}
auto print_spaces(int count)
{
// for(int i = 0; i < count; i++)
// {
// std::cout <<
// }
std::string s(count, ' ');
std::cout << s;
}
int main()
{
std::vector<std::vector<int>> pascalTriangle = generate(5);
for(int i = 0; i < pascalTriangle.size(); i++)
{
print_spaces((pascalTriangle.size() - i - 1));
for(int j : pascalTriangle[i])
{
std::cout << " " << j;
}
std::cout << "\n";
}
}