Chapter 6

Recursion, Scope, and Lifetime


Page 273

6.1 The output of files.cc on the directory tapestry would be

  book.tex

since there is only one file that is not also a directory in tapestry. If there is no check for a directory in files.cc then the output will be (of files.cc )

   .
   ..
   chap2
   chap1
   chap3
   book.tex
  library

The order of these files may be different, the file "." is the current directory, and the file ".." is the parent directory.

6.2 If a while loop is used it will look like this:

    while (dir.GetEntry(entry))
    {
       // process entry
    }

It's NOT necessary to use a line: entry = dir.Current() and, in fact, this won't work without the use of member functions First() and Next().

6.3 The if statement checking for "." and ".." is necessary or an infinite recursion would result. Suppose, for example, that you entire "mydir" as the name of a directory. If "." generates a recursive call, then "mydir" will be examined again which will generate a call for "." to examine "mydir" again which will .... (hopefully you see what happens here).

If the comparison with "." is removed, but ".." remains, then the parent directory of "mydir" will be searched recursively, which will yield "mydir" as one of the entries which will call the parent directory, and so forth and so on.

6.4 If tabCount+2 is used in the recursive calls then there will be two tabs rather than one tab between each directory and its subdirectories as shown, for example (with one tab), in the output box on page 270.

6.5 Moving the Tab and output statements after the check for being a directory causes the subdirectory names to be printed after the contents of the subdirectory, e.g., as shown below (generated by moving the statements and re-executing)

prompt> enter directory name: tapestry
        (1) chapter2.tex
                (1) hello.cc
                (2) bday.cc
                (3) oldmac1.cc
        (2) progs
        (3) oldmac.eps
(1) chap2
        (1) chapter1.tex
        (2) finalbigpic.eps
(2) chap1
        (1) chapter3.tex
                (1) fly.cc
                (2) macinput.cc
                (3) pizza.cc
        (2) progs
(3) chap3
        (1) CPstring.h
        (2) dice.h
        (3) dice.cc
(4) library
(5) book.tex

Page 292

6.6 If the user enters

   "yellow"  "red" "Apple"
the output will be

                  Banana yellow Banana red Apple

If the user enters "Apple" the output will be

                  Apple

If the definition: string last = "Banana" is removed from inside the loop, and the user enters

   "Banana" "yellow" "Banana" "red" "Apple"

the output will be

                  Banana yellow Banana red Apple

6.7 The segment will not compile since the identifier 'last' in the loop guard will be undefined.

6.8 The output will be 0 printed ten times (on a line by itself) in addition to the output from main (the local variable has value 0).

6.9 The parameters one and two alias the parameter number. This means that the statement

   one = two*one 

effectively squares the value of 'one' since one and two reference the same memory location.

The statement two = two/one assigns the value of 1 to two NO MATTER what the value of parameter 'two' is because 'one' and 'two' reference the same memory location. This means that ANY value of number will cause 1.0 to be printed except for 0 which will cause an error because of division by zero.

6.10 If 'static' is removed from 'delimiter' then 'delimiter' will be initialized to "" every time the function Print is called rather than before the function is called for the first time. This means that no word will have a comma after it.

If static is removed from 'numWords' then the initialization to 0 will happen each time the function is entered and the statement

   cout << endl;

will NEVER be executed.

6.11 LINE_SIZE is only accessed in Print , therefore there is no compelling reason for it to be global. When constants are accessed in more than one function it is reasonable to make them global.

6.12 The statement

    Dummy::first = first;

will 'do the right thing' by assigning the value of parameter 'first' to private variable 'first'.

6.13 The statement below increments a global variable funCounter. This will correctly track how many function calls are made, even recursive calls.

    ::funCounter += 1;