Chapter 6 Exercise Set 1

  1. Write a program to test the sequence function introduced in the The while statement section. You should call this function from main with several different arguments, and observe its behavior.

  2. Write programs to test each of the following two functions:

    void lool_forever(int n) {
        while (true) {
            cout << "n is now " << n << "." << endl;
            n = n + 1;
        }
    }
    

    and

    void recurse_forever(int n) {
        cout << "n is now " << n << "." << endl;
        recurse_forever(n + 1);
    }
    

    Call these functions from your main function with 1 as an argument. What happens?

    Do a web search for the term call stack (or runtime stack). What do you think the call stack has to do with the outcome of your experiment with these two functions?

  3. Log tables may not be useful any more, but for computer scientists, knowing the powers of two is! Modify the last program in the Tables so that it outputs the powers of two up to 66536 (that’s \(2^{16}\)). Print it out and memorize it.

  4. Rename the parameter high in the function print_multiples in More generalization section to upper. Put both of these in a file named print_mult_tables.cpp that has a main function that calls them. Compile and test your program and confirm that it works as expected.