Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 6. Functions and an Introduction... > Answers to Self-Review Exercises

Answers to Self-Review Exercises

6.1
  1. functions, classes.

  2. function call.

  3. local variable.

  4. return.

  5. void.

  6. scope.

  7. return;, return expression; or encounter the closing right brace of a function.

  8. function prototype.

  9. rand.

  10. srand.

  11. auto, register, extern, static.

  12. auto.

  13. register.

  14. global.

  15. static.

  16. function scope, global namespace scope, local scope, function-prototype scope, class scope, namespace scope.

  17. recursive.

  18. base.

  19. overloading.

  20. unary scope resolution operator (::).

  21. const.

  22. template.

6.2
  1. local scope.

  2. local scope.

  3. global namespace scope.

  4. global namespace scope.

  5. global namespace scope.

  6. function-prototype scope.

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

					  

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(5.1) = 5.1
fabs(0.0) = 0.0
fabs(-8.8) = 8.8
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(2.600, 1.200) = 0.200
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()

  4. double intToDouble( int number )

6.5
  1. double hypotenuse(double, double );

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

  3. void instructions();

  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 isn’t supposed to.

    Correction: Eliminate the return statement or change the return type.

6.8This creates a reference parameter of type “reference to double” that enables the function to modify the original variable in the calling function.
6.9False. C++ enables pass-by-reference using reference parameters (and pointers, as we discuss in Chapter 8).
6.10See 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   #include <cmath>
 5   using namespace std;
 6
 7   const double PI = 3.14159; // define global constant PI
 8
 9   // calculates volume of a sphere
10   inline double sphereVolume( const double radius )
11   {
12      return 4.0 / 3.0 * PI * pow( radius, 3 );
13   } // end inline function sphereVolume
14
15   int main()
16   {
17      double radiusValue;
18
19      // prompt user for radius
20      cout << "Enter the length of the radius of your sphere: ";
21      cin >> radiusValue; // input radius
22
23      // use radiusValue to calculate volume of sphere and display result
24      cout << "Volume of sphere with radius " << radiusValue
25         << " is " << sphereVolume( radiusValue ) << endl;
26   } // end main

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial