pascal_triangle.cpp File Reference

Pascal's triangle implementation. More...

`#include <cstring>`
`#include <iomanip>`
`#include <iostream>`
Include dependency graph for pascal_triangle.cpp:

## Functions

void | show_pascal (int **arr, int n)
int ** | pascal_triangle (int **arr, int n)
int | main ()

## Detailed Description

Pascal's triangle implementation.

## Function Documentation

## main()

int main | ( | void |  | ) |

main function

{
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;
}
std::cin
std::cout
std::endl
T endl(T... args)
std::memset
T memset(T... args)
show_pascal
void show_pascal(int **arr, int n)
Definition: pascal_triangle.cpp:18
pascal_triangle
int ** pascal_triangle(int **arr, int n)
Definition: pascal_triangle.cpp:36
Here is the call graph for this function:

## pascal_triangle()

int** pascal_triangle | ( | int ** | arr,
|  | int | n
| ) |  |

Print the triangle

Parameters

  [in,out] | arr | array containing Pascal numbers
  [in] | n | depth of Pascal triangle to print

Returns
  arr pointer returned

{
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;
}

## show_pascal()

void show_pascal | ( | int ** | arr,
|  | int | n
| ) |  |

Print the triangle

Parameters

  [in] | arr | 2D-array containing Pascal numbers
  [in] | n | depth of Pascal triangle to print

{
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n + i; ++j) {
if (arr[i][j] == 0)
std::cout << std::setw(4) << " ";
else
std::cout << std::setw(4) << arr[i][j];
}
std::cout << std::endl;
}
}
std::setw
T setw(T... args)
Here is the call graph for this function: