TheAlgorithms-C-Plus-Plus/Others/Pascal_Triangle.cpp
joker123 cb4722e052
Add files via upload
add Pascal's Triangle Algorithm
2019-11-12 22:39:16 +09:00

52 lines
938 B
C++

#include<iostream>
using namespace std;
int main()
{
int n = 0;
cout << "Set Pascal's Triangle Height" << endl;
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));
}
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];
}
}
//pint Pascal's Triangle
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n + i; ++j)
{
if (arr[i][j] == 0)
cout << " ";
else
cout << arr[i][j];
}
cout << endl;
}
//deallocation
for (int i = 0; i < n; ++i)
{
delete[] arr[i];
}
delete [] arr;
return 0;
}