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/ch02/Ex02_05.cpp | 43 ++++++++++++++ Bachelor/Prog1/examples/ch02/Ex02_08.cpp | 55 ++++++++++++++++++ Bachelor/Prog1/examples/ch02/Ex02_15.cpp | 44 ++++++++++++++ Bachelor/Prog1/examples/ch02/Ex02_24.cpp | 39 +++++++++++++ Bachelor/Prog1/examples/ch02/Ex02_25.cpp | 47 +++++++++++++++ Bachelor/Prog1/examples/ch02/Ex02_42.cpp | 47 +++++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_07.cpp | 54 ++++++++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_09.cpp | 76 +++++++++++++++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_11.cpp | 64 +++++++++++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_14.cpp | 44 ++++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_16.cpp | 38 +++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_17.cpp | 36 ++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_20.cpp | 38 +++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_21.cpp | 60 +++++++++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_22.cpp | 95 +++++++++++++++++++++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_24.cpp | 39 +++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_26.cpp | 46 +++++++++++++++ Bachelor/Prog1/examples/ch02/Fig02_27.cpp | 45 +++++++++++++++ 18 files changed, 910 insertions(+) create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_05.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_08.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_15.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_24.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_25.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_42.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_07.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_09.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_11.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_14.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_16.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_17.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_20.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_21.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_22.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_24.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_26.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_27.cpp (limited to 'Bachelor/Prog1/examples/ch02') diff --git a/Bachelor/Prog1/examples/ch02/Ex02_05.cpp b/Bachelor/Prog1/examples/ch02/Ex02_05.cpp new file mode 100644 index 0000000..1189989 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_05.cpp @@ -0,0 +1,43 @@ +// Ex. 2.5: ex02_05.cpp +// Calculate the sum of the integers from 1 to 10. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int sum; // stores sum of integers 1 to 10 + int x; // counter + + x = 1; // count from 1 + sum = 0; // initialize sum + + while ( x <= 10 ) { + sum += x; // add x to sum + ++x; // increment x + + } // end while + + cout << "The sum is: " << sum << endl; + + return 0; // indicate successful termination + +} // end function 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/ch02/Ex02_08.cpp b/Bachelor/Prog1/examples/ch02/Ex02_08.cpp new file mode 100644 index 0000000..db9685a --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_08.cpp @@ -0,0 +1,55 @@ +// Ex. 2.8: ex02_08.cpp +// Raise x to the y power. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + int x; // base + int y; // exponent + int i; // counts from 1 to y + int power; // used to calculate x raised to power y + + i = 1; // initialize i to begin counting from 1 + power = 1; // initialize power + + cout << "Enter base as an integer: "; // prompt for base + cin >> x; // input base + + // prompt for exponent + cout << "Enter exponent as an integer: "; + cin >> y; // input exponent + + // count from 1 to y and multiply power by x each time + while ( i <= y ) { + power *= x; + ++i; + + } // end while + + cout << power << endl; // display result + + return 0; // indicate successful termination + +} // end function 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/ch02/Ex02_15.cpp b/Bachelor/Prog1/examples/ch02/Ex02_15.cpp new file mode 100644 index 0000000..63e8e14 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_15.cpp @@ -0,0 +1,44 @@ +// Ex. 2.15: ex02_15.cpp +// What does this program print? +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int y; // declare y + int x = 1; // initialize x + int total = 0; // initialize total + + while ( x <= 10 ) { // loop 10 times + y = x * x; // perform calculation + cout << y << endl; // output result + total += y; // add y to total + ++x; // increment counter x + + } // end while + + cout << "Total is " << total << endl; // display result + + return 0; // indicate successful termination + +} // end function 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/ch02/Ex02_24.cpp b/Bachelor/Prog1/examples/ch02/Ex02_24.cpp new file mode 100644 index 0000000..3faf50c --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_24.cpp @@ -0,0 +1,39 @@ +// Ex. 2.24: ex02_24.cpp +// What does this program print? +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int count = 1; // initialize count + + while ( count <= 10 ) { // loop 10 times + + // output line of text + cout << ( count % 2 ? "****" : "++++++++" ) + << endl; + ++count; // increment count + } + + return 0; // indicate successful termination + +} // end function 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/ch02/Ex02_25.cpp b/Bachelor/Prog1/examples/ch02/Ex02_25.cpp new file mode 100644 index 0000000..6d4a82a --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_25.cpp @@ -0,0 +1,47 @@ +// Ex. 2.25: ex_02_25.cpp +// What does this program print? +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int row = 10; // initialize row + int column; // declare column + + while ( row >= 1 ) { // loop until row < 1 + column = 1; // set column to 1 as iteration begins + + while ( column <= 10 ) { // loop 10 times + cout << ( row % 2 ? "<" : ">" ); // output + ++column; // increment column + + } // end inner while + + --row; // decrement row + cout << endl; // begin new output line + + } // end outer while + + return 0; // indicate successful termination + +} // end function 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/ch02/Ex02_42.cpp b/Bachelor/Prog1/examples/ch02/Ex02_42.cpp new file mode 100644 index 0000000..3c3b514 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_42.cpp @@ -0,0 +1,47 @@ +// Ex. 2.42: ex02_42.cpp +// What does this program print? +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + int x, // declare x + y; // declare y + + // prompt user for input + cout << "Enter two integers in the range 1-20: "; + cin >> x >> y; // read values for x and y + + for ( int i = 1; i <= y; i++ ) { // count from 1 to y + + for ( int j = 1; j <= x; j++ ) // count from 1 to x + cout << '@'; // output @ + + cout << endl; // begin new line + + } // end outer for + + return 0; // indicate successful termination + +} // end function 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/ch02/Fig02_07.cpp b/Bachelor/Prog1/examples/ch02/Fig02_07.cpp new file mode 100644 index 0000000..eac7fce --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_07.cpp @@ -0,0 +1,54 @@ +// Fig. 2.7: fig02_07.cpp +// Class average program with counter-controlled repetition. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + int total; // sum of grades input by user + int gradeCounter; // number of grade to be entered next + int grade; // grade value + int average; // average of grades + + // initialization phase + total = 0; // initialize total + gradeCounter = 1; // initialize loop counter + + // processing phase + while ( gradeCounter <= 10 ) { // loop 10 times + cout << "Enter grade: "; // prompt for input + cin >> grade; // read grade from user + total = total + grade; // add grade to total + gradeCounter = gradeCounter + 1; // increment counter + } + + // termination phase + average = total / 10; // integer division + + // display result + cout << "Class average is " << average << endl; + + return 0; // indicate program ended successfully + +} // end function 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/ch02/Fig02_09.cpp b/Bachelor/Prog1/examples/ch02/Fig02_09.cpp new file mode 100644 index 0000000..a0c1065 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_09.cpp @@ -0,0 +1,76 @@ +// Fig. 2.9: fig02_09.cpp +// Class average program with sentinel-controlled repetition. +#include + +using std::cout; +using std::cin; +using std::endl; +using std::fixed; + +#include // parameterized stream manipulators + +using std::setprecision; // sets numeric output precision + +// function main begins program execution +int main() +{ + int total; // sum of grades + int gradeCounter; // number of grades entered + int grade; // grade value + + double average; // number with decimal point for average + + // initialization phase + total = 0; // initialize total + gradeCounter = 0; // initialize loop counter + + // processing phase + // get first grade from user + cout << "Enter grade, -1 to end: "; // prompt for input + cin >> grade; // read grade from user + + // loop until sentinel value read from user + while ( grade != -1 ) { + total = total + grade; // add grade to total + gradeCounter = gradeCounter + 1; // increment counter + + cout << "Enter grade, -1 to end: "; // prompt for input + cin >> grade; // read next grade + + } // end while + + // termination phase + // if user entered at least one grade ... + if ( gradeCounter != 0 ) { + + // calculate average of all grades entered + average = static_cast< double >( total ) / gradeCounter; + + // display average with two digits of precision + cout << "Class average is " << setprecision( 2 ) + << fixed << average << endl; + + } // end if part of if/else + + else // if no grades were entered, output appropriate message + cout << "No grades were entered" << endl; + + return 0; // indicate program ended successfully + +} // end function 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/ch02/Fig02_11.cpp b/Bachelor/Prog1/examples/ch02/Fig02_11.cpp new file mode 100644 index 0000000..5cf6523 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_11.cpp @@ -0,0 +1,64 @@ +// Fig. 2.11: fig02_11.cpp +// Analysis of examination results. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + // initialize variables in declarations + int passes = 0; // number of passes + int failures = 0; // number of failures + int studentCounter = 1; // student counter + int result; // one exam result + + // process 10 students using counter-controlled loop + while ( studentCounter <= 10 ) { + + // prompt user for input and obtain value from user + cout << "Enter result (1 = pass, 2 = fail): "; + cin >> result; + + // if result 1, increment passes; if/else nested in while + if ( result == 1 ) // if/else nested in while + passes = passes + 1; + + else // if result not 1, increment failures + failures = failures + 1; + + // increment studentCounter so loop eventually terminates + studentCounter = studentCounter + 1; + + } // end while + + // termination phase; display number of passes and failures + cout << "Passed " << passes << endl; + cout << "Failed " << failures << endl; + + // if more than eight students passed, print "raise tuition" + if ( passes > 8 ) + cout << "Raise tuition " << endl; + + return 0; // successful termination + +} // end function 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/ch02/Fig02_14.cpp b/Bachelor/Prog1/examples/ch02/Fig02_14.cpp new file mode 100644 index 0000000..b56633a --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_14.cpp @@ -0,0 +1,44 @@ +// Fig. 2.14: fig02_14.cpp +// Preincrementing and postincrementing. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int c; // declare variable + + // demonstrate postincrement + c = 5; // assign 5 to c + cout << c << endl; // print 5 + cout << c++ << endl; // print 5 then postincrement + cout << c << endl << endl; // print 6 + + // demonstrate preincrement + c = 5; // assign 5 to c + cout << c << endl; // print 5 + cout << ++c << endl; // preincrement then print 6 + cout << c << endl; // print 6 + + return 0; // indicate successful termination + +} // end function 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/ch02/Fig02_16.cpp b/Bachelor/Prog1/examples/ch02/Fig02_16.cpp new file mode 100644 index 0000000..a1998b0 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_16.cpp @@ -0,0 +1,38 @@ +// Fig. 2.16: fig02_16.cpp +// Counter-controlled repetition. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int counter = 1; // initialization + + while ( counter <= 10 ) { // repetition condition + cout << counter << endl; // display counter + ++counter; // increment + + } // end while + + return 0; // successful termination + +} // end function 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/ch02/Fig02_17.cpp b/Bachelor/Prog1/examples/ch02/Fig02_17.cpp new file mode 100644 index 0000000..26e9a9f --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_17.cpp @@ -0,0 +1,36 @@ +// Fig. 2.17: fig02_17.cpp +// Counter-controlled repetition with the for structure. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + // Initialization, repetition condition and incrementing + // are all included in the for structure header. + + for ( int counter = 1; counter <= 10; counter++ ) + cout << counter << endl; + + return 0; // indicate successful termination + +} // end function 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/ch02/Fig02_20.cpp b/Bachelor/Prog1/examples/ch02/Fig02_20.cpp new file mode 100644 index 0000000..6a8081f --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_20.cpp @@ -0,0 +1,38 @@ +// Fig. 2.20: fig02_20.cpp +// Summation with for. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int sum = 0; // initialize sum + + // sum even integers from 2 through 100 + for ( int number = 2; number <= 100; number += 2 ) + sum += number; // add number to sum + + cout << "Sum is " << sum << endl; // output sum + return 0; // successful termination + +} // end function 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/ch02/Fig02_21.cpp b/Bachelor/Prog1/examples/ch02/Fig02_21.cpp new file mode 100644 index 0000000..d27d6e8 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_21.cpp @@ -0,0 +1,60 @@ +// Fig. 2.21: fig02_21.cpp +// Calculating compound interest. +#include + +using std::cout; +using std::endl; +using std::ios; +using std::fixed; + +#include + +using std::setw; +using std::setprecision; + +#include // enables program to use function pow + +// function main begins program execution +int main() +{ + double amount; // amount on deposit + double principal = 1000.0; // starting principal + double rate = .05; // interest rate + + // output table column heads + cout << "Year" << setw( 21 ) << "Amount on deposit" << endl; + + // set floating-point number format + cout << fixed << setprecision( 2 ); + + // calculate amount on deposit for each of ten years + for ( int year = 1; year <= 10; year++ ) { + + // calculate new amount for specified year + amount = principal * pow( 1.0 + rate, year ); + + // output one table row + cout << setw( 4 ) << year + << setw( 21 ) << amount << endl; + + } // end for + + return 0; // indicate successful termination + +} // end function 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/ch02/Fig02_22.cpp b/Bachelor/Prog1/examples/ch02/Fig02_22.cpp new file mode 100644 index 0000000..025f666 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_22.cpp @@ -0,0 +1,95 @@ +// Fig. 2.22: fig02_22.cpp +// Counting letter grades. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + int grade; // one grade + int aCount = 0; // number of As + int bCount = 0; // number of Bs + int cCount = 0; // number of Cs + int dCount = 0; // number of Ds + int fCount = 0; // number of Fs + + cout << "Enter the letter grades." << endl + << "Enter the EOF character to end input." << endl; + + // loop until user types end-of-file key sequence + while ( ( grade = cin.get() ) != EOF ) { + + // determine which grade was input + switch ( grade ) { // switch structure nested in while + + case 'A': // grade was uppercase A + case 'a': // or lowercase a + ++aCount; // increment aCount + break; // necessary to exit switch + + case 'B': // grade was uppercase B + case 'b': // or lowercase b + ++bCount; // increment bCount + break; // exit switch + + case 'C': // grade was uppercase C + case 'c': // or lowercase c + ++cCount; // increment cCount + break; // exit switch + + case 'D': // grade was uppercase D + case 'd': // or lowercase d + ++dCount; // increment dCount + break; // exit switch + + case 'F': // grade was uppercase F + case 'f': // or lowercase f + ++fCount; // increment fCount + break; // exit switch + + case '\n': // ignore newlines, + case '\t': // tabs, + case ' ': // and spaces in input + break; // exit switch + + default: // catch all other characters + cout << "Incorrect letter grade entered." + << " Enter a new grade." << endl; + break; // optional; will exit switch anyway + + } // end switch + + } // end while + + // output summary of results + cout << "\n\nTotals for each letter grade are:" + << "\nA: " << aCount // display number of A grades + << "\nB: " << bCount // display number of B grades + << "\nC: " << cCount // display number of C grades + << "\nD: " << dCount // display number of D grades + << "\nF: " << fCount // display number of F grades + << endl; + + return 0; // indicate successful termination + +} // end function 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/ch02/Fig02_24.cpp b/Bachelor/Prog1/examples/ch02/Fig02_24.cpp new file mode 100644 index 0000000..23724b7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_24.cpp @@ -0,0 +1,39 @@ +// Fig. 2.24: fig02_24.cpp +// Using the do/while repetition structure. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int counter = 1; // initialize counter + + do { + cout << counter << " "; // display counter + } while ( ++counter <= 10 ); // end do/while + + cout << endl; + + return 0; // indicate successful termination + +} // end function 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/ch02/Fig02_26.cpp b/Bachelor/Prog1/examples/ch02/Fig02_26.cpp new file mode 100644 index 0000000..a17fd07 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_26.cpp @@ -0,0 +1,46 @@ +// Fig. 2.26: fig02_26.cpp +// Using the break statement in a for structure. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + + int x; // x declared here so it can be used after the loop + + // loop 10 times + for ( x = 1; x <= 10; x++ ) { + + // if x is 5, terminate loop + if ( x == 5 ) + break; // break loop only if x is 5 + + cout << x << " "; // display value of x + + } // end for + + cout << "\nBroke out of loop when x became " << x << endl; + + return 0; // indicate successful termination + +} // end function 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/ch02/Fig02_27.cpp b/Bachelor/Prog1/examples/ch02/Fig02_27.cpp new file mode 100644 index 0000000..9504968 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_27.cpp @@ -0,0 +1,45 @@ +// Fig. 2.27: fig02_27.cpp +// Using the continue statement in a for structure. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + // loop 10 times + for ( int x = 1; x <= 10; x++ ) { + + // if x is 5, continue with next iteration of loop + if ( x == 5 ) + continue; // skip remaining code in loop body + + cout << x << " "; // display value of x + + } // end for structure + + cout << "\nUsed continue to skip printing the value 5" + << endl; + + return 0; // indicate successful termination + +} // end function 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