diff options
Diffstat (limited to 'challenge-108/ulrich-rieke/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-108/ulrich-rieke/cpp/ch-2.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-108/ulrich-rieke/cpp/ch-2.cpp b/challenge-108/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..5d39d4793d --- /dev/null +++ b/challenge-108/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,26 @@ +#include <vector> +#include <iostream> + +int main( ) { + std::vector<int> bellnumbers { 1 } ; + std::vector<int> currentRow ; + std::vector<int> previousRow { 1 } ; + while ( bellnumbers.size( ) < 10 ) { + currentRow.push_back( previousRow.back( )) ; + int currentIndex = 0 ; + while ( currentRow.size( ) < ( previousRow.size( ) + 1 ) ) { + currentIndex++ ; + currentRow.push_back( currentRow[ currentIndex - 1 ] + + previousRow[ currentIndex - 1 ] ) ; + } + bellnumbers.push_back( *(currentRow.begin( ) ) ) ; + previousRow = currentRow ; + currentRow.clear( ) ; + } + std::cout << "These are the first 10 Bell numbers:\n" ; + for ( int i = 1 ; i < 10 ; i++ ) { + std::cout << " " << i << " : " << bellnumbers[ i - 1 ] << '\n' ; + } + std::cout << 10 << " : " << bellnumbers[9] << '\n' ; + return 0 ; +} |
