2020-05-28 23:24:55 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Pascal's triangle implementation
|
|
|
|
*/
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <string> // required for Visual C
|
|
|
|
#else
|
2020-05-26 11:13:26 +08:00
|
|
|
#include <cstring>
|
2020-05-28 23:24:55 +08:00
|
|
|
#endif
|
|
|
|
#include <iomanip>
|
2020-05-26 11:13:26 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-05-28 23:24:55 +08:00
|
|
|
/**
|
|
|
|
* Print the triangle
|
|
|
|
* \param [in] arr 2D-array containing Pascal numbers
|
|
|
|
* \param [in] n depth of Pascal triangle to print
|
|
|
|
*/
|
2020-05-26 11:13:26 +08:00
|
|
|
void show_pascal(int **arr, int n) {
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
for (int j = 0; j < n + i; ++j) {
|
|
|
|
if (arr[i][j] == 0)
|
2020-05-28 23:24:55 +08:00
|
|
|
std::cout << std::setw(4) << " ";
|
2020-05-26 11:13:26 +08:00
|
|
|
else
|
2020-05-28 23:24:55 +08:00
|
|
|
std::cout << std::setw(4) << arr[i][j];
|
2020-05-26 11:13:26 +08:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2019-11-28 20:29:54 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 23:24:55 +08:00
|
|
|
/**
|
|
|
|
* Print the triangle
|
|
|
|
* \param [in,out] arr array containing Pascal numbers
|
|
|
|
* \param [in] n depth of Pascal triangle to print
|
|
|
|
* \result arr pointer returned
|
|
|
|
*/
|
2020-05-26 11:13:26 +08:00
|
|
|
int **pascal_triangle(int **arr, int n) {
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
for (int j = n - i - 1; j < n + i; ++j) {
|
|
|
|
if (j == n - i - 1 || j == n + i - 1)
|
|
|
|
arr[i][j] = 1; // The edge of the Pascal triangle goes in 1
|
|
|
|
else
|
|
|
|
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
2019-11-28 20:29:54 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 23:24:55 +08:00
|
|
|
/**
|
|
|
|
* main function
|
|
|
|
*/
|
2020-05-26 11:13:26 +08:00
|
|
|
int main() {
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
std::cout << "Set Pascal's Triangle Height" << std::endl;
|
|
|
|
std::cin >> n;
|
|
|
|
|
|
|
|
// memory allocation (Assign two-dimensional array to store Pascal triangle)
|
|
|
|
int **arr = new int *[n];
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
arr[i] = new int[2 * n - 1];
|
|
|
|
memset(arr[i], 0, sizeof(int) * (2 * n - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
pascal_triangle(arr, n);
|
|
|
|
show_pascal(arr, n);
|
|
|
|
|
|
|
|
// deallocation
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
delete[] arr[i];
|
|
|
|
}
|
|
|
|
delete[] arr;
|
|
|
|
|
|
|
|
return 0;
|
2019-11-28 20:29:54 +08:00
|
|
|
}
|