-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
58 lines (53 loc) · 1.16 KB
/
main.cpp
File metadata and controls
58 lines (53 loc) · 1.16 KB
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
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
string format(unsigned int i)
{
ostringstream result;
cout.width(10); result << right << to_string(i);
return result.str();
}
void pascal(unsigned int rows)
{
vector<unsigned int> current = {0, 1, 0};
vector<unsigned int> temp;
unsigned int spaces = rows;
for (int e = 0; e < rows; ++e)
{
for (int i = 0; i < spaces - 1; ++i)
{
cout << " ";
}
--spaces;
for (int i = 1; i < current.size() - 1; ++i)
{
cout << format(current[i]);
}
cout << endl;
temp.push_back(0);
for (int i = 1; i < current.size(); ++i)
{
temp.push_back(current[i - 1] + current [i]);
}
temp.push_back(0);
current.clear();
current = temp;
temp.clear();
}
}
int main()
{
int rows;
cout << "How many rows of Pascal's triangle would you like?\n";
cin >> rows;
while (!cin)
{
cout << "Not a valid entry, please enter a number\n";
cin.clear();
cin.ignore();
}
pascal(rows);
return 0;
}