// Iterates the function 'threemap' until it reaches state N=1 #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 main(int argc, char* argv[]) { int N=12; // starting number int I=1000; // maximum number of iterations int match=1; // termination condition if(argc>1) { sscanf( argv[1], "%d", &N ) ; // put the first command-line argument in N } cout << N << endl ; for ( int i = 1 ; i <= I ; i ++ ) { N = threemap( N ) ; cout << N << " " ; if ( N == match ) { cout << "\n(" << i << " iterations)\n" ; return 0; } if ( !(i%10) ) cout << endl ; } return 0; }