Chapter 6

advertisement
[Page 311]
Self-Review Exercises
6.1
Answer each of the following:
a. Program components in C++ are called ________ and ________.
b. A function is invoked with a(n) ________.
c. A variable that is known only within the function in which it is
defined is called a(n) ________.
d. The ________ statement in a called function passes the value of an
expression back to the calling function.
e. The keyword ________ is used in a function header to indicate
that a function does not return a value or to indicate that a function
contains no parameters.
f. The ________ of an identifier is the portion of the program in
which the identifier can be used.
g. The three ways to return control from a called function to a caller
are ________, ________ and ________.
h. A(n)________ allows the compiler to check the number, types and
order of the arguments passed to a function.
i. Function ________ is used to produce random numbers.
j. Function ________ is used to set the random number seed to
randomize a program.
k. The storage-class specifiers are mutable, ________, ________,
________ and ________.
l. Variables declared in a block or in the parameter list of a function
are assumed to be of storage class ________ unless specified
otherwise.
m. Storage-class specifier ________ is a recommendation to the
compiler to store a variable in one of the computer's registers.
n. A variable declared outside any block or function is a(n) ________
variable.
o. For a local variable in a function to retain its value between calls
to the function, it must be declared with the ________ storageclass specifier.
p. The six possible scopes of an identifier are ________, ________,
________, ________, ________ and ________.
q. A function that calls itself either directly or indirectly (i.e., through
another function) is a(n) ________ function.
r. A recursive function typically has two components: One that
provides a means for the recursion to terminate by testing for a(n)
________ case and one that expresses the problem as a recursive
s.
t.
u.
v.
6.2
call for a slightly simpler problem than the original call.
In C++, it is possible to have various functions with the same
name that operate on different types or numbers of arguments.
This is called function ________.
The ________ enables access to a global variable with the same
name as a variable in the current scope.
The ________ qualifier is used to declare read-only variables.
A function ________ enables a single function to be defined to
perform a task on many different data types.
For the program in Fig. 6.40, state the scope (either function scope, file
scope, block scope or function-prototype scope) of each of the following
elements:
a.
b.
c.
d.
e.
f.
The variable x in main.
The variable y in cube.
The function cube.
The function main.
The function prototype for cube.
The identifier y in the function prototype for cube.
Figure 6.40. Program for Exercise 6.2
1 // Exercise 6.2: Ex06_02.cpp
2 #include <iostream>
3 using std::cout;
4 using std::endl;
5
6 int cube( int y ); // function prototype
7
8 int main()
9 {
10
int x;
11
12
for ( x = 1; x <= 10; x++ ) // loop 10 times
13
cout << cube( x ) << endl; // calculate
cube of x and output results
14
15
return 0; // indicates successful termination
16 } // end main
17
18 // definition of function cube
19 int cube( int y )
20 {
21
return y * y * y;
22 } // end function cube
6.3
Write a program that tests whether the examples of the math library
function calls shown in Fig. 6.2 actually produce the indicated results.
6.4
Give the function header for each of the following functions:
a. Function hypotenuse that takes two double-precision, floatingpoint arguments, side1 and side2, and returns a double-precision,
floating-point result.
b. Function smallest that takes three integers, x, y and z, and
returns an integer.
c. Function instructions that does not receive any arguments and
does not return a value. [Note: Such functions are commonly used
to display instructions to a user.]
d. Function intToDouble that takes an integer argument, number,
and returns a double-precision, floating-point result.
6.5
Give the function prototype for each of the following:
a.
b.
c.
d.
6.6
The function described in Exercise 6.4(a).
The function described in Exercise 6.4(b).
The function described in Exercise 6.4(c).
The function described in Exercise 6.4(d).
Write a declaration for each of the following:
a. Integer count that should be maintained in a register. Initialize
count to 0.
b. Double-precision, floating-point variable lastVal that is to retain
its value between calls to the function in which it is defined.
6.7
Find the error in each of the following program segments, and explain
how the error can be corrected (see also Exercise 6.53):
a.
{
int g( void )
cout << "Inside function g" << endl;
int h( void )
{
cout << "Inside function h" << endl;
}
}
b.
{
int sum( int x, int y )
int result;
result = x + y;
}
c.
{
int sum( int n )
if ( n == 0 )
return 0;
else
n + sum( n - 1 );
}
d.
{
void f ( double a);
float a;
cout << a << endl;
}
e.
{
void product( void )
int a;
int b;
int c;
int result;
cout << "Enter three integers: ";
cin >> a >> b >> c;
result = a * b * c;
cout << "Result is " << result;
return result;
}
6.8
Why would a function prototype contain a parameter type declaration
such as double &?
6.9
(True/False) All arguments to function calls in C++ are passed by value.
6.10
Write a complete program that prompts the user for the radius of a sphere,
and calculates and prints the volume of that sphere. Use an inline
function sphereVolume that returns the result of the following expression:
( 4.0 / 3.0 ) * 3.14159 * pow( radius, 3 ).
[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) functionprototype scope.
6.3
See the following program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Exercise 6.3: Ex06_03.cpp
// Testing the math library functions.
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include <cmath>
using namespace std;
int main()
{
cout << fixed << setprecision( 1 );
cout << "sqrt(" << 900.0 << ") = " << sqrt( 900.0 )
<< "\nsqrt(" << 9.0 << ") = " << sqrt( 9.0 );
cout << "\nexp(" << 1.0 << ") = " << setprecision( 6 )
<< exp( 1.0 ) << "\nexp(" << setprecision( 1 ) << 2.0
<< ") = " << setprecision( 6 ) << exp( 2.0 );
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
cout << "\nlog(" << 2.718282 << ") = " << setprecision( 1 )
<< log( 2.718282 )
<< "\nlog(" << setprecision( 6 ) << 7.389056 << ") = "
<< setprecision( 1 ) << log( 7.389056 );
cout << "\nlog10(" << 1.0 << ") = " << log10( 1.0 )
<< "\nlog10(" << 10.0 << ") = " << log10( 10.0 )
<< "\nlog10(" << 100.0 << ") = " << log10( 100.0 );
cout << "\nfabs(" << 13.5 << ") = " << fabs( 13.5 )
<< "\nfabs(" << 0.0 << ") = " << fabs( 0.0 )
<< "\nfabs(" << -13.5 << ") = " << fabs( -13.5 );
cout << "\nceil(" << 9.2 << ") = " << ceil( 9.2 )
<< "\nceil(" << -9.8 << ") = " << ceil( -9.8 );
cout << "\nfloor(" << 9.2 << ") = " << floor( 9.2 )
<< "\nfloor(" << -9.8 << ") = " << floor( -9.8 );
cout << "\npow(" << 2.0 << ", " << 7.0 << ") = "
<< pow( 2.0, 7.0 ) << "\npow(" << 9.0 << ", "
<< 0.5 << ") = " << pow( 9.0, 0.5 );
cout << setprecision(3) << "\nfmod("
<< 13.675 << ", " << 2.333 << ") = "
<< fmod( 13.675, 2.333) << setprecision( 1 );
cout << "\nsin(" << 0.0 << ") = " << sin( 0.0 );
cout << "\ncos(" << 0.0 << ") = " << cos( 0.0 );
cout << "\ntan(" << 0.0 << ") = " << tan( 0.0 ) << endl;
return 0; // indicates successful termination
} // 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
a.
double hypotenuse( double side1, double side2 )
b. int smallest( int x, int y, int z )
c. void instructions( void ) // in C++ (void) can be written ()
d. double intToDouble( int number )
6.5
a.
b.
c.
d.
6.6
a. register int count = 0;
b. static double lastVal;
6.7
a. Error: Function h is defined in function g.
double hypotenuse( double, double );
int smallest( int, int, int );
void instructions( void ); // in C++ (void) can be written ()
double intToDouble( int );
Correction: Move the definition of h out of the definition of g.
b. 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;
c. 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 );
d. 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;.
e. 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
6.10
False. C++ enables pass-by-reference using reference parameters (and pointers, as we
discuss in Chapter 8).
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
Download