Update and rename Pascal_Triangle.cpp to pascal_triangle.cpp

change file name and put code inside a function
This commit is contained in:
joker123 2019-11-17 15:00:37 +09:00 committed by GitHub
parent cb4722e052
commit 2cbd85914d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,38 @@
using namespace std;
void show_pascal(int **arr, int n)
{
//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;
}
}
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;
}
int main()
{
int n = 0;
@ -17,29 +49,8 @@ int main()
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;
}
pascal_triangle(arr, n);
show_pascal(arr, n);
//deallocation
for (int i = 0; i < n; ++i)