Solution
#include <iostream>
#include <iomanip>
using namespace std;
void print_array(int arr[], int len)
{
for(int i = 0; i < len; i++){
cout.width(5);
cout << setfill(' ') << arr[i];
}
cout << endl;
}
void print_triangle(int n)
{
int *a = new int[1];
a[0] = 1;
print_array(a, 1);
int *b;
for(int i = 2; i < n + 1; i ++){
b = new int[i];
b[0] = 1;
for(int j = 1; j < i - 1; j++){
b[j] = a[j - 1] + a[j];
}
b[i - 1] = 1;
print_array(b, i);
delete[] a;
a = b;
}
delete[] a;
}
int main(int argc, char* argv[])
{
print_triangle(10);
return 0;
}
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Problem
Implement an algorithm to print the following diagram
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1