-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPascal.cpp
65 lines (61 loc) · 1.73 KB
/
Pascal.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
#include <iostream>
using namespace std;
#include <vector>
class Pascal {
public:
void printFull();
void get(int, int);
void print(int);
};
void Pascal::get(int i, int j) {
//gets the value at i,j given in main
int coeff = 1;
vector<int> row;
for (int k = 0; k <= i; k++) {
coeff = coeff * (i - k) / (k + 1);
row.push_back(coeff);
}
cout << row[j] << endl;
}
void Pascal::printFull() {
//prints 8 rows of pascals triangle
int coeff = 1;
int rows = 8;
for(int i = 0; i < rows; i++) {
for(int space = 1; space <= rows-i; space++)
cout <<" ";
for(int j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coeff = 1;
else
coeff = coeff*(i-j+1)/j;
cout << coeff << " ";
}
cout << endl;
}
}
void Pascal::print(int m) {
int coef = 1;
int rows = 8;
for(int i = 0; i < rows; i++) {
for(int space = 1; space <= rows-i; space++)
cout << " ";
for(int j = 0; j<= i; j++) {
if(j == 0 || i == 0)
coef = 1;
else
coef = coef*(i-j+1)/j;
if (coef%m != 0)
cout << " * ";
else
cout << " ";
}
cout << endl;
}
}
int main() {
Pascal pasc;
pasc.printFull();
pasc.get(3, 1);
pasc.print(2);
}