aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/paulo-custodio/cpp/ch-2.cpp
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2021-02-22 17:45:14 +0800
committer冯昶 <fengchang@novel-supertv.com>2021-02-22 17:45:14 +0800
commit572994875f5972916deabeb84f3648ac4640107b (patch)
tree1458cfc22c7b80dd025415f73e7c808814bb0db7 /challenge-003/paulo-custodio/cpp/ch-2.cpp
parent7021a6ee78be5c0a1bc1a30db583004a88fc7a15 (diff)
parent2c26164a5a90aa14a19078d845769d3ec9fbb5ae (diff)
downloadperlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.tar.gz
perlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.tar.bz2
perlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-003/paulo-custodio/cpp/ch-2.cpp')
-rw-r--r--challenge-003/paulo-custodio/cpp/ch-2.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-003/paulo-custodio/cpp/ch-2.cpp b/challenge-003/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..cf64c51a86
--- /dev/null
+++ b/challenge-003/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,44 @@
+/*
+Challenge 003
+
+Challenge #2
+Create a script that generates Pascal Triangle. Accept number of rows from
+the command line. The Pascal Triangle should have at least 3 rows. For more
+information about Pascal Triangle, check this wikipedia page.
+
+*/
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+void draw_pascal(int rows) {
+ // allocate current and next row
+ std::vector<int> data[2];
+ for (int i = 0; i < 2; i++)
+ data[i].resize(rows+1);
+
+ int cur = 0;
+ data[cur][0] = 1;
+ for (int row = 1; row <= rows; row++) {
+ // print current row
+ for (int col = 0; col < rows-row; col++)
+ std::cout << ' ';
+ for (int col = 0; col < row; col++)
+ std::cout << data[cur][col] << ' ';
+ std::cout << std::endl;
+
+ // compute next row
+ int nxt = 1-cur;
+ data[nxt][0] = 1;
+ data[nxt][row] = 1;
+ for (int col = 1; col < row; col++)
+ data[nxt][col] = data[cur][col-1] + data[cur][col];
+ cur = nxt;
+ }
+}
+
+int main(int argc, char* argv[]) {
+ if (argc == 2)
+ draw_pascal(atoi(argv[1]));
+}