aboutsummaryrefslogtreecommitdiff
path: root/challenge-248/ulrich-rieke/cpp/ch-2.cpp
blob: a20957bbd99f0d1af310f42453dbb540150c6505 (plain)
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
#include <iostream> 
#include <string> 
#include <vector>

std::vector<std::string> split( const std::string & startline ,
      const std::string & sep ) {
   std::vector<std::string> 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<int> & 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 <return> to end!\n" ;
   std::vector<std::vector<int>> matrix ;
   std::string line ;
   std::getline( std::cin , line ) ;
   while ( line.length( ) > 0 ) {
      std::vector<std::string> linenumbers ( split( line , " " ) ) ;
      std::vector<int> 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<std::vector<int>> result ;
   for ( int r = 0 ; r < matrixlen - 1 ; r++ ) {
      std::vector<int> 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 ;
}