Created
November 21, 2014 21:12
-
-
Save larsfu/e9e9a20d5b1e19c481c6 to your computer and use it in GitHub Desktop.
pascal triangle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) | |
{ | |
unsigned int n, k; | |
unsigned int indent; | |
unsigned int size = 8; | |
int **triangle = new int*[size]; | |
for ( n = 0; n < size; ++n ) { | |
triangle[n] = new int[n + 1]; | |
for ( k = 0; k < n + 1; ++k ) { | |
if(k == 0 || k == n) | |
triangle[n][k] = 1; | |
else | |
triangle[n][k] = triangle[n-1][k] + triangle[n-1][k-1]; | |
} | |
} | |
//Ausgabe | |
for ( n = 0; n < size; ++n ) | |
{ | |
for ( indent = 0; indent < size - n; ++indent ) | |
cout << " "; | |
for ( k = 0; k < n + 1; ++k ) | |
{ | |
if ( triangle[ n ][ k ] < 100 ) | |
cout << " "; | |
cout << triangle[ n ][ k ] << " "; | |
if ( triangle[ n ][ k ] < 10 ) | |
cout << " "; | |
} | |
cout << endl; | |
} | |
for ( n = 0; n < size; ++n ) { | |
delete [] triangle[n]; | |
} | |
delete [] triangle; | |
return 0; | |
} | |
/* | |
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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment