From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog1/examples/ch03/Ex03_02.cpp | 41 +++++++++++ Bachelor/Prog1/examples/ch03/Ex03_03.cpp | 67 ++++++++++++++++++ Bachelor/Prog1/examples/ch03/Ex03_10.cpp | 53 ++++++++++++++ Bachelor/Prog1/examples/ch03/Ex03_49.cpp | 36 ++++++++++ Bachelor/Prog1/examples/ch03/Ex03_50.cpp | 51 ++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_03.cpp | 45 ++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_04.cpp | 60 ++++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_07.cpp | 46 +++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_08.cpp | 89 ++++++++++++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_09.cpp | 54 +++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_10.cpp | 111 ++++++++++++++++++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_12.cpp | 96 ++++++++++++++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_14.cpp | 52 ++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_15.cpp | 56 +++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_18.cpp | 50 ++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_19.cpp | 50 ++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_20.cpp | 61 ++++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_21.cpp | 37 ++++++++++ Bachelor/Prog1/examples/ch03/Fig03_22.cpp | 35 ++++++++++ Bachelor/Prog1/examples/ch03/Fig03_23.cpp | 54 +++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_24.cpp | 44 ++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_25.cpp | 50 ++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_26.cpp | 50 ++++++++++++++ Bachelor/Prog1/examples/ch03/Fig03_27.cpp | 76 ++++++++++++++++++++ 24 files changed, 1364 insertions(+) create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_02.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_03.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_10.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_49.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_50.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_03.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_04.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_07.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_08.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_09.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_10.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_12.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_14.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_15.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_18.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_19.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_20.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_21.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_22.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_23.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_24.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_25.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_26.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_27.cpp (limited to 'Bachelor/Prog1/examples/ch03') diff --git a/Bachelor/Prog1/examples/ch03/Ex03_02.cpp b/Bachelor/Prog1/examples/ch03/Ex03_02.cpp new file mode 100644 index 0000000..cb1f501 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_02.cpp @@ -0,0 +1,41 @@ +// Exercise 3.2: ex03_02.cpp +#include + +using std::cout; +using std::endl; + +int cube( int y ); // function prototype + +int main() +{ + int x; + + // loop 10 times, calculate cube of x and output results + for ( x = 1; x <= 10; x++ ) + cout << cube( x ) << endl; + + return 0; // indicates successful termination + +} // end main + +// definition of function cube +int cube( int y ) +{ + return y * y * y; +} + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_03.cpp b/Bachelor/Prog1/examples/ch03/Ex03_03.cpp new file mode 100644 index 0000000..af9b320 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_03.cpp @@ -0,0 +1,67 @@ +// Exercise 3.3: ex03_03.cpp +// Testing the math library functions. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include + +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 ); + 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 + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_10.cpp b/Bachelor/Prog1/examples/ch03/Ex03_10.cpp new file mode 100644 index 0000000..466ad86 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_10.cpp @@ -0,0 +1,53 @@ +// Exercise 3.10: ex03_10.cpp +// Inline function that calculates the volume of a sphere. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +// define global constant PI +const double PI = 3.14159; + +// calculates volume of a sphere +inline double sphereVolume( const double radius ) +{ + return 4.0 / 3.0 * PI * pow( radius, 3 ); + +} // end inline function sphereVolume + +int main() +{ + double radiusValue; + + // prompt user for radius + cout << "Enter the length of the radius of your sphere: "; + cin >> radiusValue; // input radius + + // use radiusValue to calculate volume of sphere + // and display result + cout << "Volume of sphere with radius " << radiusValue + << " is " << sphereVolume( radiusValue ) << endl; + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_49.cpp b/Bachelor/Prog1/examples/ch03/Ex03_49.cpp new file mode 100644 index 0000000..faaf62f --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_49.cpp @@ -0,0 +1,36 @@ +// Exercise 3.49: ex03_49.cpp +// What is wrong with this program? +#include + +using std::cin; +using std::cout; + +int main() +{ + int c; + + if ( ( c = cin.get() ) != EOF ) { + main(); + cout << c; + } + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_50.cpp b/Bachelor/Prog1/examples/ch03/Ex03_50.cpp new file mode 100644 index 0000000..c887d3d --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_50.cpp @@ -0,0 +1,51 @@ +// Exercise 3.50: ex03_50.cpp +// What does this program do? +#include + +using std::cout; +using std::cin; +using std::endl; + +int mystery( int, int ); // function prototype + +int main() +{ + int x, y; + + cout << "Enter two integers: "; + cin >> x >> y; + cout << "The result is " << mystery( x, y ) << endl; + + return 0; // indicates successful termination + +} // end main + +// Parameter b must be a positive +// integer to prevent infinite recursion +int mystery( int a, int b ) +{ + // base case + if ( b == 1 ) + return a; + + // recursive step + else + return a + mystery( a, b - 1 ); + +} // end function mystery + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_03.cpp b/Bachelor/Prog1/examples/ch03/Fig03_03.cpp new file mode 100644 index 0000000..271802e --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_03.cpp @@ -0,0 +1,45 @@ +// Fig. 3.3: fig03_03.cpp +// Creating and using a programmer-defined function. +#include + +using std::cout; +using std::endl; + +int square( int ); // function prototype + +int main() +{ + // loop 10 times and calculate and output + // square of x each time + for ( int x = 1; x <= 10; x++ ) + cout << square( x ) << " "; // function call + + cout << endl; + + return 0; // indicates successful termination + +} // end main + +// square function definition returns square of an integer +int square( int y ) // y is a copy of argument to function +{ + return y * y; // returns square of y as an int + +} // end function square + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_04.cpp b/Bachelor/Prog1/examples/ch03/Fig03_04.cpp new file mode 100644 index 0000000..d9e27f3 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_04.cpp @@ -0,0 +1,60 @@ +// Fig. 3.4: fig03_04.cpp +// Finding the maximum of three floating-point numbers. +#include + +using std::cout; +using std::cin; +using std::endl; + +double maximum( double, double, double ); // function prototype + +int main() +{ + double number1; + double number2; + double number3; + + cout << "Enter three floating-point numbers: "; + cin >> number1 >> number2 >> number3; + + // number1, number2 and number3 are arguments to + // the maximum function call + cout << "Maximum is: " + << maximum( number1, number2, number3 ) << endl; + + return 0; // indicates successful termination + +} // end main + +// function maximum definition; +// x, y and z are parameters +double maximum( double x, double y, double z ) +{ + double max = x; // assume x is largest + + if ( y > max ) // if y is larger, + max = y; // assign y to max + + if ( z > max ) // if z is larger, + max = z; // assign z to max + + return max; // max is largest value + +} // end function maximum + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_07.cpp b/Bachelor/Prog1/examples/ch03/Fig03_07.cpp new file mode 100644 index 0000000..4971cc4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_07.cpp @@ -0,0 +1,46 @@ +// Fig. 3.7: fig03_07.cpp +// Shifted, scaled integers produced by 1 + rand() % 6. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // contains function prototype for rand + +int main() +{ + // loop 20 times + for ( int counter = 1; counter <= 20; counter++ ) { + + // pick random number from 1 to 6 and output it + cout << setw( 10 ) << ( 1 + rand() % 6 ); + + // if counter divisible by 5, begin new line of output + if ( counter % 5 == 0 ) + cout << endl; + + } // end for structure + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch03/Fig03_08.cpp b/Bachelor/Prog1/examples/ch03/Fig03_08.cpp new file mode 100644 index 0000000..8bb489e --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_08.cpp @@ -0,0 +1,89 @@ +// Fig. 3.8: fig03_08.cpp +// Roll a six-sided die 6000 times. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // contains function prototype for rand + +int main() +{ + int frequency1 = 0; + int frequency2 = 0; + int frequency3 = 0; + int frequency4 = 0; + int frequency5 = 0; + int frequency6 = 0; + int face; // represents one roll of the die + + // loop 6000 times and summarize results + for ( int roll = 1; roll <= 6000; roll++ ) { + face = 1 + rand() % 6; // random number from 1 to 6 + + // determine face value and increment appropriate counter + switch ( face ) { + + case 1: // rolled 1 + ++frequency1; + break; + + case 2: // rolled 2 + ++frequency2; + break; + + case 3: // rolled 3 + ++frequency3; + break; + + case 4: // rolled 4 + ++frequency4; + break; + + case 5: // rolled 5 + ++frequency5; + break; + + case 6: // rolled 6 + ++frequency6; + break; + + default: // invalid value + cout << "Program should never get here!"; + + } // end switch + + } // end for + + // display results in tabular format + 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; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_09.cpp b/Bachelor/Prog1/examples/ch03/Fig03_09.cpp new file mode 100644 index 0000000..5ea5fb6 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_09.cpp @@ -0,0 +1,54 @@ +// Fig. 3.9: fig03_09.cpp +// Randomizing die-rolling program. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::setw; + +// contains prototypes for functions srand and rand +#include + +int main() +{ + unsigned seed; + + cout << "Enter seed: "; + cin >> seed; + srand( seed ); // seed random number generator + + // loop 10 times + for ( int counter = 1; counter <= 10; counter++ ) { + + // pick random number from 1 to 6 and output it + cout << setw( 10 ) << ( 1 + rand() % 6 ); + + // if counter divisible by 5, begin new line of output + if ( counter % 5 == 0 ) + cout << endl; + + } // end for + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_10.cpp b/Bachelor/Prog1/examples/ch03/Fig03_10.cpp new file mode 100644 index 0000000..5ebbd59 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_10.cpp @@ -0,0 +1,111 @@ +// Fig. 3.10: fig03_10.cpp +// Craps. +#include + +using std::cout; +using std::endl; + +// contains function prototypes for functions srand and rand +#include + +#include // contains prototype for function time + +int rollDice( void ); // function prototype + +int main() +{ + // enumeration constants represent game status + enum Status { CONTINUE, WON, LOST }; + + int sum; + int myPoint; + + Status gameStatus; // can contain CONTINUE, WON or LOST + + // randomize random number generator using current time + srand( time( 0 ) ); + + sum = rollDice(); // first roll of the dice + + // determine game status and point based on sum of dice + switch ( sum ) { + + // win on first roll + case 7: + case 11: + gameStatus = WON; + break; + + // lose on first roll + case 2: + case 3: + case 12: + gameStatus = LOST; + break; + + // remember point + default: + gameStatus = CONTINUE; + myPoint = sum; + cout << "Point is " << myPoint << endl; + break; // optional + + } // end switch + + // while game not complete ... + while ( gameStatus == CONTINUE ) { + sum = rollDice(); // roll dice again + + // determine game status + if ( sum == myPoint ) // win by making point + gameStatus = WON; + else + if ( sum == 7 ) // lose by rolling 7 + gameStatus = LOST; + + } // end while + + // display won or lost message + if ( gameStatus == WON ) + cout << "Player wins" << endl; + else + cout << "Player loses" << endl; + + return 0; // indicates successful termination + +} // end main + +// roll dice, calculate sum and display results +int rollDice( void ) +{ + int die1; + int die2; + int workSum; + + die1 = 1 + rand() % 6; // pick random die1 value + die2 = 1 + rand() % 6; // pick random die2 value + workSum = die1 + die2; // sum die1 and die2 + + // display results of this roll + cout << "Player rolled " << die1 << " + " << die2 + << " = " << workSum << endl; + + return workSum; // return sum of dice + +} // end function rollDice + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_12.cpp b/Bachelor/Prog1/examples/ch03/Fig03_12.cpp new file mode 100644 index 0000000..593c27a --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_12.cpp @@ -0,0 +1,96 @@ +// Fig. 3.12: fig03_12.cpp +// A scoping example. +#include + +using std::cout; +using std::endl; + +void useLocal( void ); // function prototype +void useStaticLocal( void ); // function prototype +void useGlobal( void ); // function prototype + +int x = 1; // global variable + +int main() +{ + int x = 5; // local variable to main + + cout << "local x in main's outer scope is " << x << endl; + + { // start new scope + + int x = 7; + + cout << "local x in main's inner scope is " << x << endl; + + } // end new scope + + cout << "local x in main's outer scope is " << x << endl; + + useLocal(); // useLocal has local x + useStaticLocal(); // useStaticLocal has static local x + useGlobal(); // useGlobal uses global x + useLocal(); // useLocal reinitializes its local x + useStaticLocal(); // static local x retains its prior value + useGlobal(); // global x also retains its value + + cout << "\nlocal x in main is " << x << endl; + + return 0; // indicates successful termination + +} // end main + +// useLocal reinitializes local variable x during each call +void useLocal( void ) +{ + int x = 25; // initialized each time useLocal is called + + cout << endl << "local x is " << x + << " on entering useLocal" << endl; + ++x; + cout << "local x is " << x + << " on exiting useLocal" << endl; + +} // end function useLocal + +// useStaticLocal initializes static local variable x only the +// first time the function is called; value of x is saved +// between calls to this function +void useStaticLocal( void ) +{ + // initialized first time useStaticLocal is called. + static int x = 50; + + cout << endl << "local static x is " << x + << " on entering useStaticLocal" << endl; + ++x; + cout << "local static x is " << x + << " on exiting useStaticLocal" << endl; + +} // end function useStaticLocal + +// useGlobal modifies global variable x during each call +void useGlobal( void ) +{ + cout << endl << "global x is " << x + << " on entering useGlobal" << endl; + x *= 10; + cout << "global x is " << x + << " on exiting useGlobal" << endl; + +} // end function useGlobal + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_14.cpp b/Bachelor/Prog1/examples/ch03/Fig03_14.cpp new file mode 100644 index 0000000..ca4f128 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_14.cpp @@ -0,0 +1,52 @@ +// Fig. 3.14: fig03_14.cpp +// Recursive factorial function. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +unsigned long factorial( unsigned long ); // function prototype + +int main() +{ + // Loop 10 times. During each iteration, calculate + // factorial( i ) and display result. + for ( int i = 0; i <= 10; i++ ) + cout << setw( 2 ) << i << "! = " + << factorial( i ) << endl; + + return 0; // indicates successful termination + +} // end main + +// recursive definition of function factorial +unsigned long factorial( unsigned long number ) +{ + // base case + if ( number <= 1 ) + return 1; + + // recursive step + else + return number * factorial( number - 1 ); + +} // end function factorial + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_15.cpp b/Bachelor/Prog1/examples/ch03/Fig03_15.cpp new file mode 100644 index 0000000..3882d55 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_15.cpp @@ -0,0 +1,56 @@ +// Fig. 3.15: fig03_15.cpp +// Recursive fibonacci function. +#include + +using std::cout; +using std::cin; +using std::endl; + +unsigned long fibonacci( unsigned long ); // function prototype + +int main() +{ + unsigned long result, number; + + // obtain integer from user + cout << "Enter an integer: "; + cin >> number; + + // calculate fibonacci value for number input by user + result = fibonacci( number ); + + // display result + cout << "Fibonacci(" << number << ") = " << result << endl; + + return 0; // indicates successful termination + +} // end main + +// recursive definition of function fibonacci +unsigned long fibonacci( unsigned long n ) +{ + // base case + if ( n == 0 || n == 1 ) + return n; + + // recursive step + else + return fibonacci( n - 1 ) + fibonacci( n - 2 ); + +} // end function fibonacci + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_18.cpp b/Bachelor/Prog1/examples/ch03/Fig03_18.cpp new file mode 100644 index 0000000..9e5781c --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_18.cpp @@ -0,0 +1,50 @@ +// Fig. 3.18: fig03_18.cpp +// Functions that take no arguments. +#include + +using std::cout; +using std::endl; + +void function1(); // function prototype +void function2( void ); // function prototype + +int main() +{ + function1(); // call function1 with no arguments + function2(); // call function2 with no arguments + + return 0; // indicates successful termination + +} // end main + +// function1 uses an empty parameter list to specify that +// the function receives no arguments +void function1() +{ + cout << "function1 takes no arguments" << endl; + +} // end function1 + +// function2 uses a void parameter list to specify that +// the function receives no arguments +void function2( void ) +{ + cout << "function2 also takes no arguments" << endl; + +} // end function2 + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_19.cpp b/Bachelor/Prog1/examples/ch03/Fig03_19.cpp new file mode 100644 index 0000000..e785534 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_19.cpp @@ -0,0 +1,50 @@ +// Fig. 3.19: fig03_19.cpp +// Using an inline function to calculate. +// the volume of a cube. +#include + +using std::cout; +using std::cin; +using std::endl; + +// Definition of inline function cube. Definition of function +// appears before function is called, so a function prototype +// is not required. First line of function definition acts as +// the prototype. +inline double cube( const double side ) +{ + return side * side * side; // calculate cube + +} // end function cube + +int main() +{ + cout << "Enter the side length of your cube: "; + + double sideValue; + + cin >> sideValue; + + // calculate cube of sideValue and display result + cout << "Volume of cube with side " + << sideValue << " is " << cube( sideValue ) << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_20.cpp b/Bachelor/Prog1/examples/ch03/Fig03_20.cpp new file mode 100644 index 0000000..e94ca30 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_20.cpp @@ -0,0 +1,61 @@ +// Fig. 3.20: fig03_20.cpp +// Comparing pass-by-value and pass-by-reference +// with references. +#include + +using std::cout; +using std::endl; + +int squareByValue( int ); // function prototype +void squareByReference( int & ); // function prototype + +int main() +{ + int x = 2; + int z = 4; + + // demonstrate squareByValue + cout << "x = " << x << " before squareByValue\n"; + cout << "Value returned by squareByValue: " + << squareByValue( x ) << endl; + cout << "x = " << x << " after squareByValue\n" << endl; + + // demonstrate squareByReference + cout << "z = " << z << " before squareByReference" << endl; + squareByReference( z ); + cout << "z = " << z << " after squareByReference" << endl; + + return 0; // indicates successful termination +} // end main + +// squareByValue multiplies number by itself, stores the +// result in number and returns the new value of number +int squareByValue( int number ) +{ + return number *= number; // caller's argument not modified + +} // end function squareByValue + +// squareByReference multiplies numberRef by itself and +// stores the result in the variable to which numberRef +// refers in function main +void squareByReference( int &numberRef ) +{ + numberRef *= numberRef; // caller's argument modified + +} // end function squareByReference + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_21.cpp b/Bachelor/Prog1/examples/ch03/Fig03_21.cpp new file mode 100644 index 0000000..4e1af15 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_21.cpp @@ -0,0 +1,37 @@ +// Fig. 3.21: fig03_21.cpp +// References must be initialized. +#include + +using std::cout; +using std::endl; + +int main() +{ + int x = 3; + + // y refers to (is an alias for) x + int &y = x; + + cout << "x = " << x << endl << "y = " << y << endl; + y = 7; + cout << "x = " << x << endl << "y = " << y << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_22.cpp b/Bachelor/Prog1/examples/ch03/Fig03_22.cpp new file mode 100644 index 0000000..90ab6a9 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_22.cpp @@ -0,0 +1,35 @@ +// Fig. 3.22: fig03_22.cpp +// References must be initialized. +#include + +using std::cout; +using std::endl; + +int main() +{ + int x = 3; + int &y; // Error: y must be initialized + + cout << "x = " << x << endl << "y = " << y << endl; + y = 7; + cout << "x = " << x << endl << "y = " << y << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_23.cpp b/Bachelor/Prog1/examples/ch03/Fig03_23.cpp new file mode 100644 index 0000000..b3cffc5 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_23.cpp @@ -0,0 +1,54 @@ +// Fig. 3.23: fig03_23.cpp +// Using default arguments. +#include + +using std::cout; +using std::endl; + +// function prototype that specifies default arguments +int boxVolume( int length = 1, int width = 1, int height = 1 ); + +int main() +{ + // no arguments--use default values for all dimensions + cout << "The default box volume is: " << boxVolume(); + + // specify length; default width and height + cout << "\n\nThe volume of a box with length 10,\n" + << "width 1 and height 1 is: " << boxVolume( 10 ); + + // specify length and width; default height + cout << "\n\nThe volume of a box with length 10,\n" + << "width 5 and height 1 is: " << boxVolume( 10, 5 ); + + // specify all arguments + cout << "\n\nThe volume of a box with length 10,\n" + << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 ) + << endl; + + return 0; // indicates successful termination + +} // end main + +// function boxVolume calculates the volume of a box +int boxVolume( int length, int width, int height ) +{ + return length * width * height; + +} // end function boxVolume + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_24.cpp b/Bachelor/Prog1/examples/ch03/Fig03_24.cpp new file mode 100644 index 0000000..b1fa28b --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_24.cpp @@ -0,0 +1,44 @@ +// Fig. 3.24: fig03_24.cpp +// Using the unary scope resolution operator. +#include + +using std::cout; +using std::endl; + +#include + +using std::setprecision; + +// define global constant PI +const double PI = 3.14159265358979; + +int main() +{ + // define local constant PI + const float PI = static_cast< float >( ::PI ); + + // display values of local and global PI constants + cout << setprecision( 20 ) + << " Local float value of PI = " << PI + << "\nGlobal double value of PI = " << ::PI << endl; + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_25.cpp b/Bachelor/Prog1/examples/ch03/Fig03_25.cpp new file mode 100644 index 0000000..2539aee --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_25.cpp @@ -0,0 +1,50 @@ +// Fig. 3.25: fig03_25.cpp +// Using overloaded functions. +#include + +using std::cout; +using std::endl; + +// function square for int values +int square( int x ) +{ + cout << "Called square with int argument: " << x << endl; + return x * x; + +} // end int version of function square + +// function square for double values +double square( double y ) +{ + cout << "Called square with double argument: " << y << endl; + return y * y; + +} // end double version of function square + +int main() +{ + int intResult = square( 7 ); // calls int version + double doubleResult = square( 7.5 ); // calls double version + + cout << "\nThe square of integer 7 is " << intResult + << "\nThe square of double 7.5 is " << doubleResult + << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_26.cpp b/Bachelor/Prog1/examples/ch03/Fig03_26.cpp new file mode 100644 index 0000000..5b2fdec --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_26.cpp @@ -0,0 +1,50 @@ +// Fig. 3.26: fig03_26.cpp +// Name mangling. + +// function square for int values +int square( int x ) +{ + return x * x; +} + +// function square for double values +double square( double y ) +{ + return y * y; +} + +// function that receives arguments of types +// int, float, char and int * +void nothing1( int a, float b, char c, int *d ) +{ + // empty function body +} + +// function that receives arguments of types +// char, int, float * and double * +char *nothing2( char a, int b, float *c, double *d ) +{ + return 0; +} + +int main() +{ + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_27.cpp b/Bachelor/Prog1/examples/ch03/Fig03_27.cpp new file mode 100644 index 0000000..13a671f --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_27.cpp @@ -0,0 +1,76 @@ +// Fig. 3.27: fig03_27.cpp +// Using a function template. +#include + +using std::cout; +using std::cin; +using std::endl; + +// definition of function template maximum +template < class T > // or template< typename T > +T maximum( T value1, T value2, T value3 ) +{ + T max = value1; + + if ( value2 > max ) + max = value2; + + if ( value3 > max ) + max = value3; + + return max; + +} // end function template maximum + +int main() +{ + // demonstrate maximum with int values + int int1, int2, int3; + + cout << "Input three integer values: "; + cin >> int1 >> int2 >> int3; + + // invoke int version of maximum + cout << "The maximum integer value is: " + << maximum( int1, int2, int3 ); + + // demonstrate maximum with double values + double double1, double2, double3; + + cout << "\n\nInput three double values: "; + cin >> double1 >> double2 >> double3; + + // invoke double version of maximum + cout << "The maximum double value is: " + << maximum( double1, double2, double3 ); + + // demonstrate maximum with char values + char char1, char2, char3; + + cout << "\n\nInput three characters: "; + cin >> char1 >> char2 >> char3; + + // invoke char version of maximum + cout << "The maximum character value is: " + << maximum( char1, char2, char3 ) + << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ -- cgit v1.2.3