![]()
// ex03_02.cpp
#include <iostream>
using std::cout;
using std::endl;
int cube( int y );
int main()
{
int x;
for ( x = 1; x <= 10; x++ )
cout << cube(
x
) << endl;
return 0;
}
int cube( int y )
{
return y * y * y;
}
![]()
// ex03_03.cpp
// Testing the math library func
#include <iostream>
using std::cout;
using std::endl;
using std::ios;
#include <iomanip>
using std::setiosflags;
using std::fixed;
using std::setprecision;
#include <cmath>
int main()
{
cout << setiosflags( ios::fixed
|
ios::showpoint )
<<
setprecision(
1 )
<<
"sqrt("
<< 900.0 << ") = " << sqrt( 900.0 )
<<
"\nsqrt("
<< 9.0 << ") = " << sqrt( 9.0 )
<<
"\nexp("
<< 1.0 << ") = " << setprecision( 6 )
<<
exp(
1.0 ) << "\nexp(" << setprecision( 1 ) << 2.0
<<
")
= " << setprecision( 6 ) << exp( 2.0 )
<<
"\nlog("
<< 2.718282 << ") = " << setprecision( 1 )
<<
log(
2.718282 ) << "\nlog(" << setprecision( 6 )
<<
7.389056
<< ") = " << setprecision( 1 )
<<
log(
7.389056 ) << endl;
cout << "log10(" << 1.0
<<
") = " << log10( 1.0 )
<<
"\nlog10("
<< 10.0 << ") = " << log10( 10.0 )
<<
"\nlog10("
<< 100.0 << ") = " << log10( 100.0 )
<<
"\nfabs("
<< 13.5 << ") = " << fabs( 13.5 )
<<
"\nfabs("
<< 0.0 << ") = " << fabs( 0.0 )
<<
"\nfabs("
<< -13.5 << ") = " << fabs( -13.5 ) << endl;
cout << "ceil(" << 9.2
<<
") = " << ceil( 9.2 )
<<
"\nceil("
<< -9.8 << ") = " << ceil( -9.8 )
<<
"\nfloor("
<< 9.2 << ") = " << floor( 9.2 )
<<
"\nfloor("
<< -9.8 << ") = " << floor( -9.8 ) << endl;
cout << "pow(" << 2.0
<<
", " << 7.0 << ") = "
<<
pow(
2.0, 7.0 ) << "\npow(" << 9.0 << ", "
<<
0.5
<< ") = " << pow( 9.0, 0.5 )
<<
setprecision(3)
<< "\nfmod("
<<
13.675
<< ", " << 2.333 << ") = "
<< fmod( 13.675, 2.333 ) <<
setprecision(
1 )
<<
"\nsin("
<< 0.0 << ") = " << sin( 0.0 )
<<
"\ncos("
<< 0.0 << ") = " << cos( 0.0 )
<<
"\ntan("
<< 0.0 << ") = " << tan( 0.0 ) << endl;
return 0;
}
![]()
// ex03_10.cpp
// Inline function that calculates the volume of a
sphere
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
const double PI = 3.14159;
inline double sphereVolume( const double r )
{ return 4.0 / 3.0 * PI * r * r * r; }
int main()
{
double radius;
cout << "Enter the length of
the radius
of your sphere: ";
cin >> radius;
cout << "Volume of sphere with
radius
" << radius <<
" is " << sphereVolume( radius ) << endl;
return 0;
}
![]()
// ex03_49.cpp
#include <iostream>
using std::cin;
using std::cout;
int main()
{
int c;
if ( ( c = cin.get() ) != EOF ) {
main();
cout << c;
}
return 0;
}
![]()
// ex03_50.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int mystery( int, int );
int main()
{
int x, y;
cout << "Enter two integers:
";
cin >> x >> y;
cout << "The result is "
<<
mystery( x, y ) << endl;
return 0;
}
// Parameter b must be a positive
// integer to prevent infinite recursion
int mystery( int a, int b )
{
if ( b == 1 )
return a;
else
return a + mystery(
a,
b - 1 );
}
![]()
// Fig. 3.3: fig03_03.cpp
// Creating and using a programmer-defined function
#include <iostream>
using std::cout;
using std::endl;
int square( int ); // function prototype
int main()
{
for ( int x = 1; x <= 10; x++ )
cout <<
square(
x ) << " ";
cout << endl;
return 0;
}
// Function definition
int square( int y )
{
return y * y;
}
![]()
// Fig. 3.4: fig03_04.cpp
// Finding the maximum of three integers
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int maximum( int, int, int ); // function prototype
int main()
{
int a, b, c;
cout << "Enter three
integers: ";
cin >> a >> b >> c;
// a, b and c below are arguments to
// the maximum function call
cout << "Maximum is: " <<
maximum(
a, b, c ) << endl;
return 0;
}
// Function maximum definition
// x, y and z below are parameters to
// the maximum function definition
int maximum( int x, int y, int z )
{
int max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max;
}
![]()
// Fig. 3.7: fig03_07.cpp
// Shifted, scaled integers produced by 1 + rand()
%
6
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstdlib>
int main()
{
for ( int i = 1; i <= 20; i++ ) {
cout << setw(
10
) << ( 1 + rand() % 6 );
if ( i % 5 == 0 )
cout
<< endl;
}
return 0;
}
![]()
// Fig. 3.8: fig03_08.cpp
// Roll a six-sided die 6000 times
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstdlib>
int main()
{
int frequency1 = 0, frequency2 = 0,
frequency3 =
0,
frequency4 = 0,
frequency5 =
0,
frequency6 = 0,
face;
for ( int roll = 1; roll <=
6000; roll++
) {
face = 1 + rand() %
6;
switch ( face ) {
case
1:
++frequency1;
break;
case
2:
++frequency2;
break;
case
3:
++frequency3;
break;
case
4:
++frequency4;
break;
case
5:
++frequency5;
break;
case
6:
++frequency6;
break;
default:
cout << "should never get here!";
}
}
cout << "Face" << setw(
13 )
<< "Frequency"
<<
"\n
1" << setw( 13 ) << frequency1
<<
"\n
2" << setw( 13 ) << frequency2
<<
"\n
3" << setw( 13 ) << frequency3
<<
"\n
4" << setw( 13 ) << frequency4
<<
"\n
5" << setw( 13 ) << frequency5
<<
"\n
6" << setw( 13 ) << frequency6 << endl;
return 0;
}
![]()
// Fig. 3.9: fig03_09.cpp
// Randomizing die-rolling program
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstdlib>
int main()
{
unsigned seed;
cout << "Enter seed: ";
cin >> seed;
srand( seed );
for ( int i = 1; i <= 10; i++ ) {
cout << setw(
10
) << 1 + rand() % 6;
if ( i % 5 == 0 )
cout
<< endl;
}
return 0;
}
![]()
// Fig. 3.10: fig03_10.cpp
// Craps
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
#include <ctime>
using std::time;
int rollDice( void ); // function prototype
int main()
{
enum Status { CONTINUE, WON, LOST };
int sum, myPoint;
Status gameStatus;
srand( time( 0 ) );
sum =
rollDice();
// first roll of the dice
switch ( sum ) {
case 7:
case
11:
// win on first roll
gameStatus
= WON;
break;
case 2:
case 3:
case
12:
// lose on first roll
gameStatus
= LOST;
break;
default:
// remember point
gameStatus
= CONTINUE;
myPoint
= sum;
cout
<< "Point is " << myPoint << endl;
break;
// optional
}
while ( gameStatus == CONTINUE )
{
// keep rolling
sum = rollDice();
if ( sum ==
myPoint )
// win by making point
gameStatus
= WON;
else
if
(
sum == 7 ) //
lose
by rolling 7
gameStatus = LOST;
}
if ( gameStatus == WON )
cout <<
"Player
wins" << endl;
else
cout <<
"Player
loses" << endl;
return 0;
}
int rollDice( void )
{
int die1, die2, workSum;
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
workSum = die1 + die2;
cout << "Player rolled "
<<
die1 << " + " << die2
<<
"
= " << workSum << endl;
return workSum;
}
![]()
// Fig. 3.12: fig03_12.cpp
// A scoping example
#include <iostream>
using std::cout;
using std::endl;
void a( void ); // function prototype
void b( void ); // function prototype
void c( void ); // function prototype
int x = 1; // global variable
int main()
{
int x = 5; // local
variable
to main
cout << "local x in outer scope of main is " << x << endl;
{
// start new scope
int x = 7;
cout <<
"local x
in inner scope of main is " << x << endl;
}
// end new scope
cout << "local x in outer scope of main is " << x << endl;
a();
// a has automatic local x
b();
// b has static local x
c();
// c uses global x
a();
// a reinitializes automatic local x
b();
// static local x retains its previous value
c();
// global x also retains its value
cout << "local x in main is " << x << endl;
return 0;
}
void a( void )
{
int x = 25; // initialized each
time
a is called
cout << endl << "local
x in a
is " << x
<<
"
after entering a" << endl;
++x;
cout << "local x in a is "
<<
x
<<
"
before exiting a" << endl;
}
void b( void )
{
static int x = 50; //
Static
initialization only
// first time b is called.
cout << endl <<
"local
static x is " << x
<<
" on entering b" << endl;
++x;
cout << "local static x is
"
<< x
<<
" on exiting b" << endl;
}
void c( void )
{
cout << endl << "global x
is
" << x
<<
"
on entering c" << endl;
x *= 10;
cout << "global x is " <<
x
<< " on exiting c" << endl;
}
![]()
// Fig. 3.14: fig03_14.cpp
// Recursive factorial function
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
unsigned long factorial( unsigned long );
int main()
{
for ( int i = 0; i <= 10; i++ )
cout << setw(
2
) << i << "! = " << factorial( i ) << endl;
return 0;
}
// Recursive definition of function factorial
unsigned long factorial( unsigned long number )
{
if ( number <= 1 ) // base
case
return 1;
else
// recursive case
return number *
factorial(
number - 1 );
}
![]()
// Fig. 3.15: fig03_15.cpp
// Recursive fibonacci function
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
unsigned long fibonacci( unsigned long );
int main()
{
unsigned long result, number;
cout << "Enter an
integer: ";
cin >> number;
result = fibonacci( number );
cout << "Fibonacci("
<<
number << ") = " << result << endl;
return 0;
}
// Recursive definition of function fibonacci
unsigned long fibonacci( unsigned long n )
{
if ( n == 0 || n == 1 ) // base
case
return n;
else
// recursive case
return fibonacci( n
-
1 ) + fibonacci( n - 2 );
}
![]()
// Fig. 3.18: fig03_18.cpp
// Functions that take no arguments
#include <iostream>
using std::cout;
using std::endl;
void function1();
void function2( void );
int main()
{
function1();
function2();
return 0;
}
void function1()
{
cout << "function1 takes no
arguments"
<< endl;
}
void function2( void )
{
cout << "function2 also takes no
arguments"
<< endl;
}
![]()
// Fig. 3.19: fig03_19.cpp
// Using an inline function to calculate
// the volume of a cube.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
inline double cube( const double s ) { return s * s * s; }
int main()
{
cout << "Enter the side length
of
your cube: ";
double side;
cin >> side;
cout << "Volume of cube with
side
"
<<
side
<< " is " << cube( side ) << endl;
return 0;
}
![]()
// Fig. 3.20: fig03_20.cpp
// Comparing call-by-value and call-by-reference
// with references.
#include <iostream>
using std::cout;
using std::endl;
int squareByValue( int );
void squareByReference( int & );
int main()
{
int x = 2, z = 4;
cout << "x = " << x
<<
" before squareByValue\n"
<<
"Value
returned by squareByValue: "
<<
squareByValue(
x ) << endl
<<
"x
= " << x << " after squareByValue\n" << endl;
cout << "z = " << z
<<
" before squareByReference" << endl;
squareByReference( z );
cout << "z = " << z
<<
" after squareByReference" << endl;
return 0;
}
int squareByValue( int a )
{
return a *= a; // caller's
argument
not modified
}
void squareByReference( int &cRef )
{
cRef *= cRef; //
caller's
argument modified
}
![]()
// Fig. 3.21: fig03_21.cpp
// References must be initialized
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int x = 3, &y = x; // y is
now
an alias for x
cout << "x = " << x
<<
endl << "y = " << y << endl;
y = 7;
cout << "x = " << x
<<
endl << "y = " << y << endl;
return 0;
}
![]()
// Fig. 3.22: fig03_22.cpp
// References must be initialized
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int x = 3, &y; //
Error:
y must be initialized
cout << "x = " << x
<<
endl << "y = " << y << endl;
y = 7;
cout << "x = " << x
<<
endl << "y = " << y << endl;
return 0;
}
![]()
// Fig. 3.23: fig03_23.cpp
// Using default arguments
#include <iostream>
using std::cout;
using std::endl;
int boxVolume( int length = 1, int width = 1, int height = 1 );
int main()
{
cout << "The default box volume
is:
" << boxVolume()
<< "\n\nThe volume
of
a box with length 10,\n"
<<
"width
1 and height 1 is: " << boxVolume( 10 )
<<
"\n\nThe
volume of a box with length 10,\n"
<<
"width
5 and height 1 is: " << boxVolume( 10, 5 )
<<
"\n\nThe
volume of a box with length 10,\n"
<<
"width
5 and height 2 is: " << boxVolume( 10, 5, 2 )
<<
endl;
return 0;
}
// Calculate the volume of a box
int boxVolume( int length, int width, int height )
{
return length * width * height;
}
![]()
// Fig. 3.24: fig03_24.cpp
// Using the unary scope resolution operator
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setprecision;
const double PI = 3.14159265358979;
int main()
{
const float PI = static_cast< float
>(
::PI );
cout << setprecision( 20 )
<<
"
Local float value of PI = " << PI
<<
"\nGlobal
double value of PI = " << ::PI << endl;
return 0;
}
![]()
// Fig. 3.25: fig03_25.cpp
// Using overloaded functions
#include <iostream>
using std::cout;
using std::endl;
int square( int x ) { return x * x; }
double square( double y ) { return y * y; }
int main()
{
cout << "The square of integer 7
is
" << square( 7 )
<<
"\nThe
square of double 7.5 is " << square( 7.5 )
<<
endl;
return 0;
}
![]()
// Fig. 3.26: fig03_26.cpp
// Name mangling
int square( int x ) { return x * x; }
double square( double y ) { return y * y; }
void nothing1( int a, float b, char c, int *d )
{ } // empty function body
char *nothing2( char a, int b, float *c, double
*d )
{ return 0; }
int main()
{
return 0;
}
![]()
// Fig. 3.27: fig03_27.cpp
// Using a function template
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
template < class T >
T maximum( T value1, T value2, T value3 )
{
T max = value1;
if ( value2 > max )
max = value2;
if ( value3 > max )
max = value3;
return max;
}
int main()
{
int int1, int2, int3;
cout << "Input three integer
values:
";
cin >> int1 >> int2
>> int3;
cout << "The maximum integer
value
is: "
<<
maximum(
int1, int2, int3
);
// int version
double double1, double2, double3;
cout << "\nInput three double
values:
";
cin >> double1 >> double2
>> double3;
cout << "The maximum double
value
is: "
<<
maximum(
double1, double2, double3 ); // double version
char char1, char2, char3;
cout << "\nInput three
characters:
";
cin >> char1 >> char2
>> char3;
cout << "The maximum character
value
is: "
<<
maximum(
char1, char2, char3 ) // char
version
<<
endl;
return 0;
}