diff --git a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/26.cpp b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/26.cpp index 79cddf8..1efb3c8 100644 --- a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/26.cpp +++ b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/26.cpp @@ -9,13 +9,26 @@ * If there would be no savings, no message should be printed. * * Jesus Hilario Hernandez -* February 26, 2018 +* February 28, 2018 * ********************************************************************/ #include +#include + using namespace std; int main() { + // Constant Variables + const float A_CHARGES = 39.99, + + A_ADD = .45, + A_MINS = 450, + B_CHARGES = 59.99, + B_ADD = .40, + B_MINS = 900, + + C_CHARGES = 69.99; + // Variables char menu_choice; @@ -34,9 +47,12 @@ int main() // Validate menu_choice switch (menu_choice) { + // Variables int mins_used; - float total_charges; + float total_charges_A, + total_charges_B, + total_charges_C; case 'a': case 'A': @@ -58,22 +74,46 @@ int main() else { // Display total amount due - cout << "Total monthly charges: "; - + cout << setprecision(2) << fixed; + + // Conditional Statement for each Package + total_charges_A = mins_used > A_MINS ? A_CHARGES + ((mins_used - A_MINS) * A_ADD) : A_CHARGES; + total_charges_B = mins_used > B_MINS ? B_CHARGES + ((mins_used - B_MINS) * B_ADD) : B_CHARGES; + total_charges_C = C_CHARGES; + // Calculates customer's monthly bill (choice A) if (menu_choice == 'a' || menu_choice == 'A') - total_charges = mins_used > 450 ? 39.99 + ((mins_used - 450) * .45) : 39.99; - + { + cout << "Package A monthly charges: $" << total_charges_A << endl; + cout << endl; + + // Decides if Package A is greater than B + if (total_charges_A > total_charges_B) + { + cout << "You could have saved $" << total_charges_A - total_charges_B; + cout << " with Package B." << endl; + cout << "You could have saved $" << total_charges_A - total_charges_C; + cout << " with Package C." << endl; + } + + } // Calculates customer's monthly bill (choice B) else if (menu_choice == 'b' || menu_choice == 'B') - total_charges = mins_used > 900 ? 59.99 + ((mins_used - 900) * .40) : 59.99; - + { + cout << "Package B monthly charges: $" << total_charges_B << endl; + + // Decides if Package B is greater than B + if (total_charges_B > total_charges_C) + { + cout << "You could have saved $" << total_charges_B - total_charges_C; + cout << " with Package C." << endl; + } + + } // Calculates customer's monthly bill (choice C) else if (menu_choice == 'c' || menu_choice == 'C') - total_charges = 69.99; + cout << "Package C monthly charges: $" << total_charges_C << endl; - // Display total charges - cout << total_charges << endl; } break; diff --git a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/27.cpp b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/27.cpp new file mode 100644 index 0000000..3e8dd6c --- /dev/null +++ b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/27.cpp @@ -0,0 +1,225 @@ +/******************************************************************** +* +* 27. Mobile Service Provider, Part 3 +* +* Months with 30 days have 720 hours, and months with 31 days +* have 744 hours. February, with 28 days, has 672 hours. You can +* calculate the number of minutes in a month by multiplying its +* number of hours by 60. Enhance the input validation of the +* Mobile Service Provider program by asking the user for the +* month (by name), and validating that the number of minutes +* entered is not more than the maximum for the entire month. +* Here is a table of the months, their days, and number of hours +* in each. +* +* ============================================================== +* Month Days Hours +* ------------------------------------------------------------- +* January 31 744 +* February 28 672 +* March 31 744 +* April 30 720 +* May 31 744 +* June 30 720 +* July 31 744 +* August 31 744 +* September 30 720 +* October 31 744 +* November 30 720 +* December 31 744 +* ============================================================== +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include +#include + +using namespace std; +int main() +{ + // Constant Variables + const float A_CHARGES = 39.99, + A_ADD = .45, + A_MINS = 450, + B_CHARGES = 59.99, + B_ADD = .40, + B_MINS = 900, + C_CHARGES = 69.99, + _30_DAYS = 30, + _31_DAYS = 31, + _28_DAYS = 28, + NUM_MINS_IN_28_DAYS = 672 * 60, + NUM_MINS_IN_30_DAYS = 720 * 60, + NUM_MINS_IN_31_DAYS = 744 * 60; + + + // Variables + char menu_choice; // Menu selection (menu_choice) + string month; // User (month) + + // Display menu + cout << "\nPackage A: For $39.99 per month 450 minutes are provided." << endl; + cout << " Additional minutes are $0.45 per minute." << endl; + cout << "Package B: For $59.99 per month 900 minutes are provided." << endl; + cout << " Additional minutes are $0.40 per minute." << endl; + cout << "Package C: For $69.99 per month unlimited minutes provided.\n" << endl; + + // Ask for package customer purchased + cout << "Choose package: "; + cin >> menu_choice; + cout << endl; + + // Validate menu_choice + switch (menu_choice) + { + + // Variables + int counter; + float total_charges_A, + total_charges_B, + total_charges_C; + + case 'a': + case 'A': + case 'B': + case 'b': + case 'c': + case 'C': + + // Ask for (month) + cout << "Choose a month" << endl; + cin >> month; + + // Validate (month) + if (month == "January" || month == "january") + counter = _31_DAYS; + else if (month == "February" || month == "february") + counter = _28_DAYS; + else if (month == "March" || month == "march") + counter = _31_DAYS; + else if (month == "April" || month == "april") + counter = _30_DAYS; + else if (month == "May" || month == "may") + counter = _31_DAYS; + else if (month == "June" || month == "june") + counter = _30_DAYS; + else if (month == "July" || month == "july") + counter = _31_DAYS; + else if (month == "August" || month == "august") + counter = _31_DAYS; + else if (month == "September" || month == "september") + counter = _30_DAYS; + else if (month == "October" || month == "october") + counter = _31_DAYS; + else if (month == "November" || month == "november") + counter = _30_DAYS; + else if (month == "December" || month == "december") + counter = _31_DAYS; + else + { + // Explain input error (month) + cout << "Invalid month. Check spelling, rerun program "; + cout << "and try again." << endl; + } + + // Check if (counter) is between 28 and 31 + if (counter >= _28_DAYS && counter <= _31_DAYS) + { + // Variables + int mins_used; // Minutes used + + // Ask how many (mins_used) + cout << "How many minutes used? "; + cin >> mins_used; + cout << endl; + + // Validate (mins_used) + if (mins_used < 0) + { + // Explain input error (mins_used) + cout << "We're sorry. Minutes must be greater than 0.\n"; + cout << "Rerun the program and try again." << endl; + } + else + { + // Validate (mins_used) + if (counter == _31_DAYS && mins_used > NUM_MINS_IN_31_DAYS) + { + // Explain error (mins_used) to (NUM_MINS_IN_31_DAYS) + cout << "We're sorry. Minutes must be " << NUM_MINS_IN_31_DAYS; + cout << " or less.\nRerun the program and try again." << endl; + } + else if (counter == _30_DAYS && mins_used > NUM_MINS_IN_30_DAYS) + { + // Explain error (mins_used) to (NUM_MINS_IN_30_DAYS) + cout << "We're sorry. Minutes must be " << NUM_MINS_IN_30_DAYS; + cout << " or less.\nReturn the program and try again." << endl; + } + else if (counter == _28_DAYS && mins_used > NUM_MINS_IN_28_DAYS) + { + // Explain error (mins_used) to (NUM_MINS_IN_28_DAYS) + cout << "We're sorry. Minutes must be " << NUM_MINS_IN_28_DAYS; + cout << " or less.\nReturn the program and try again." << endl; + } + else + { + // Format precision + cout << setprecision(2) << fixed; + + // Conditional Statement for each Package + total_charges_A = mins_used > A_MINS ? A_CHARGES + ((mins_used - A_MINS) * A_ADD) : A_CHARGES; + total_charges_B = mins_used > B_MINS ? B_CHARGES + ((mins_used - B_MINS) * B_ADD) : B_CHARGES; + total_charges_C = C_CHARGES; + + // Calculates customer's monthly bill (choice A) + if (menu_choice == 'a' || menu_choice == 'A') + { + cout << "Package A monthly charges: $" << total_charges_A << endl; + cout << endl; + + // Decides if Package A is greater than B + if (total_charges_A > total_charges_B) + { + cout << "You could have saved $" << total_charges_A - total_charges_B; + cout << " with Package B." << endl; + cout << "You could have saved $" << total_charges_A - total_charges_C; + cout << " with Package C." << endl; + } + + } + // Calculates customer's monthly bill (choice B) + else if (menu_choice == 'b' || menu_choice == 'B') + { + cout << "Package B monthly charges: $" << total_charges_B << endl; + + // Decides if Package B is greater than B + if (total_charges_B > total_charges_C) + { + cout << "You could have saved $" << total_charges_B - total_charges_C; + cout << " with Package C." << endl; + } + + } + // Calculates customer's monthly bill (choice C) + else if (menu_choice == 'c' || menu_choice == 'C') + cout << "Package C monthly charges: $" << total_charges_C << endl; + + } + } + } + break; + + default: + // Display error (menu_choice). + cout << "We're sorry. Menu choice must be A, B, or C.\n"; + cout << "Rerun the program and try again." << endl; + } + + cout << endl; + + // Terminate program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/Checkpoint/5.1.cpp b/Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/Checkpoint/5.1.cpp new file mode 100644 index 0000000..d7c3196 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/Checkpoint/5.1.cpp @@ -0,0 +1,57 @@ +/******************************************************************** +* +* 5.1 What will the following program segments display? +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +using namespace std; +int main() +{ + // What will the following program segments display? + + // A) + int x = 2, + y = x++; + + cout << x << y << endl; // 32 + + // B) + int a = 2, + b = ++a; + + cout << a << b << endl; // 33 + + // C) + int c = 2, + d = 4; + + cout << c++ << --d << endl; // 23 + + // D) + int e = 2, + f = 2 * e++; + + cout << e << f << endl; // 34 + + // E) + int g = 99; + + if (g++ < 100) + cout << "It is true!\n"; // "It is true!" + else + cout << "It is false!\n"; + + // F) + int h = 0; + + if (++h) + cout << "It is true!\n"; // "It is true!" + else + cout << "It is false!\n"; + + // Terminate Program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/5-1.cpp b/Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/Examples/5-1.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/5-1.cpp rename to Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/Examples/5-1.cpp diff --git a/Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/5-2.cpp b/Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/Examples/5-2.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/5-2.cpp rename to Chapter-5-Loops-and-Files/5.1-increment-and-decrement-operators/Examples/5-2.cpp diff --git a/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/5-14.cpp b/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/5-14.cpp new file mode 100644 index 0000000..e224d49 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/5-14.cpp @@ -0,0 +1,48 @@ +//************************************************************** +// This program averages test scores. It asks the user for the +// number of students and the number of test scores per student. +// +// By: Jesus Hilario Hernandez +// Last Updated: January 10, 2017 +//*************************************************************** +#include +#include +using namespace std; + +int main() +{ + int numStudents, // Number of students + numTests; // Number of tests per student + double total, // Accumulator for total scores + average; // Average test score + + // Set up numeric output formatting. + cout << fixed << showpoint << setprecision(1); + + // Get the number of students. + cout << "This program averages test scores.\n"; + cout << "For how many students do you have scores? "; + cin >> numStudents; + + // Get the number of test scores per student. + cout << "How many test scores does each student have? "; + cin >> numTests; + + // Determine each student's average score. + for (int student = 1; student <= numStudents; student++) + { + total = 0; // Initialize the accumulator. + for (int test = 1; test <= numTests; test++) + { + double score; + cout << "Enter score " << test << " for "; + cout << "student " << student << ": "; + cin >> score; + total += score; + } + average = total / numTests; + cout << "The average score for student " << student; + cout << " is " << average << ".\n\n"; + } + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Hours.cpp b/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Hours.cpp new file mode 100644 index 0000000..3da599a --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Hours.cpp @@ -0,0 +1,31 @@ +/******************************************************************** +* +* This program simulates a digital clock. It displays the +* seconds from 0 to 59. +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include +using namespace std; +int main() +{ + cout << fixed << right; + cout.fill('0'); + for (int hours = 0; hours < 24; hours++) + { + for (int minutes = 0; minutes < 60; minutes++) + { + for (int seconds = 0; seconds < 60; seconds++) + { + cout << setw(2) << hours << ':'; + cout << setw(2) << minutes << ':'; + cout << setw(2) << seconds << endl; + } + } + } + // Terminate program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Minutes.cpp b/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Minutes.cpp new file mode 100644 index 0000000..f3d79f0 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Minutes.cpp @@ -0,0 +1,27 @@ +/******************************************************************** +* +* This program simulates a digital clock. It displays the +* seconds from 0 to 59. +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include +using namespace std; +int main() +{ + cout << fixed << right; + cout.fill('0'); + for (int minutes = 0; minutes < 60; minutes++) + { + for (int seconds = 0; seconds < 60; seconds++) + { + cout << setw(2) << minutes << ":"; + cout << setw(2) << seconds << endl; + } + } + // Terminate program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Seconds.cpp b/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Seconds.cpp new file mode 100644 index 0000000..746e7a5 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.10-Nested-Loops/Examples/Digital Clock/Seconds.cpp @@ -0,0 +1,22 @@ +/******************************************************************** +* +* This program simulates a digital clock. It displays the +* seconds from 0 to 59. +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include // setw() +using namespace std; +int main() +{ + cout << fixed << right; + cout.fill('0'); + for (int seconds = 0; seconds < 60; seconds++) + cout << setw(2) << seconds << endl; + + // Terminate + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.16 - 5.21.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.16 - 5.21.txt new file mode 100644 index 0000000..7a8185e --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.16 - 5.21.txt @@ -0,0 +1,66 @@ +5.16 + + a). What is an output file? + An output file is a file that data is written to. + b). What is an input file? + An input is a file that data is read from. + + + + +5.17 + + What three steps must be taken when a file is used by a program? + + 1. Open the file. - Creates a connection between the file and the program. + 2. Process the file. - Data is written or read + 3. Close the file. - Disconncts the file from the program. + + + + +5.18 + + What is the difference between a text file and a binary file? + + A (text file) can be read, while the binary file has not been decoded + and cannot be read by the program, unless decoded. + + - A (text file) contains data that has been encoded as text + - A (binary file) contains data that has not been converted to text. + + Thus, you cannot view the contents of a (binary file) with a text + editor. + + + + +5.19 + + What is the difference between sequential access and random access? + + - (Sequential access) accessess data from the beginning to the end + of a file. + + - (Random access) can jump directly to any piece of data in a file + without reading the data that comes before it. + + + + +5.20 + + What type of file stream object do you create if you want to write data + to a file? + + ofstream // to create and write + + + + +5.21 + + What type of file stream object do you create if you want to read data + from a file? + + ifstream // to open and read diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.22.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.22.cpp new file mode 100644 index 0000000..bb1abf0 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.22.cpp @@ -0,0 +1,44 @@ +/******************************************************************** +* +* 5.22 +* +* Write a short program that uses a (for) loop to write the +* numbers 1 through 10 to a file. +* +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include // ofstream, open() +#include // setw() + +using namespace std; +int main() +{ + // Define file stream object to link numbers.txt file + ofstream output_file; + + // 1. Open file numbers.txt through file stream object (output_file) + // 1. Open file numbers.txt through file stream object (output_file) + output_file.open("numbers.txt"); + + // Format fixed right with 0 fill + output_file << fixed << right; + output_file.fill('0'); + + // 2. Process file numbers.txt through file stream object (output_file) + // 2. Process file numbers.txt through file stream object (output_file) + for (int num = 1; num <= 10; num++) + { + output_file << setw(2) << num << endl; + } + + // 3. Close file numbers.txt through file stream object (output_file) + // 3. Close file numbers.txt through file stream object (output_file) + output_file.close(); + + // Terminate program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.23.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.23.cpp new file mode 100644 index 0000000..37ac0c3 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/5.23.cpp @@ -0,0 +1,19 @@ +/******************************************************************** +* +* 5.23 Write a shor program that opens the file created by the +* program you wrote for Checkpoint 5.22, reads all of the +* numbers from the file, and displays them. +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include +using namespace std; +int main() +{ + + // Terminate program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/numbers.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/numbers.txt new file mode 100644 index 0000000..bce7098 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Checkpoint/numbers.txt @@ -0,0 +1,10 @@ +01 +02 +03 +04 +05 +06 +07 +08 +09 +10 diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-15.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-15.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-15.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-15.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-16.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-16.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-16.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-16.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-17.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-17.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-17.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-17.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-18.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-18.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-18.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-18.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-19.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-19.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-19.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-19.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-20.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-20.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-20.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-20.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-21.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-21.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-21.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-21.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-22.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-22.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-22.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-22.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-23.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-23.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-23.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-23.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-24.cpp b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-24.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/5-24.cpp rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/5-24.cpp diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Friends.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/Friends.txt similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Friends.txt rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/Friends.txt diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Friends2.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/Friends2.txt similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Friends2.txt rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/Friends2.txt diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/ListOfNumbers.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/ListOfNumbers.txt similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/ListOfNumbers.txt rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/ListOfNumbers.txt diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Numbers.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/Numbers.txt similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Numbers.txt rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/Numbers.txt diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/NumericData.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/NumericData.txt similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/NumericData.txt rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/NumericData.txt diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Sales.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/Sales.txt similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Sales.txt rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/Sales.txt diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/demofile.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/demofile.txt similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/demofile.txt rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/demofile.txt diff --git a/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/demofile2.txt b/Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/demofile2.txt similarity index 100% rename from Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/demofile2.txt rename to Chapter-5-Loops-and-Files/5.11-Using-Files-for-Data-Storage/Examples/demofile2.txt diff --git a/Chapter-5-Loops-and-Files/5.2-Intro-to-Loops-The-While-Loop/5-3.cpp b/Chapter-5-Loops-and-Files/5.2-Intro-to-Loops-The-While-Loop/Examples/5-3.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.2-Intro-to-Loops-The-While-Loop/5-3.cpp rename to Chapter-5-Loops-and-Files/5.2-Intro-to-Loops-The-While-Loop/Examples/5-3.cpp diff --git a/Chapter-5-Loops-and-Files/5.2-Intro-to-Loops-The-While-Loop/5-4.cpp b/Chapter-5-Loops-and-Files/5.2-Intro-to-Loops-The-While-Loop/Examples/5-4.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.2-Intro-to-Loops-The-While-Loop/5-4.cpp rename to Chapter-5-Loops-and-Files/5.2-Intro-to-Loops-The-While-Loop/Examples/5-4.cpp diff --git a/Chapter-5-Loops-and-Files/5.3-Using-While-Loop-for-Input-Validation/Checkpoint/5.2-5.4.cpp b/Chapter-5-Loops-and-Files/5.3-Using-While-Loop-for-Input-Validation/Checkpoint/5.2-5.4.cpp new file mode 100644 index 0000000..beed665 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.3-Using-While-Loop-for-Input-Validation/Checkpoint/5.2-5.4.cpp @@ -0,0 +1,88 @@ +/******************************************************************** +* +* 5.2 Write an input validation loop that asks the user to +* enter a number in the range of 10 through 25. +* +* 5.3 Write an input validation loop that asks the user to +* enter‘Y’, ‘y’, ‘N’, or ‘n’. +* +* 5.4 Write an input validation loop that asks the user to +* enter “Yes” or “No”. +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include + +using namespace std; + +int main() +{ + /**************************************************** + * (5.2) * + * Write an input validation loop that asks the * + * user to enter a number in the range of 10 * + * through 25. * + * * + ****************************************************/ + + // Variables + int number; + + // Ask for (number) + cout << "Enter a number in the range of 10-25: "; + cin >> number; + + // Validate (number) + while (number < 10 || number > 25) + { + cout << "ERROR: Enter a value in the range 10-25. "; + cin >> number; + } + + /**************************************************** + * (5.3) * + * Write an input validation loop that asks the * + * user to enter‘Y’, ‘y’, ‘N’, or ‘n’. * + * * + ****************************************************/ + + // Variables + char user_letter; + + // Ask for (user_letter) + cout << "Enter yes or no (y/n): "; + cin >> user_letter; + + // Validate (user_letter) + while (user_letter != 'Y' && user_letter != 'y' && user_letter != 'N' && user_letter != 'n') + { + cout << "ERROR: Enter either 'Y' or 'N'."; + cin >> user_letter; + } + + /**************************************************** + * (5.4) * + * Write an input validation loop that asks the * + * user to enter “Yes” or “No”. * + * * + ****************************************************/ + // Variables + string user_answer; + + // Ask for (user_answer) + cout << "Enter \"Yes\" or \"No\": "; + cin >> user_answer; + + // Validate (user_answer) + while (user_answer != "Yes" && user_answer != "yes" && user_answer != "no" && user_answer != "No") + { + cout << "ERROR: Enter either \"Yes\" or \"No\"."; + cin >> user_answer; + } + + // Terminate Program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.3-Using-While-Loop-for-Input-Validation/5-5.cpp b/Chapter-5-Loops-and-Files/5.3-Using-While-Loop-for-Input-Validation/Examples/5-5.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.3-Using-While-Loop-for-Input-Validation/5-5.cpp rename to Chapter-5-Loops-and-Files/5.3-Using-While-Loop-for-Input-Validation/Examples/5-5.cpp diff --git a/Chapter-5-Loops-and-Files/5.4-Counters/5-6.cpp b/Chapter-5-Loops-and-Files/5.4-Counters/Examples/5-6.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.4-Counters/5-6.cpp rename to Chapter-5-Loops-and-Files/5.4-Counters/Examples/5-6.cpp diff --git a/Chapter-5-Loops-and-Files/5.5-Do-While-Loop/Checkpoint/5.5.cpp b/Chapter-5-Loops-and-Files/5.5-Do-While-Loop/Checkpoint/5.5.cpp new file mode 100644 index 0000000..fbb087c --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.5-Do-While-Loop/Checkpoint/5.5.cpp @@ -0,0 +1,52 @@ +/******************************************************************** +* +* 5.5 What will the following program segments display? +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include + +using namespace std; + +int main() +{ + // (A) // + // Variables + int count = 10; + + // Pretest loop + do + { + cout << "Hello World\n"; + count++; + } while (count < 1); // Hello World 1x + + + // (B) // + // Variables + int v = 10; + + // Pretest loop + do + cout << v << endl; + while (v < 5); // 10 1x + + + // (C) // + // Variables + int count_2 = 0, number = 0, limit = 4; + + // Pretest loop (user controlled loop) + do + { + number += 2; + count_2++; + } while (count_2 < limit); + cout << number << " " << count_2 << endl; // 8 4 + + // Terminate Program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.5-Do-While-Loop/5-7.cpp b/Chapter-5-Loops-and-Files/5.5-Do-While-Loop/Examples/5-7.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.5-Do-While-Loop/5-7.cpp rename to Chapter-5-Loops-and-Files/5.5-Do-While-Loop/Examples/5-7.cpp diff --git a/Chapter-5-Loops-and-Files/5.5-Do-While-Loop/5-8.cpp b/Chapter-5-Loops-and-Files/5.5-Do-While-Loop/Examples/5-8.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.5-Do-While-Loop/5-8.cpp rename to Chapter-5-Loops-and-Files/5.5-Do-While-Loop/Examples/5-8.cpp diff --git a/Chapter-5-Loops-and-Files/5.6-The-For-Loop/Checkpoint/5.6-5.11.cpp b/Chapter-5-Loops-and-Files/5.6-The-For-Loop/Checkpoint/5.6-5.11.cpp new file mode 100644 index 0000000..8465d61 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.6-The-For-Loop/Checkpoint/5.6-5.11.cpp @@ -0,0 +1,115 @@ +/******************************************************************** +* +* 5.6 - 5.11 +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include + +using namespace std; + +int main() +{ + /**************************************************** + * (5.6) * + * Name the three expressions that appear inside * + * the parentheses in the for loop's header. * + * * + ****************************************************/ + // ANSWER: + // for (initialization, test, update) + + /**************************************************** + * (5.7) * + * You want to write a for loop that displays "I * + * love to program" 50 times. Assume that you will * + * use a counter variable named (count). * + * * + ****************************************************/ + // A) What initialization expression will you use? + // ANSWER: count = 1; + + // B) What test expression will you use? + // ANSWER: count <= 50; + + // C) What update expression will you use? + // ANSWER: count++; + + // D) Write the loop. + for (int count = 1; count <= 50; count++) + cout << count << ". I love to program" << endl; + + /**************************************************** + * (5.8) * + * What will the following program segments * + * display? + * * + ****************************************************/ + /******* + * A. * + *******/ + for (int count = 0; count < 6; count++) + cout << (count + count); + + // ANSWER: + // 0246810 + + /******* + * B. * + *******/ + for (int value = -5; value < 5; value++) + cout << value; + + // ANSWER: + // -5-4-3-2-101234 + // Line break + cout << endl; + + + /******* + * C. * + *******/ + int x; + for (x = 5; x <= 14; x += 3) + cout << x << endl; + cout << x << endl; + + // ANSWER: + // 5 + // 8 + // 11 + // 14 + // 17 + + /**************************************************** + * (5.9) * + * write a for loops that displays your name * + * 10 times. * + ****************************************************/ + for (int i = 1; i <= 10; i++) + cout << i << ". Jesus Hilaro Hernandez" << endl; + + /**************************************************** + * (5.10) * + * Write a for loop that displays all of the odd * + * numbers, 1 through 49. * + ****************************************************/ + for (int i = 1; i <= 49; i += 2) + cout << i << ". Odd numbers." << endl; + + /**************************************************** + * (5.11) * + * Write a for loop that displays every fifth * + * number, zero through 100. * + ****************************************************/ + for (int i = 0; i <= 100; i += 5) + cout << i << ". Every 5th number through 0 and 100." << endl; + + + + // Terminate Program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.6-The-For-Loop/5-10.cpp b/Chapter-5-Loops-and-Files/5.6-The-For-Loop/Examples/5-10.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.6-The-For-Loop/5-10.cpp rename to Chapter-5-Loops-and-Files/5.6-The-For-Loop/Examples/5-10.cpp diff --git a/Chapter-5-Loops-and-Files/5.6-The-For-Loop/5-11.cpp b/Chapter-5-Loops-and-Files/5.6-The-For-Loop/Examples/5-11.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.6-The-For-Loop/5-11.cpp rename to Chapter-5-Loops-and-Files/5.6-The-For-Loop/Examples/5-11.cpp diff --git a/Chapter-5-Loops-and-Files/5.6-The-For-Loop/5-9.cpp b/Chapter-5-Loops-and-Files/5.6-The-For-Loop/Examples/5-9.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.6-The-For-Loop/5-9.cpp rename to Chapter-5-Loops-and-Files/5.6-The-For-Loop/Examples/5-9.cpp diff --git a/Chapter-5-Loops-and-Files/5.7-Keeping-Running-Total/5-12.cpp b/Chapter-5-Loops-and-Files/5.7-Keeping-Running-Total/Examples/5-12.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.7-Keeping-Running-Total/5-12.cpp rename to Chapter-5-Loops-and-Files/5.7-Keeping-Running-Total/Examples/5-12.cpp diff --git a/Chapter-5-Loops-and-Files/5.8-Sentinels/Checkpoint/5.12-5.15.cpp b/Chapter-5-Loops-and-Files/5.8-Sentinels/Checkpoint/5.12-5.15.cpp new file mode 100644 index 0000000..2a94d86 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.8-Sentinels/Checkpoint/5.12-5.15.cpp @@ -0,0 +1,110 @@ +/******************************************************************** +* +* 5.12 - 5.15 +* +* Jesus Hilario Hernandez +* February 28, 2018 +* +********************************************************************/ +#include +#include + +using namespace std; + +int main() +{ + /**************************************************** + * (5.12) * + * Write a for loop that repeats seven times, * + * asking the user to enter a number. The loop * + * should also calculate the sum of the numbers * + * entered. * + * * + ****************************************************/ + // Variables + double total_of_7_nums = 0; + int number; + for (int i = 1; i <= 7; i++) + { + cout << "Enter a Number: "; + cin >> number; + total_of_7_nums += number; + } + cout << "Sum of all numbers = " << total_of_7_nums << endl; + + + + /**************************************************** + * (5.13) * + * In the following program segment, which * + * variable and which is the accumulator? * + * * + ****************************************************/ + + // Variables + int a, x, y = 0; + for (x = 0; x < 10; x++) + { + cout << "Enter a number: "; + cin >> a; + y += a; + } + cout << "The sum of those numbers is " << y << endl; + + // ANSWER: y + + + + /**************************************************** + * (5.14) * + * * + * Why should you be careful when choosing a * + * sentinel value? * + * * + ****************************************************/ + // ANSWER: Be sure that the sentinel value is not part of a + // list of number used. Or that it is not part of the + // running total. + + + + /*************************************************** + * (5.15) * + * * + * How would you modify Program 5-13 so any * + * negative value is a sentinel? * + * * + ****************************************************/ + // Variables + int c, d, f = 0; + + cout << "Enter a number: "; + cin >> c; + + // pretest loop (any negative value [sentinel]) + while (c > 0) + { + f += c; + cout << "Enter a number: "; + cin >> c; + } + + cout << "The sum of those numbers is " << f << endl; + + + // Variables + int r = 0, s, t = 0; + + // Or + do + { + t += r; + cout << "Enter a number: "; + cin >> r; + } while (r > 0); + + cout << "Total sum of number is " << t << endl; + + // Terminate Program + return 0; +} diff --git a/Chapter-5-Loops-and-Files/5.8-Sentinels/5-13.cpp b/Chapter-5-Loops-and-Files/5.8-Sentinels/Examples/5-13.cpp similarity index 100% rename from Chapter-5-Loops-and-Files/5.8-Sentinels/5-13.cpp rename to Chapter-5-Loops-and-Files/5.8-Sentinels/Examples/5-13.cpp diff --git a/Chapter-5-Loops-and-Files/5.9-Focus-On-Software-Egineering-Deciding-Which-Loop-To-Use/Deciding-Which-Loop-To-Use.txt b/Chapter-5-Loops-and-Files/5.9-Focus-On-Software-Egineering-Deciding-Which-Loop-To-Use/Deciding-Which-Loop-To-Use.txt new file mode 100644 index 0000000..3a53307 --- /dev/null +++ b/Chapter-5-Loops-and-Files/5.9-Focus-On-Software-Egineering-Deciding-Which-Loop-To-Use/Deciding-Which-Loop-To-Use.txt @@ -0,0 +1,37 @@ +CONCEPT: Although most repetitive algorithms can be written with any of the + three types of loops, each works best in different situations. + + The ideal to use in different situations: + + 1. - The (while) loop. The (while) loop is a conditional loop, which means + it repeats as long as a particular condit exists. It is also a pretest + loop, so it is + + IDEAL in situations where you do not want to loop to + iterate if the condition is false from the beginning. + + For example, validating input that has been read and reading lists of + data terminated by a sentinel value are good applications of the + (while) loop. + + + + 2. - The (do-while) loop. The (do-while) loop is also a conditional loop. + (while) loop, however, (do-while) is a posttest loop. It is + + IDEAL in situations where you always want the loop to iterate at least + once. The (do-while) loop is a good choice for repeating a menu. + + + + 3. - The (for) loop. The (for) loop is a pretest loop that has built-in + expressions for initializing, testing, and updating. These expressions + make it very convenient to use counter variable to control the number + of iterations that the loop performs. The initialization expression + can initialize the counter variable to a starting value, the test + expression can test the counter variable to determine whether it holds + the maximum value, and the update expression can increment the counter + variable. The (for) loop is + + IDEAL in situations where the exact number + of iterations is known.