Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
| 6.1 |
| |
| 6.2 |
| |
| 6.3 | See the following program:
Code View:
Scroll
/
Show All 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
| |
| 6.4 |
| |
| 6.5 |
| |
| 6.6 |
| |
| 6.7 |
| |
| 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. | |
| 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:
Code View:
Scroll
/
Show All 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 |