Answers to Exercises

  1. Here are the answers for each of the programs in Figure 5.22 through 5.27:

    1. 1 1 1 1 1 1 1 1 1 1 1

    2. 6 6 6 6 6 6 6 6 6 6

    3. 1 2 3 4 5 6 7 8 9 10

    4. 1 2 3 4 5 6 7 8 9 10

    5. 1 2 3 4 5 6 7 8 9 10

    6. 1 2 3 4 5 6 7 8 9 10

Why are these the way they are? Well, let's take them in order, except for b, which I'll take up last.

The reason for the results from a should be fairly obvious; since we set the variable count to 0 every time we enter the counter function, incrementing it always gives the answer 1.

As for c-f, they all produce the same answer; namely, the output value starts at 1 and increments by 1 each time. This is because the variable named count is statically allocated in each of these cases, which has two consequences: The initialization of ...

Get Learning to Program in C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.