Topical Information

The purpose of this question set is to give you a chance to focus your knowledge of the built-in libraries in C/C++.

Question Set Information

Questions

  1. Show the preprocessor directive(s) needed to include any library in your program. (What symbol makes this a preprocessor directive?)

    
            #include <______>
    
            ^            ^
            |            |
            |            +------ fill in the library's name
            |
            +------ the # symbol makes this a preprocessor directive
    
    
            We also need to place a using directive to more conveniently
            use standard library features (since they are all collected
            in a namespace called std).
    
    
  2. Which of the following are part of the built-in library suite? (Remember that there are two different naming conventions!)

    If it is a library, tell if it is C or C++ native (also tell if it is using a new-style or old-style name). If it isn't, explain the confusion.

    1. math.h C, old-style
    2. time.h C, old-style
    3. cctype C, new-style
    4. iostream C++, new-style
    5. cpow not a library; pow is a function in cmath
    6. cstdlib C, new-style
    7. float.h C, old-style
    8. rand.h not a library; rand is a function in cstdlib
    9. cmath C, new-style
    10. namespace not a library; standard library identifiers are enclosed in a namespace to avoid name clashes with programmer identifiers
    11. iomanip.h C++, old-style
  3. The pow function comes from the cmath library. So does the fabs function for absolute value. However, the abs and labs functions for absolute value are in the cstdlib library (along with the functions for random number generation).

  4. Translate the following math/physics formulas to C++ expressions (variables need not be declared; they'd all be floating-point anyway, right?):

    
                                         
    
    pow(x, y+7)               (-b + sqrt( b*b - 4*a*c))/(2*a)          fabs(x - y)
    
    
    
                                              
    
                    P + rho*v*v/2 + rho*g*y                             2*M*R*R/5
    
               OR   pressure + density*velocity*velocity/2         OR   2*mass*radius*radius/5
                             + density*gravity*height
    
    
  5.  
    TRUE/FALSE When I include functions from the built-in library suite, I must also type their prototypes into the main application file myself.
    TRUE/FALSE My variable names must match the names of the arguments listed in the library function's head.
    TRUE/FALSE It is common for programmers to browse the physical contents of .h files (i.e. actually look at the library header files).
    TRUE/FALSE Libraries contain only functions — no constants or variables.
    TRUE/FALSE C libraries work exactly the same as C++ libraries (although they have funny names on some systems).
  6. Name at least three of the objects, functions, and/or constants provided by each of the following libraries.

    1. cmath
      
              fabs           ceil      log        asin      sin
              pow            exp       log10      acos      cos
              floor          sqrt      atan2      atan      tan
      
          
    2. cctype
      
              toupper        ispunct       isdigit          isupper
              tolower        iscntrl       isalnum          islower
              isspace        isalpha       isxdigit
      
          
    3. iostream
      
              cout           .ignore        .fail          .precision
              cin            .peek          .flush
              cerr           .clear         .setf
      
          
    4. cstdlib
      
              RAND_MAX        abs
              rand            labs
              srand
      
          
  7.  

    TRUE/FALSE The tolower function will only work on alphabetic values.
    TRUE/FALSE The argument (and return value) of tolower is of type char.
    TRUE/FALSE Your program must use/store the value returned from tolower — your variable is not automatically altered.
    TRUE/FALSE However, to print the returned value directly, you must type-cast it or the ASCII value of the lowercase version will be printed instead of the lowercase version itself!