www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 313 (continued)]

Answers to Self-Review Exercises

6.1

a) functions, classes. b) function call. c) local variable. d) return. e) void. f) scope. g) return;, return expression; or encounter the closing right brace of a function. h) function prototype. i) rand. j) srand. k) auto, register, extern, static. l) auto. m) register. n) global. o) static. p) function scope, file scope, block scope, function-prototype scope, class scope, namespace scope. q) recursive. r) base. s) overloading. t) unary scope resolution operator (::). u) const. v) template.


[Page 314]
6.2

a) block scope. b) block scope. c) file scope. d) file scope. e) file scope. f) function-prototype scope.

6.3

See the following program:

 1  // Exercise 6.3: Ex06_03.cpp
 2  // Testing the math library functions.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6  using std::fixed;
 7
 8  #include <iomanip>
 9  using std::setprecision;
10
11  #include <cmath>
12  using namespace std;
13
14  int main()
15  {
16     cout << fixed << setprecision( 1 );
17
18     cout << "sqrt(" << 900.0 << ") = " << sqrt( 900.0 )
19        << "\nsqrt(" << 9.0 << ") = " << sqrt( 9.0 );
20     cout << "\nexp(" << 1.0 << ") = " << setprecision( 6 )
21        << exp( 1.0 ) << "\nexp(" << setprecision( 1 ) << 2.0
22        << ") = " << setprecision( 6 ) << exp( 2.0 );
23     cout << "\nlog(" << 2.718282 << ") = " << setprecision( 1 )
24        << log( 2.718282 )
25        << "\nlog(" << setprecision( 6 ) << 7.389056 << ") = "
26        << setprecision( 1 ) << log( 7.389056 );
27     cout << "\nlog10(" << 1.0 << ") = " << log10( 1.0 )
28        << "\nlog10(" << 10.0 << ") = " << log10( 10.0 )
29        << "\nlog10(" << 100.0 << ") = " << log10( 100.0 );
30     cout << "\nfabs(" << 13.5 << ") = " << fabs( 13.5 )
31        << "\nfabs(" << 0.0 << ") = " << fabs( 0.0 )
32        << "\nfabs(" << -13.5 << ") = " << fabs( -13.5 );
33     cout << "\nceil(" << 9.2 << ") = " << ceil( 9.2 )
34        << "\nceil(" << -9.8 << ") = " << ceil( -9.8 );
35     cout << "\nfloor(" << 9.2 << ") = " << floor( 9.2 )
36        << "\nfloor(" << -9.8 << ") = " << floor( -9.8 );
37     cout << "\npow(" << 2.0 << ", " << 7.0 << ") = "
38        << pow( 2.0, 7.0 ) << "\npow(" << 9.0 << ", "
39        << 0.5 << ") = " << pow( 9.0, 0.5 );
40     cout << setprecision(3) << "\nfmod("
41        << 13.675 << ", " << 2.333 << ") = "
42        << fmod( 13.675, 2.333) << setprecision( 1 );
43     cout << "\nsin(" << 0.0 << ") = " << sin( 0.0 );
44     cout << "\ncos(" << 0.0 << ") = " << cos( 0.0 );
45     cout << "\ntan(" << 0.0 << ") = " << tan( 0.0 ) << endl;
46     return 0; // indicates successful termination
47  } // end main


[Page 315]

 sqrt(900.0) = 30.0
 sqrt(9.0) = 3.0
 exp(1.0) = 2.718282
 exp(2.0) = 7.389056
 log(2.718282) = 1.0
 log(7.389056) = 2.0
 log10(1.0) = 0.0
 log10(10.0) = 1.0
 log10(100.0) = 2.0
 fabs(13.5) = 13.5
 fabs(0.0) = 0.0
 fabs(-13.5) = 13.5
 ceil(9.2) = 10.0
 ceil(-9.8) = -9.0
 floor(9.2) = 9.0
 floor(-9.8) = -10.0
 pow(2.0, 7.0) = 128.0
 pow(9.0, 0.5) = 3.0
 fmod(13.675, 2.333) = 2.010
 sin(0.0) = 0.0
 cos(0.0) = 1.0
 tan(0.0) = 0.0


6.4
  1. double hypotenuse( double side1, double side2 )

  2. int smallest( int x, int y, int z )

  3. void instructions( void ) // in C++ (void) can be written ()

  4. double intToDouble( int number )

6.5
  1. double hypotenuse( double, double );

  2. int smallest( int, int, int );

  3. void instructions( void ); // in C++ (void) can be written ()

  4. double intToDouble( int );

6.6
  1. register int count = 0;

  2. static double lastVal;

6.7
  1. Error: Function h is defined in function g.

    Correction: Move the definition of h out of the definition of g.

  2. Error: The function is supposed to return an integer, but does not.

    Correction: Delete variable result and place the following statement in the function:

    return x + y;
    
  3. Error: The result of n + sum( n - 1 ) is not returned; sum returns an improper result.

    Correction: Rewrite the statement in the else clause as

    return n + sum( n - 1 );
    
  4. Errors: Semicolon after the right parenthesis that encloses the parameter list, and redefining the parameter a in the function definition.

    Corrections: Delete the semicolon after the right parenthesis of the parameter list, and delete the declaration float a;.

  5. Error: The function returns a value when it is not supposed to.

    Correction: Eliminate the return statement.

6.8

This creates a reference parameter of type "reference to double" that enables the function to modify the original variable in the calling function.


[Page 316]
6.9

False. C++ enables pass-by-reference using reference parameters (and pointers, as we discuss in Chapter 8).

6.10

See the following program:

 1  // Exercise 6.10 Solution: Ex06_10.cpp
 2  // Inline function that calculates the volume of a sphere.
 3  #include <iostream>
 4  using std::cin;
 5  using std::cout;
 6  using std::endl;
 7
 8  #include <cmath>
 9  using std::pow;
10
11  const double PI = 3.14159; // define global constant PI
12
13  // calculates volume of a sphere
14  inline double sphereVolume( const double radius )
15  {
16     return 4.0 / 3.0 * PI * pow( radius, 3 );
17  } // end inline function sphereVolume
18
19  int main()
20  {
21     double radiusValue;
22
23     // prompt user for radius
24     cout << "Enter the length of the radius of your sphere: ";
25     cin >> radiusValue; // input radius
26
27     // use radiusValue to calculate volume of sphere and display result
28     cout << "Volume of sphere with radius " << radiusValue
29        << " is " << sphereVolume( radiusValue ) << endl;
30     return 0; // indicates successful termination
31  } // end main


Previous Page
Next Page