Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int binomalCoeff(int n, int k);
- void Allocate(int n);
- void Print(int n);
- void Deallocate(int n);
- int** a;
- int main()
- {
- int n;
- cout << "Enter number of rows n" << endl << "n = ";
- cin >> n;
- Allocate( n);
- Print(n);
- Deallocate(n);
- return 0;
- }
- int binomalCoeff(int n, int k)
- {
- if (k > n)
- {
- return 0;
- }
- else if (n == 0 || k == 0)
- {
- return 1;
- }
- return binomalCoeff(n - 1, k) + binomalCoeff(n - 1, k - 1);
- }
- void Allocate(int n)
- {
- a = new int*[n];
- for (int i = 0; i < n; i++)
- {
- a[i] = new int[i + 1];
- }
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < row + 1; col++)
- {
- a[row][col] = binomalCoeff(row, col);
- }
- }
- }
- void Print(int n)
- {
- for (int row = 0; row < n; row++)
- {
- cout << "[" << row << "] ";
- for (int col = 0; col < row + 1; col++)
- {
- cout << setw(3) << a[row][col] << " ";
- }
- cout << endl;
- }
- }
- void Deallocate(int n)
- {
- for (int i = 0; i < n; i++)
- {
- delete[] a[i];
- }
- delete[] a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement