-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.cpp
More file actions
35 lines (29 loc) · 732 Bytes
/
triangle.cpp
File metadata and controls
35 lines (29 loc) · 732 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
#include "triangle.hpp"
// Example:
// 1
// 1 1
// 1 2 1
// 1 3 3 1
// 1 4 6 4 1
// 1 5 10 10 5 1
// Triangle constructor
PascalsTriangle::PascalsTriangle(int triangleSize) {
this->InitRows(triangleSize);
}
// Access the element on the n row, k column
long long PascalsTriangle::Combinations(int n, int k) {
if (k > n || n < 0 || k < 0) {
return 0;
}
return rows[n][k];
}
// Recursive method for Pascal's triangle
void PascalsTriangle::InitRows(int triangleSize) {
for (int n = 0; n <= triangleSize; n++) {
std::vector<long long> row = {1};
for (int k = 1; k <= n; k++) {
row.push_back(Combinations(n - 1, k) + Combinations(n - 1, k - 1));
}
rows.push_back(row);
}
}