// ThreeMap3.cc // Prints out values of a function #include using namespace std; int threemap (int n) { if (n%2) // n is odd return (3*n+1); else // n is even return (n/2) ; } int sumsquaredigits (int n) { // Returns sum of squares of digits in n int answer = 0 , rem ; while ( n > 0 ) { // assumes positive n rem = n%10 ; // remainder is rightmost digit answer += rem * rem ; n = n/10 ; } return answer ; } void printout( int (*func)(int), int xmin=1, int xmax=120 ); int main() { cerr << "Choose threemap(0) or sumsquaredigits(1): " ; int answer; cin >> answer ; if(answer) printout( sumsquaredigits ) ; else printout( threemap ) ; return 0; } void printout( int (*func)(int), int xmin, int xmax ) { for ( int x = xmin ; x <= xmax ; x ++ ) { cout << x << " " << func( x ) << endl ; } }