mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Update and rename Pascal_Triangle.cpp to pascal_triangle.cpp
change file name and put code inside a function
This commit is contained in:
parent
cb4722e052
commit
2cbd85914d
@ -2,32 +2,8 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
void show_pascal(int **arr, int n)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -40,13 +16,48 @@ int main()
|
||||
}
|
||||
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;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
pascal_triangle(arr, n);
|
||||
show_pascal(arr, n);
|
||||
|
||||
//deallocation
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
delete[] arr[i];
|
||||
}
|
||||
delete [] arr;
|
||||
delete[] arr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user