GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — programming/problems/math/PascalTriangle.cpp at master · rp-code9/programming Expand file tree/ Copy pathPascalTriangle.cppMore file actions 71 lines (57 loc) · 1.53 KB / Copy pathPascalTriangle.cppFile metadata and controls71 lines (57 loc) · 1.53 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 59 60 61 62 63 64 65 66 67 68 69 70 71 // https://www.geeksforgeeks.org/find-the-nth-row-in-pascals-triangle/ #include <iostream> #include <vector> template <typename T> void print(std::vector<T>& items) { if (items.size() == 0) { return; } for (auto item : items) { std::cout << item << " "; } std::cout << "\n"; } std::vector<int> generatePascalTriangleRec(int row) { std::vector<int> currentRow; currentRow.push_back(1); if (row == 0) { return currentRow; } std::vector<int> previousRow = generatePascalTriangleRec(row - 1); for (int i = 1; i < previousRow.size(); ++i) { currentRow.push_back(previousRow[i - 1] + previousRow[i]); } currentRow.push_back(1); return currentRow; } std::vector<int> generatePascalTriangleIter(int row) { int previous = 1; std::vector<int> currentRow; currentRow.push_back(1); for (int i = 1; i <= row; ++i) { int current = previous * (row - i + 1) / i; currentRow.push_back(current); previous = current; } return currentRow; } int main() { int n = 10; std::cout << "Recursive\n"; for (int i = 0; i < n; ++i) { std::cout << "Row " << i << " : "; std::vector<int> numbers = generatePascalTriangleRec(i); print(numbers); } std::cout << "===============================\n"; std::cout << "Iterative\n"; for (int i = 0; i < n; ++i) { std::cout << "Row " << i << " : "; std::vector<int> numbers = generatePascalTriangleIter(i); print(numbers); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |