-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pascal.cpp
61 lines (45 loc) · 1.22 KB
/
Pascal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
long int r, k, max; //rzad, kolumna
cout << "This program prints the Pascal triangle\n";
cout << "enter level: ";
cin >> r;
r = r - 1;
k = r;
max = r;
long int tab[ r + 1 ][ k + 1 ]; //create two-dimensional tables
for( r = 0; r <= max; r++ )
{
for( k = 0; k <= max; k++ )
{
tab[ r ][ k ] = 0;
}
} // clean the whiteboard
tab[ 0 ][ 0 ] = 1;
tab[ 1 ][ 0 ] = 1;
tab[ 1 ][ 1 ] = 1;
for( long int r = 2; r <= max; r++ )
{
for( long int k = 0; k <= r; k++ )
{
if( r == k ) tab[ k ][ r ] = 1;
if( k == 0 ) tab[ k ][ r ] = 1;
tab[ r ][ k ] = tab[ r - 1 ][ k - 1 ] + tab[ r - 1 ][ k ];
}
} //calculation of cell values
for( r = 0; r <= max; r++ )
{
for( k = 0; k <= r; k++ )
{
cout << tab[ r ][ k ] << " ";
}
cout << endl;
} //writing the result on the screen
r=2;
k=0;
tab[ r ][ k ] = tab[ r - 1 ][ k - 1 ] + tab[ r - 1 ][ k ];
cout << tab[2 ][ 2 ] << " ";
}