From 60c8fe6ec139ec65766bc30e41b50c2582ddf244 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 19 Dec 2023 02:00:49 +0000 Subject: - Added solutions by Laurent Rosenfeld. - Added solutions by Ulrich Rieke. --- challenge-248/ulrich-rieke/cpp/ch-2.cpp | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 challenge-248/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-248/ulrich-rieke/cpp/ch-2.cpp') diff --git a/challenge-248/ulrich-rieke/cpp/ch-2.cpp b/challenge-248/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..a20957bbd9 --- /dev/null +++ b/challenge-248/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +void print( const std::vector & numbers ) { + std::cout << " ( " ; + for ( int i : numbers ) { + std::cout << i << " " ; + } + std::cout << ")\n" ; +} + +int main( ) { + std::cout << "Enter a matrix by inputting an equal number of integers per line!\n" ; + std::cout << "Enter to end!\n" ; + std::vector> matrix ; + std::string line ; + std::getline( std::cin , line ) ; + while ( line.length( ) > 0 ) { + std::vector linenumbers ( split( line , " " ) ) ; + std::vector numbers ; + for ( auto s : linenumbers ) + numbers.push_back( std::stoi( s ) ) ; + matrix.push_back( numbers ) ; + std::getline( std::cin , line ) ; + } + int matrixlen = matrix.size( ) ; + int rowlen = matrix[0].size( ) ; + std::vector> result ; + for ( int r = 0 ; r < matrixlen - 1 ; r++ ) { + std::vector resultline ; + for ( int col = 0 ; col < rowlen - 1 ; col++ ) { + int sum = matrix[ r ][col] + matrix[r][ col + 1 ] + + matrix[ r + 1][ col ] + matrix[ r + 1 ][ col + 1 ] ; + resultline.push_back( sum ) ; + } + result.push_back( resultline ) ; + } + std::cout << "(\n" ; + for ( const auto & numline : result ) { + print( numline ) ; + } + std::cout << ")\n" ; + return 0 ; +} + -- cgit