├── .gitignore ├── 10th Edition ├── Chapter 02 │ ├── 00.cpp │ ├── 01.cpp │ ├── 02.cpp │ ├── 03.cpp │ ├── 04.cpp │ ├── 05.cpp │ ├── 06.cpp │ ├── 07.cpp │ ├── 08.cpp │ ├── 09.cpp │ ├── 10.cpp │ ├── 11.cpp │ ├── 12.cpp │ ├── 13.cpp │ ├── 14.cpp │ ├── 15.cpp │ ├── 16.cpp │ ├── 17.cpp │ ├── 18.cpp │ ├── 19.cpp │ └── 20.cpp ├── Chapter 03 │ ├── 000.cpp │ ├── 001.cpp │ ├── 002.cpp │ ├── 003.cpp │ ├── 004.cpp │ ├── 005.cpp │ ├── 006.cpp │ ├── 007.cpp │ ├── 008.cpp │ ├── 009.cpp │ ├── 010.cpp │ ├── 011.cpp │ ├── 012.cpp │ ├── 013.cpp │ ├── 014.cpp │ ├── 015.cpp │ ├── 016.cpp │ ├── 017.cpp │ ├── 018.cpp │ ├── 019.cpp │ ├── 020.cpp │ ├── 021.cpp │ ├── 022.cpp │ ├── 023.cpp │ ├── 024.cpp │ └── 025.cpp └── Chapter 04 │ ├── 00.cpp │ ├── 007.cpp │ ├── 008.cpp │ ├── 008a.cpp │ ├── 009.cpp │ ├── 01.cpp │ ├── 010.cpp │ ├── 011.cpp │ ├── 012.cpp │ ├── 013.cpp │ ├── 014.cpp │ ├── 015.cpp │ ├── 016.cpp │ ├── 017.cpp │ ├── 018.cpp │ ├── 019.cpp │ ├── 02.cpp │ ├── 020.cpp │ ├── 021.cpp │ ├── 022.cpp │ ├── 023.cpp │ ├── 024.cpp │ ├── 025.cpp │ ├── 026.cpp │ ├── 027.cpp │ ├── 028.cpp │ ├── 03.cpp │ ├── 04.cpp │ ├── 05.cpp │ ├── 06.cpp │ └── demo-for-vid.cpp ├── 25.cpp ├── 8th Edition Challenges ├── Chapter 02 │ ├── 00.cpp │ ├── 01.cpp │ ├── 02.cpp │ ├── 03.cpp │ ├── 04.cpp │ ├── 05.cpp │ ├── 06.cpp │ ├── 07.cpp │ ├── 08.cpp │ ├── 09.cpp │ ├── 10.cpp │ ├── 11.cpp │ ├── 12.cpp │ ├── 13.cpp │ ├── 14.cpp │ ├── 15.cpp │ ├── 16.cpp │ ├── 17.cpp │ └── 18.cpp ├── Chapter 03 │ ├── 000.cpp │ ├── 001.cpp │ ├── 002.cpp │ ├── 003.cpp │ ├── 004.cpp │ ├── 005.cpp │ ├── 006.cpp │ ├── 007.cpp │ ├── 008.cpp │ ├── 009.cpp │ ├── 010.cpp │ ├── 011.cpp │ ├── 012.cpp │ ├── 013.cpp │ ├── 014.cpp │ ├── 015.cpp │ ├── 016.cpp │ ├── 017.cpp │ ├── 018.cpp │ ├── 019.cpp │ ├── 020.cpp │ ├── 021.cpp │ ├── 022.cpp │ ├── 023.cpp │ └── 024.cpp ├── Chapter 04 │ ├── 000.cpp │ ├── 001.cpp │ ├── 002.cpp │ ├── 003.cpp │ ├── 004.cpp │ ├── 005.cpp │ ├── 006.cpp │ ├── 007.cpp │ ├── 008.cpp │ ├── 008a.cpp │ ├── 009.cpp │ ├── 010.cpp │ ├── 011.cpp │ ├── 012.cpp │ ├── 013.cpp │ ├── 014.cpp │ ├── 015.cpp │ ├── 016.cpp │ ├── 017.cpp │ ├── 018.cpp │ ├── 019.cpp │ ├── 020.cpp │ ├── 021.cpp │ ├── 022.cpp │ ├── 023.cpp │ ├── 024.cpp │ ├── 025.cpp │ ├── 026.cpp │ ├── 027.cpp │ ├── 028.cpp │ └── demo-for-vid.cpp ├── Chapter 05 │ ├── 000.cpp │ ├── 001.cpp │ ├── 002.cpp │ ├── 003.cpp │ ├── 004.cpp │ ├── 005.cpp │ ├── 006.cpp │ ├── 007.cpp │ ├── 008.cpp │ ├── 009.cpp │ ├── 010.cpp │ ├── 011.cpp │ ├── 012.cpp │ ├── 013.cpp │ ├── 014.cpp │ ├── 015.cpp │ ├── 016.cpp │ ├── 017.cpp │ ├── 018.cpp │ ├── 019.cpp │ ├── 020.cpp │ ├── 021.cpp │ ├── 022.cpp │ ├── 023.cpp │ ├── 024.cpp │ ├── 025.cpp │ ├── 026.cpp │ ├── LineUp.txt │ ├── Random.txt │ └── savings_account_report.txt ├── Chapter 06 │ ├── 00.cpp │ ├── 01.cpp │ ├── 02.cpp │ ├── 03.cpp │ ├── 04.cpp │ ├── 05.cpp │ ├── 06.cpp │ ├── 07.cpp │ ├── 08.cpp │ ├── 09.cpp │ ├── 10.cpp │ ├── 11.cpp │ ├── 12.cpp │ ├── 13.cpp │ ├── 14.cpp │ ├── 15.cpp │ ├── 16.cpp │ ├── 17.cpp │ ├── 18.cpp │ ├── 19.cpp │ ├── 20.cpp │ ├── 21.cpp │ ├── 22.cpp │ ├── 23.cpp │ ├── 24.cpp │ ├── 25.cpp │ ├── hospital_report.txt │ └── primeNumber.txt ├── Chapter 07 │ ├── 00.cpp │ ├── 00.txt │ ├── 01.cpp │ ├── 02.cpp │ ├── 03.cpp │ ├── 04.cpp │ ├── 05.cpp │ ├── 06.cpp │ ├── 07.cpp │ ├── 08.cpp │ ├── 09.cpp │ ├── 10.cpp │ ├── 11.cpp │ ├── 12.cpp │ ├── 13.cpp │ ├── 14.cpp │ ├── 15.cpp │ ├── 16.cpp │ ├── 17.cpp │ ├── 18.cpp │ ├── 19.cpp │ ├── 20.cpp │ ├── 7-23.cpp │ ├── BoyNames.txt │ ├── CorrectAnswers.txt │ ├── GirlNames.txt │ ├── RainOrShine.txt │ ├── StudentAnswers.txt │ ├── Teams.txt │ ├── WorldSeriesWinners.txt │ ├── numbers.txt │ ├── seating_chart.txt │ └── seating_prices.txt ├── Chapter 08 │ ├── 00.cpp │ ├── 00.txt │ ├── 01.cpp │ ├── 02.cpp │ ├── 03.cpp │ ├── 04.cpp │ ├── 05.cpp │ ├── 06.cpp │ ├── 07.cpp │ ├── 08.cpp │ ├── 09.cpp │ ├── 10.cpp │ ├── 11.cpp │ ├── 12.cpp │ ├── 1994_Gas_Averages_Lowest_to_Highest.txt │ ├── 1994_Weekly_Gas_Averages.txt │ ├── names.txt │ └── program_8-8.cpp ├── Chapter 09 │ ├── 00.cpp │ ├── 00.txt │ ├── 01.cpp │ ├── 02.cpp │ ├── 03.cpp │ ├── 04.cpp │ ├── 05.cpp │ ├── 06.cpp │ ├── 07.cpp │ ├── 08.cpp │ ├── 09.cpp │ ├── 10.cpp │ ├── 11.cpp │ ├── 12.cpp │ ├── 13.cpp │ └── 9-19.cpp └── Chapter 10 │ ├── 00.cpp │ ├── 01.cpp │ └── 02.cpp ├── LICENSE └── README.md /10th Edition/Chapter 02/00.cpp: -------------------------------------------------------------------------------- 1 | /**/ 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | return 0; 9 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 1. Sum of Two Numbers 4 | 5 | VIDEO WALK THROUGH HERE: https://youtu.be/34_sECtzYZM 6 | 7 | 8 | */ 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int number1 = 50, 15 | number2 = 100, 16 | total = number1 + number2; 17 | 18 | cout << endl 19 | << "number1 + number2 = " << total << endl 20 | << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/02.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 2. Sales Prediction 4 | 5 | Video walk through here: https://youtu.be/625ZvaRfteg 6 | Video walk through here: https://youtu.be/625ZvaRfteg 7 | Video walk through here: https://youtu.be/625ZvaRfteg 8 | 9 | */ 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | float total_sales_percent = .58, 16 | total_sales_this_year = 8.6E6, // $8,600,000 or $8.6 million 17 | total_predicted_sales = total_sales_percent * total_sales_this_year; 18 | 19 | cout << endl 20 | << "The East Coast division will generate " 21 | << total_predicted_sales 22 | << " in sales this year." 23 | << endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/03.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3. Sales Tax 3 | 4 | Video walk through: https://youtu.be/zPrXAcTC-tE 5 | Video walk through: https://youtu.be/zPrXAcTC-tE 6 | Video walk through: https://youtu.be/zPrXAcTC-tE 7 | 8 | */ 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | const float STATE_TAX = .04, 15 | COUNTY_TAX = .02; 16 | int purchase_amount = 95; 17 | 18 | float total_state_purchase = purchase_amount * STATE_TAX, 19 | total_county_purchase = purchase_amount * COUNTY_TAX; 20 | 21 | cout << "\nPurchase amout = $" << purchase_amount 22 | << "\nTotal state tax = $" << total_state_purchase 23 | << "\nTotal county tax = $" << total_county_purchase 24 | << "\n" << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/04.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 4. Restaurant Bill 3 | 4 | video walk through: https://youtu.be/hZDxlBgkTfs 5 | video walk through: https://youtu.be/hZDxlBgkTfs 6 | video walk through: https://youtu.be/hZDxlBgkTfs 7 | 8 | */ 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | const float TAX = .0675, // %6.75 15 | TIP = .20; // %20 16 | 17 | float meal_charge = 88.67, // $88.67 18 | tax_amount = meal_charge * TAX, 19 | tip_amount = (meal_charge + tax_amount) * TIP, 20 | total_bill = meal_charge + tax_amount + tip_amount; 21 | 22 | cout << "\nMeal cost: $" << meal_charge << endl 23 | << "Tax amount: $" << tax_amount << endl 24 | << "Tip amount: $" << tip_amount << endl 25 | << "Total bill: $" << total_bill << endl 26 | << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/05.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 5. Average of Values 3 | 4 | video walk through: https://youtu.be/lVfbFS19xEM 5 | video walk through: https://youtu.be/lVfbFS19xEM 6 | video walk through: https://youtu.be/lVfbFS19xEM 7 | 8 | */ 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | const int DIVISOR = 5; 15 | 16 | int number_1 = 28, 17 | number_2 = 32, 18 | number_3 = 37, 19 | number_4 = 24, 20 | number_5 = 36, 21 | 22 | sum = number_1 + 23 | number_2 + 24 | number_3 + 25 | number_4 + 26 | number_5, 27 | 28 | average = sum / DIVISOR; 29 | 30 | cout << endl 31 | << "The average is " << average 32 | << endl 33 | << endl; 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/06.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 06. ANNUAL PAY 4 | 5 | video walk through: https://youtu.be/WV4yreeWv9s 6 | video walk through: https://youtu.be/WV4yreeWv9s 7 | video walk through: https://youtu.be/WV4yreeWv9s 8 | 9 | */ 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | double payAmount = 2200.0, 16 | payPeriods = 26, 17 | annualPay = payAmount * payPeriods; 18 | 19 | cout << endl 20 | << "Total annual pay = $" << annualPay 21 | << endl 22 | << endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/07.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 07. OCEAN LEVELS 4 | video walk through: https://youtu.be/aWj0rbOtMOw 5 | video walk through: https://youtu.be/aWj0rbOtMOw 6 | video walk through: https://youtu.be/aWj0rbOtMOw 7 | 8 | */ 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | const double MIL_PER_YEAR = 1.5; 15 | 16 | int years1 = 5, 17 | years2 = 7, 18 | years3 = 10; 19 | 20 | float mils_after_5 = MIL_PER_YEAR * years1, 21 | mils_after_7 = MIL_PER_YEAR * years2, 22 | mils_after_10 = MIL_PER_YEAR * years3; 23 | 24 | cout << endl 25 | << "In " << years1 << " years, the ocean's level will grow " << mils_after_5 << " millimeters higher." << endl 26 | << "In " << years2 << " years, the ocean's level will grow " << mils_after_7 << " millimeters higher." << endl 27 | << "In " << years3 << " years, the ocean's level will grow " << mils_after_10 << " millimeters higher." << endl 28 | << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/09.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 9. Cyborg Data Type Sizes 3 | You have been given a job as a programmer on a Cyborg supercomputer. In order to accomplish some calculations, you need to know how many bytes the following data types use: char, int, float, and double. You do not have any technical documenta- tion, so you can’t look this information up. Write a C++ program that will determine the amount of memory used by these types and display the information on the screen. 4 | */ 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | char letter; 11 | int number; 12 | float decimal; 13 | double another_decimal; 14 | 15 | cout << endl 16 | << "A char uses " << sizeof(letter) << " byte(s)." << endl 17 | << "An int uses " << sizeof(number) << " byte(s)." << endl 18 | << "A float uses " << sizeof(decimal) << " byte(s)." << endl 19 | << "A double uses " << sizeof(another_decimal) << " byte(s)." << endl 20 | << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 10. Miles per Gallon 3 | A car holds 15 gallons of gasoline and can travel 375 miles before refueling. Write a program that calculates the number of miles per gallon the car gets. Display the result on the screen. 4 | 5 | Hint: Use the following formula to calculate miles per gallon (MPG): 6 | 7 | MPG = Miles Driven/Gallons of Gas Used 8 | 9 | */ 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | double milesDriven = 375, 16 | gallonsOfGasUsed = 15, 17 | MPG = milesDriven / gallonsOfGasUsed; 18 | 19 | cout << endl 20 | << "Miles driven = " << milesDriven << endl 21 | << "Gallons used = " << gallonsOfGasUsed << endl 22 | << "Miles per gallon = "<< MPG << endl 23 | << endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 11. Distance per Tank of Gas 3 | 4 | A car with a 20-gallon gas tank averages 23.5 miles per gallon when driven in town, and 28.9 miles per gallon when driven on the highway. Write a program that calculates and displays the distance the car can travel on one tank of gas when driven in town and when driven on the highway. 5 | 6 | Hint: The following formula can be used to calculate the distance: 7 | 8 | Distance = Number of Gallons x Average Miles per Gallon 9 | 10 | */ 11 | #include 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | const int NUM_OF_GALLONS = 20; 17 | 18 | float inTownMPG = 23.5, 19 | highwayMPG = 28.5, 20 | 21 | inTownDistance = NUM_OF_GALLONS * inTownMPG, 22 | highwayDistance = NUM_OF_GALLONS * highwayMPG; 23 | 24 | cout << endl 25 | << "Number of gallons = " << NUM_OF_GALLONS << endl 26 | << endl 27 | << "In town MPG = " << inTownMPG << endl 28 | << "In town distance = " << inTownDistance << endl 29 | << endl 30 | << "Highway MPG = " << highwayMPG << endl 31 | << "Highway distance = " << highwayDistance << endl 32 | << endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 12. Land Calculation 3 | One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of acres in a tract of land with 391,876 square feet. 4 | */ 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | const int ONE_ACRE_OF_LAND = 43560; 11 | 12 | int numberOfAcres = 391876, 13 | totalAcreage = numberOfAcres / ONE_ACRE_OF_LAND; 14 | 15 | cout << endl 16 | << "1 Acre = " << ONE_ACRE_OF_LAND << endl 17 | << "Number of acres = " << numberOfAcres << endl 18 | << "Total acreage = " << totalAcreage << endl 19 | << endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 13. Circuit Board Price 3 | An electronics company sells circuit boards at a 35 percent profit. Write a program that will calculate the selling price of a circuit board that costs $14.95. Display the result on the screen. 4 | */ 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | float profitPercent = .35, 11 | cost = 14.95, 12 | sellingPrice = (cost * profitPercent) + cost; 13 | 14 | cout << endl 15 | << "Profit = " << (profitPercent * 100) << "%" << endl 16 | << "Cost = $"<< cost << endl 17 | << "Price = $"<< sellingPrice << endl 18 | << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/14.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 14. Personal Information 3 | Write a program that displays the following pieces of information, each on a separate line: 4 | 5 | Your name 6 | Your address, with city, state, and ZIP code 7 | Your telephone number 8 | Your college major 9 | 10 | Use only a single cout statement to display all of this information. 11 | 12 | */ 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | string name = "John Doe", 19 | address = "12 address dr", 20 | phone = "444-444-4444", 21 | major = "Comp"; 22 | 23 | cout << endl 24 | << "Name: " << name << endl 25 | << "Address: " << address << endl 26 | << "Phone: " << phone << endl 27 | << "Major: " << major << endl 28 | << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/15.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 15. Triangle Pattern 3 | Write a program that displays the following pattern on the screen: 4 | 5 | * 6 | *** 7 | ***** 8 | ******* 9 | 10 | */ 11 | #include 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | cout << endl 17 | << " * " << endl 18 | << " *** " << endl 19 | << " ***** " << endl 20 | << " ******* " << endl 21 | << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/16.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 16. Diamond Pattern 3 | Write a program that displays the following pattern: 4 | 5 | * 6 | *** 7 | ***** 8 | ******* 9 | ***** 10 | *** 11 | * 12 | 13 | */ 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | cout << endl 20 | << " * " << endl 21 | << " *** " << endl 22 | << " ***** " << endl 23 | << " ******* " << endl 24 | << " ***** " << endl 25 | << " *** " << endl 26 | << " * " << endl 27 | << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/17.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 17. Stock Commission 3 | Kathryn bought 750 shares of stock at a price of $35.00 per share. She must pay her stockbroker a 2 percent commission for the transaction. Write a program that calcu- lates and displays the following: 4 | 5 | • The amount paid for the stock alone (without the commission). 6 | • The amount of the commission. 7 | • The total amount paid (for the stock plus the commission). 8 | 9 | */ 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | const float COMMISSION_PERCENT = 2; 16 | 17 | int numberOfShares = 750, 18 | sharePrice = 35, 19 | amountPaidForShares = numberOfShares * sharePrice; 20 | 21 | float commissionTotal = amountPaidForShares * (COMMISSION_PERCENT / 100), 22 | totalPaid = commissionTotal + amountPaidForShares; 23 | 24 | cout << endl 25 | << "Amount paid : $" << amountPaidForShares << endl 26 | << "Commission % : " << COMMISSION_PERCENT << "%" << endl 27 | << "Commission total : $" << commissionTotal << endl 28 | << "Grand Total : $" << totalPaid << endl 29 | << endl; 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/18.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 18. Energy Drink Consumption 3 | A soft drink company recently surveyed 16,500 of its customers and found that approxi- mately 15 percent of those surveyed purchase one or more energy drinks per week. Of those customers who purchase energy drinks, approximately 58 percent of them prefer citrus-flavored energy drinks. Write a program that displays the following: 4 | 5 | • The approximate number of customers in the survey who purchase one or more energy drinks per week. 6 | • The approximate number of customers in the survey who prefer citrus-flavored energy drinks. 7 | 8 | */ 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | const int NUM_SURVEYED = 16500; 15 | 16 | float group1 = 15, // one or more drinks 17 | group2 = 58, // citrus only 18 | 19 | group1Total = NUM_SURVEYED * (group1 / 100), 20 | group2Total = NUM_SURVEYED * (group2 / 100); 21 | 22 | cout << endl 23 | << "Group 1 = " << group1Total << " customers" << endl 24 | << "Group 2 = " << group2Total << " customers" << endl 25 | << endl; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/19.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 19. Annual High Temperatures 3 | The average July high temperature is 85 degrees Fahrenheit in New York City, 88 degrees Fahrenheit in Denver, and 106 degrees Fahrenheit in Phoenix. Write a program that calculates and reports what the new average July high temperature would be for each of these cities if temperatures rise by 2 percent. 4 | 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | const float TEMP_RISE_PERCENT = 2; 12 | 13 | float newYorkTemp = 85, 14 | denverTemp = 88, 15 | phoenixTemp = 106; 16 | 17 | cout << endl 18 | << "Old Temps: " << endl 19 | << "New York = " << newYorkTemp << "˚F " << endl 20 | << "Denver = " << denverTemp << "˚F " << endl 21 | << "Phoenix = " << phoenixTemp << "˚F " << endl; 22 | 23 | // 2% rise calculation 24 | newYorkTemp = newYorkTemp + (newYorkTemp * (TEMP_RISE_PERCENT / 100)); 25 | denverTemp = denverTemp + (denverTemp * (TEMP_RISE_PERCENT / 100)); 26 | phoenixTemp = phoenixTemp + (phoenixTemp * (TEMP_RISE_PERCENT / 100)); 27 | 28 | cout << endl 29 | << "New Temps: " << endl 30 | << "New York = " << newYorkTemp << "˚F " << endl 31 | << "Denver = " << denverTemp << "˚F " << endl 32 | << "Phoenix = " << phoenixTemp << "˚F " << endl 33 | << endl; 34 | 35 | // Screen Record 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 02/20.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 20. How Much Paint 3 | A particular brand of paint covers 340 square feet per gallon. Write a program to determine and report approximately how many gallons of paint will be needed to paint two coats on a wooden fence that is 6 feet high and 100 feet long. 4 | */ 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | const float PAINT_COVERAGE = 340; // sqft per gallon 11 | 12 | // Calculate the area of the fence: 13 | // Area = Height × Length 14 | float height = 6, // in feet 15 | length = 100, // in feet 16 | area = height * length, 17 | 18 | // Determine the number of gallons needed: 19 | // total Gallons need = Total Area / Coverage per Gallon 20 | totalArea = area * 2, // 2 coats of paint 21 | gallonsNeeded = totalArea / PAINT_COVERAGE; 22 | 23 | cout << endl 24 | << "Fence Dimensions: " << endl 25 | << "height = " << height << " ft" << endl 26 | << "length = " << length << " ft" << endl 27 | << "total area = " << totalArea << " sqft" << endl 28 | << "coverage per gallon = " << PAINT_COVERAGE << " sqft" << endl 29 | << "Gallons needed = " << gallonsNeeded << endl 30 | << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 03/000.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | return 0; 9 | } 10 | 11 | 12 | // discord c++ help invite: https://discord.gg/7NUTVfZT -------------------------------------------------------------------------------- /10th Edition/Chapter 03/001.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1. Miles per Gallon 3 | Write a program that calculates a car’s gas mileage. The program should ask the user to enter the number of gallons of gas the car can hold, and the number of miles it can be driven on a full tank. It should then display the number of miles that may be driven per gallon of gas. 4 | */ 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | float gallons, 11 | miles; 12 | 13 | cout << "\nEnter gallons: "; 14 | cin >> gallons; 15 | 16 | cout << "Enter miles: "; 17 | cin >> miles; 18 | 19 | float MPG = miles / gallons; 20 | 21 | cout << endl 22 | << "Gallons = " << gallons << endl 23 | << "Miles = " << miles << endl 24 | << "MPG = " << MPG << endl 25 | << endl; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/002.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 2. Stadium Seating 3 | There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many tickets for each class of seats were sold, then displays the amount of income gen- erated from ticket sales. Format your dollar amount in fixed-point notation, with two decimal places of precision, and be sure the decimal point is always displayed. 4 | */ 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | const float CLASS_A_PRICE = 15.00, 13 | CLASS_B_PRICE = 12.00, 14 | CLASS_C_PRICE = 9.00; 15 | 16 | int seatsA, 17 | seatsB, 18 | seatsC; 19 | 20 | float aTotal, 21 | bTotal, 22 | cTotal, 23 | incomeTotal; 24 | 25 | cout << "\nEnter number of seats sold in Class A: "; 26 | cin >> seatsA; 27 | 28 | cout << "Enter number of seats sold in Class B: "; 29 | cin >> seatsB; 30 | 31 | cout << "Enter number of seats sold in Class C: "; 32 | cin >> seatsC; 33 | 34 | aTotal = seatsA * CLASS_A_PRICE; 35 | bTotal = seatsB * CLASS_B_PRICE; 36 | cTotal = seatsC * CLASS_C_PRICE; 37 | 38 | incomeTotal = aTotal + bTotal + cTotal; 39 | 40 | cout << setprecision(2) << fixed << endl 41 | 42 | << seatsA << " Class A seats @ $" 43 | << setw(6) << CLASS_A_PRICE 44 | << " = $" << aTotal 45 | << endl 46 | 47 | << seatsB << " Class B seats @ $" 48 | << setw(6) << CLASS_B_PRICE 49 | << " = $" << bTotal 50 | << endl 51 | 52 | << seatsC << " Class C seats @ $" 53 | << setw(6) << CLASS_C_PRICE 54 | << " = $" << cTotal 55 | << endl 56 | << endl; 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/003.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3. Test Average 3 | Write a program that asks for five test scores. The program should calculate the aver- age test score and display it. The number displayed should be formatted in fixed-point notation, with one decimal point of precision. 4 | */ 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | const int NUM_OF_SCORES = 5; 13 | 14 | float score_1, 15 | score_2, 16 | score_3, 17 | score_4, 18 | score_5, 19 | average, 20 | total; 21 | 22 | cout << "\nEnter 5 test scores." << endl 23 | << "Press enter to continue"; 24 | cin.get(); 25 | 26 | cout << "\nScore 1: "; 27 | cin >> score_1; 28 | cout << "Score 2: "; 29 | cin >> score_2; 30 | cout << "Score 3: "; 31 | cin >> score_3; 32 | cout << "Score 4: "; 33 | cin >> score_4; 34 | cout << "Score 5: "; 35 | cin >> score_5; 36 | 37 | total = score_1 + 38 | score_2 + 39 | score_3 + 40 | score_4 + 41 | score_5; 42 | 43 | average = total / NUM_OF_SCORES; 44 | 45 | cout << setprecision(1) << fixed 46 | << "\nThe average test score is " 47 | << average << '\n' 48 | << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/004.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 4. Average Rainfall 3 | Write a program that calculates the average rainfall for three months. The program should ask the user to enter the name of each month, such as June or July, and the amount of rain (in inches) that fell each month. The program should display a message similar to the following: 4 | 5 | The average rainfall for June, July, and August is 6.72 inches. 6 | */ 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | // Variables 15 | const int NUM_OF_MONTHS = 3; 16 | 17 | string month1, 18 | month2, 19 | month3; 20 | 21 | float rainfallMonth1, 22 | rainfallMonth2, 23 | rainfallMonth3, 24 | total, 25 | average; 26 | 27 | cout << "\n3 month rainfall average." << endl 28 | << "Press Enter to continue." << endl; 29 | cin.get(); 30 | 31 | cout << "Month 1 name: " << endl; 32 | cin >> month1; 33 | cout << "Rain amount: " << endl; 34 | cin >> rainfallMonth1; 35 | 36 | cout << "\nMonth 2 name: " << endl; 37 | cin >> month2; 38 | cout << "Rain amount: " << endl; 39 | cin >> rainfallMonth2; 40 | 41 | cout << "\nMonth 3 name: " << endl; 42 | cin >> month3; 43 | cout << "Rain amount: " << endl; 44 | cin >> rainfallMonth3; 45 | 46 | total = rainfallMonth1 + 47 | rainfallMonth2 + 48 | rainfallMonth3; 49 | 50 | average = total / NUM_OF_MONTHS; 51 | 52 | cout << setprecision(2) << fixed 53 | << "\nRainfall:" << endl 54 | << month1 << ": " << rainfallMonth1 << " in. " << endl 55 | << month2 << ": " << rainfallMonth2 << " in. " << endl 56 | << month3 << ": " << rainfallMonth3 << " in. " << endl 57 | << "\nTotal rainfall = " << total << endl 58 | << "Average rainfall = " << average << endl 59 | << endl; 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/005.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 5. Male and Female Percentages 3 | Write a program that asks the user for the number of males and the number of females registered in a class. The program should display the percentage of males and females in the class. 4 | 5 | Hint: Suppose there are 8 males and 12 females in a class. There are 20 students in the class. The percentage of males can be calculated as 8/20 = 0.4, or 40 percent. The percentage of females can be calculated as 12/20 = 0.6, or 60 percent. 6 | */ 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int males, 15 | females; 16 | 17 | float malePercentage, 18 | femalePercentage, 19 | total; 20 | 21 | cout << "\nHow many males in the class: "; 22 | cin >> males; 23 | cout << "\nHow many females: "; 24 | cin >> females; 25 | 26 | total = males + females; 27 | 28 | malePercentage = (males / total) * 100; 29 | femalePercentage = (females / total) * 100; 30 | 31 | cout << setprecision(1) << fixed; 32 | cout << endl 33 | << "Males = " << males << endl 34 | << "Females = " << females << endl 35 | << "Total = " << total << endl 36 | << "Male % = " << malePercentage << endl 37 | << "Female % = " << femalePercentage << endl 38 | << endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/006.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6. Ingredient Adjuster 3 | 4 | A cookie recipe calls for the following ingredients: 5 | • 1.5 cups of sugar 6 | • 1 cup of butter 7 | • 2.75 cups of flour 8 | 9 | The recipe produces 48 cookies with this amount of the ingredients. Write a program that asks the user how many cookies he or she wants to make, then displays the number of cups of each ingredient needed for the specified number of cookies. 10 | */ 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | const float COOKIES = 48, // number of cookies to make 19 | SUGAR = 1.5, // cups 20 | BUTTER = 1, // cup 21 | FLOUR = 2.75; // cups 22 | 23 | float cookies, 24 | total_butter, 25 | total_sugar, 26 | total_flour; 27 | 28 | cout << "\nEnter number of cookies: "; 29 | cin >> cookies; 30 | 31 | total_sugar = (SUGAR * cookies) / COOKIES; 32 | total_flour = (FLOUR * cookies) / COOKIES; 33 | total_butter = (BUTTER * cookies) / COOKIES; 34 | 35 | cout << setprecision(2) << fixed << endl 36 | << "Total cookies = " << cookies << endl 37 | << "Total sugar = " << total_sugar << " cups " << endl 38 | << "Total flour = " << total_flour << " cups " << endl 39 | << "Total butter = " << total_butter << " cups " << endl 40 | << endl; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/007.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 7. Box Office 3 | A movie theater only keeps a percentage of the revenue earned from ticket sales. The remainder goes to the movie distributor. Write a program that calculates a theater’s gross and net box office profit for a night. The program should ask for the name of the movie, and how many adult and child tickets were sold. (The price of an adult ticket is $10.00 and a child’s ticket is $6.00.) It should display a report similar to: 4 | 5 | Movie Name: “Wheels of Fury” 6 | Adult Tickets Sold: 382 7 | Child Tickets Sold: 127 8 | Gross Box Office Profit: 4,582.00 9 | Net Box Office Profit: $ $ 3,665.60 10 | Amount Paid to Distributor: $ 916.40 11 | 12 | NOTE: Assume the theater keeps 20 percent of the gross box office profit. 13 | */ 14 | #include 15 | #include 16 | 17 | using namespace std; 18 | 19 | int main() 20 | { 21 | const float ADULT_PRICE = 10, 22 | CHILD_PRICE = 6, 23 | PROFIT = .20; // 20% 24 | 25 | string movie_name; 26 | 27 | int adult_tkt, 28 | child_tkt; 29 | 30 | float total, 31 | theater_percentage; 32 | 33 | cout << "\nMovie name? "; 34 | getline(cin, movie_name); 35 | 36 | cout << "How many adult tickets? "; 37 | cin >> adult_tkt; 38 | 39 | cout << "How many child tickets? "; 40 | cin >> child_tkt; 41 | 42 | total = (adult_tkt * ADULT_PRICE) + (child_tkt * CHILD_PRICE); 43 | theater_percentage = total * PROFIT; 44 | 45 | cout << setprecision(2) << fixed << endl 46 | << "Movie Name: " << setw(10) << movie_name << endl 47 | << "Adult tickets sold: " << setw(10) << adult_tkt << endl 48 | << "Child tickets sold: " << setw(10) << child_tkt << endl 49 | << "Total cost: " << setw(10) << total << endl 50 | << "Gross Box Office Profit: " << setw(10) << theater_percentage << endl 51 | < 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | 13 | const float INDV_WIDGET_WT = 12.5; 14 | 15 | float pallet_weight, 16 | pallet_with_widgets, 17 | widget_weight, 18 | number_of_widgets; 19 | 20 | cout << "\nHow much does the pallet weight by itself? "; 21 | cin >> pallet_weight; 22 | 23 | cout << "\nHow much does the pallet weight with widgets? "; 24 | cin >> pallet_with_widgets; 25 | 26 | widget_weight = pallet_with_widgets - pallet_weight; 27 | number_of_widgets = widget_weight / INDV_WIDGET_WT; 28 | 29 | cout << setprecision(2) << fixed << right 30 | << "\nIndividual widget weight = " << setw(6) << INDV_WIDGET_WT 31 | << "\nPallet weight = " << setw(6) << pallet_weight 32 | << "\nPallet weight with widgets = " << setw(6) << pallet_with_widgets 33 | << "\nWidget weight = " << setw(6) << widget_weight 34 | << "\nNumber of widgets = " << setw(6) << number_of_widgets 35 | << endl 36 | << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/009.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 9. How Many Calories? 3 | A bag of cookies holds 30 cookies. The calorie information on the bag claims there are 10 “servings” in the bag and that a serving equals 300 calories. Write a program that asks the user to input how many cookies he or she actually ate, then reports how many total calories were consumed. 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | const int COOKIES_PER_BAG = 30, 12 | SERVINGS_PER_BAG = 10, 13 | CALORIES_PER_SERVING = 300; 14 | 15 | float cookies, 16 | servings, 17 | calories, 18 | cookies_per_serving = COOKIES_PER_BAG / SERVINGS_PER_BAG; 19 | 20 | cout << "\nHow many cookies eaten? "; 21 | cin >> cookies; 22 | 23 | servings = cookies / cookies_per_serving; 24 | calories = servings * CALORIES_PER_SERVING; 25 | 26 | cout << setprecision(2) << fixed << right 27 | << "\nNumber of cookies per bag = " << setw(8) << COOKIES_PER_BAG 28 | << "\nNumber of servings per bag = " << setw(8) << SERVINGS_PER_BAG 29 | << "\nNumber of calories per serving = " << setw(8) << CALORIES_PER_SERVING 30 | << "\nNumber of cookies per serving = " << setw(8) << cookies_per_serving 31 | << "\nNumber of cookies eaten = " << setw(8) << cookies 32 | << "\nNumber of servings eaten = " << setw(8) << servings 33 | << "\nNumber of calories consumed = " << setw(8) << calories 34 | << endl 35 | << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/010.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 10. How Much Insurance? 3 | Many financial experts advise that property owners should insure their homes or buildings for at least 80 percent of the amount it would cost to replace the structure. Write a program that asks the user to enter the replacement cost of a building, then displays the minimum amount of insurance he or she should buy for the property. 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | // Constane variables 12 | const float INSURE_PERCENTAGE = .80; 13 | 14 | float replacement_cost, 15 | min_amount_insurance; 16 | 17 | 18 | cout << "\nThis program will calculate the 80 % for you. " << endl 19 | << "\nPress \"Enter\" to continute.\n" << endl; 20 | cin.get(); 21 | 22 | cout << "Enter the replacement cost of your home or building : $"; 23 | cin >> replacement_cost; 24 | 25 | min_amount_insurance = replacement_cost * INSURE_PERCENTAGE; 26 | 27 | cout << setprecision(2) << fixed 28 | << "\nThe minimum amount of insurance to buy at %80 is: $" 29 | << min_amount_insurance << endl 30 | << endl; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/011.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 11. Automobile Costs 3 | Write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | const int TWELVE_MONTHS = 12; 12 | 13 | float loan_payment, 14 | insurance, 15 | gas, 16 | oil, 17 | tires, 18 | maintenance, 19 | monthly_expenses, 20 | annual_cost; 21 | 22 | cout << "\nEnter the following monthly costs for your automobile: \nLoan Payment : $"; 23 | cin >> loan_payment; 24 | cout << "Insurance : $"; 25 | cin >> insurance; 26 | cout << "Gas : $"; 27 | cin >> gas; 28 | cout << "Oil : $"; 29 | cin >> oil; 30 | cout << "Tires : $"; 31 | cin >> tires; 32 | cout << "Maintenae : $"; 33 | cin >> maintenance; 34 | 35 | monthly_expenses = loan_payment + 36 | insurance + 37 | gas + 38 | oil + 39 | tires + 40 | maintenance; 41 | 42 | annual_cost = monthly_expenses * TWELVE_MONTHS; 43 | 44 | cout << "\nMonthly car expenses = $" << monthly_expenses 45 | << "\nTotal annual cost = $" << annual_cost 46 | << endl; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/012.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 12. Celsius to Fahrenheit 3 | Write a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is 4 | 5 | F = (9/5)C + 32 6 | 7 | F is the Fahrenheit temperature, and C is the Celsius temperature. 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | float F, 16 | C; 17 | 18 | cout << "\nEnter the Celsius Temp: "; 19 | cin >> C; 20 | 21 | // F = (9/5)C + 32 22 | F = ((9.0 / 5.0) * C) + 32; 23 | 24 | cout << setprecision(1) << fixed << right << endl 25 | << "Celsius Temp = " << setw(6) << C << endl 26 | << "Fahrenheit Temp = " << setw(6) << F << endl 27 | << endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/013.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 13. Currency 3 | Write a program that will convert U.S. dollar amounts to Japanese yen and to euros, storing the conversion factors in the constants YEN_PER_DOLLAR and EUROS_PER_ DOLLAR. To get the most up-to-date exchange rates, search the Internet using the term “currency exchange rate”. If you cannot find the most recent exchange rates, use the following: 4 | 5 | 1 Dollar = 98.93 Yen 6 | 1 Dollar = 0.74 Euros 7 | 8 | Format your currency amounts in fixed-point notation, with two decimal places of precision, and be sure the decimal point is always displayed. 9 | 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | const float YEN_PER_DOLLAR = 98.93, 18 | EUROS_PER_DOLLAR = .74; 19 | 20 | float user_amount, 21 | dollar_to_yen, 22 | dollar_to_euro; 23 | 24 | cout << "\nEnter U.S. dollar amount: "; 25 | cin >> user_amount; 26 | 27 | dollar_to_yen = user_amount * YEN_PER_DOLLAR; 28 | dollar_to_euro = user_amount * EUROS_PER_DOLLAR; 29 | 30 | cout << setprecision(2) << fixed << endl 31 | << "Dollar amount = $" << user_amount << endl 32 | << "Yen amount = ¥" << dollar_to_yen << endl 33 | << "Euro amount = €" << dollar_to_euro << endl 34 | << endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/015.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 15. Property Tax 3 | 4 | A county collects property taxes on the assessment value of property, which is 60 percent of the property’s actual value. If an acre of land is valued at $10,000, its assessment value is $6,000. The property tax is then 75¢ for each $100 of the assessment value. The tax for the acre assessed at $6,000 will be $45. Write a program that asks for the actual value of a piece of property, then displays the assessment value and property tax. 5 | 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | const float ASSESSMENT_PER = 0.60, 14 | PROPERTY_TAX_PER = 0.75, 15 | HUNDRED = 100; 16 | 17 | float total_assessment_value, 18 | total_property_tax, 19 | value_of_property; 20 | 21 | cout << "\nWhat is that actual value of a property? "; 22 | cin >> value_of_property; 23 | 24 | total_assessment_value = value_of_property * ASSESSMENT_PER; 25 | total_property_tax = (total_assessment_value / HUNDRED) * PROPERTY_TAX_PER; 26 | 27 | cout << setprecision(2) << fixed << right << endl 28 | << "\nProperty Value: $" << setw(12) << value_of_property 29 | << "\nAssessment value: $" << setw(12) << total_assessment_value 30 | << "\nProperty Tax: $" << setw(12) << total_property_tax 31 | << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/017.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 17. Math Tutor 4 | Write a program that can be used as a math tutor for a young student. The program should display two random numbers to be added, such as 5 | 6 | 247 7 | +129 8 | ----- 9 | 10 | The program should then pause while the student works on the problem. When the student is ready to check the answer, he or she can press a key and the program will display the correct solution: 11 | 12 | 247 13 | +129 14 | ----- 15 | 376 16 | 17 | */ 18 | #include 19 | #include // For rand and srand functions 20 | #include // For the time function 21 | using namespace std; 22 | 23 | int main() 24 | { 25 | const int MIN_VALUE = 100, 26 | MAX_VALUE = 1000; 27 | 28 | int num_1, 29 | num_2, 30 | answer; 31 | 32 | // Get system time 33 | unsigned seed = time(0); 34 | 35 | // Seed the random number generator. 36 | srand(seed); 37 | 38 | cout << "\nThis program displays two random numbers to be added." << endl; 39 | 40 | num_1 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE; 41 | num_2 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE; 42 | 43 | answer = num_1 + num_2; 44 | 45 | cout << " " << num_1 << endl 46 | << "+" << num_2 << endl 47 | << "-----" << endl 48 | << endl; 49 | 50 | cout << "Press \"Enter\" to see solution"; 51 | cin.get(); 52 | cout << endl; 53 | 54 | cout << " " << num_1 << endl 55 | << "+" << num_2 << endl 56 | << "-----" << endl 57 | << " " << answer << endl 58 | << endl; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/018.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 18. Interest Earned 3 | Assuming there are no deposits other than the original investment, the balance in a savings account after one year may be calculated as 4 | 5 | Amount = Principal x (1 + Rate/T)^T 6 | 7 | Principal is the balance in the savings account, Rate is the interest rate, and T is the number of times the interest is compounded during a year (T is 4 if the interest is compounded quarterly). 8 | 9 | Write a program that asks for the principal, the interest rate, and the number of times the interest is compounded. It should display a report similar to: 10 | 11 | Interest Rate: 4.25% 12 | Times Compounded: 12 13 | Principal: $ 1000.00 14 | Interest: $ 43.34 15 | Amount in Savings: $ 1043.34 16 | 17 | 18 | 19 | */ 20 | #include 21 | #include 22 | #include // pow(5, 10); 23 | using namespace std; 24 | 25 | int main() 26 | { 27 | 28 | float principal, 29 | rate, 30 | amount, 31 | total_interest, 32 | amount_in_savings; 33 | 34 | int T; // T 35 | 36 | // Input 37 | cout << "\nWhat is the pricipal balance? "; 38 | cin >> principal; 39 | cout << "What is the interest rate? %"; 40 | cin >> rate; 41 | cout << "Number of times the interest is compounded during a year? "; 42 | cin >> T; 43 | 44 | // Process 45 | amount = 46 | principal * pow 47 | ( 48 | (1 + (rate/100)/T), T 49 | ); 50 | 51 | total_interest = amount - principal; 52 | 53 | // Output 54 | cout << setprecision(2) << fixed << right << endl 55 | << "Interest Rate: " << setw(10) << rate << "% " << endl 56 | << "Times Compounded: " << setw(10) << T << endl 57 | << "Principal: $" << setw(10) << principal << endl 58 | << "Interest: $" << setw(10) << total_interest << endl 59 | << "Amount after a year: $" << setw(10) << amount << endl 60 | << endl; 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/020.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 20. Pizza Pi 4 | Joe’s Pizza Palace needs a program to calculate the number of slices a pizza of any size can be divided into. The program should perform the following steps: 5 | A) Ask the user for the diameter of the pizza in inches. 6 | B) Calculate the number of slices that may be taken from a pizza of that size. 7 | A) Display a message telling the number of slices. 8 | To calculate the number of slices that may be taken from the pizza, you must know the following facts: 9 | • Each slice should have an area of 14.125 inches. 10 | • To calculate the number of slices, simply divide the area of the pizza by 14.125. 11 | • The area of the pizza is calculated with this formula: 12 | 13 | Area = πr^2 14 | 15 | NOTE: π is the Greek letter pi. 3.14159 can be used as its value. The variable r is the radius of the pizza. Divide the diameter by 2 to get the radius. 16 | 17 | Make sure the output of the program displays the number of slices in fixed-point notation, rounded to one decimal place of precision. Use a named constant for pi. 18 | 19 | */ 20 | #include 21 | #include // pow() 22 | #include 23 | using namespace std; 24 | 25 | int main() 26 | { 27 | const float SLICE_AREA_SIZE = 14.125, 28 | PI = 3.14159; 29 | 30 | float d, // Diameter 31 | r, // Radius 32 | Area, 33 | num_of_slices; 34 | 35 | cout << "\nWhat is the diameter of the pizza in inches? "; 36 | cin >> d; 37 | 38 | r = d / 2; 39 | 40 | Area = PI * pow(r, 2); 41 | 42 | num_of_slices = Area / SLICE_AREA_SIZE; 43 | 44 | cout << setprecision(1) << fixed 45 | << "Number of slices = " << num_of_slices 46 | << endl; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/021.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 21. How Many Pizzas? 4 | Modify the program you wrote in Programming Challenge 20 (Pizza Pi) so it reports the number of pizzas you need to buy for a party if each person attending is expected to eat an average of four slices. The program should ask the user for the number of people who will be at the party, and for the diameter of the pizzas to be ordered. It should then calculate and display the number of pizzas to purchase. 5 | 6 | */ 7 | #include 8 | #include // setprecision(3) == .000 9 | #include // pow(6, 3) == 6^3 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | const float SLICE_AREA_SIZE = 14.125, 15 | PI = 3.14159; 16 | 17 | float d, // Diameter 18 | r, // Radius 19 | Area, 20 | num_of_slices, 21 | slices_needed, 22 | num_of_pizzas_needed; 23 | 24 | int num_of_people; 25 | 26 | // INPUT 27 | cout << "\nWhat is the number of people who will be at the party? "; 28 | cin >> num_of_people; 29 | 30 | cout << "What is the diameter of the pizza in inches? "; 31 | cin >> d; 32 | 33 | 34 | // PROCESS 35 | slices_needed = num_of_people * 4; 36 | r = d / 2; 37 | Area = PI * pow(r, 2); 38 | num_of_slices = Area / SLICE_AREA_SIZE; 39 | num_of_pizzas_needed = slices_needed / num_of_slices; 40 | 41 | 42 | // OUTPUT 43 | cout << setprecision(1) << fixed 44 | << "Number of people = " << num_of_people << endl 45 | << "Number of slices needed = " << slices_needed << endl 46 | << "Number of slices per Pizza = " << num_of_slices << endl 47 | << "Number of pizzas to order = " << num_of_pizzas_needed 48 | << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/022.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 22. Angle Calculator 3 | Write a program that asks the user for an angle, entered in radians. The program should then display the sine, cosine, and tangent of the angle. (Use the sin, cos, and tan library functions to determine these values.) The output should be displayed in fixed-point notation, rounded to four decimal places of precision. 4 | */ 5 | #include 6 | #include // setprecision() 7 | #include // sin(), tan(), and cos() 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | double angle, 14 | sine, 15 | cosine, 16 | tangent; 17 | 18 | // INPUT 19 | cout << "\nWhat is an angle in radians? "; 20 | cin >> angle; 21 | 22 | // PROCESS 23 | sine = sin(angle); 24 | cosine = cos(angle); 25 | tangent = tan(angle); 26 | 27 | // OUTPUT 28 | cout << setprecision(4) << fixed 29 | << "Angle in radians = " << angle << endl 30 | << "Sine of the angle = " << sine << endl 31 | << "Cosine of angle = " << cosine << endl 32 | << "Tangent of angle = " << tangent << endl 33 | << endl; 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 03/024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 24. Planting Grapevines 4 | 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int V, // number of grapevines that will fit in the row 13 | R, // length of the row, in feet 14 | E, // amount of space, in feet, used by an end-post assembly 15 | S; // space between vines, in feet 16 | 17 | cout << "Enter length of the row, in feet: "; 18 | cin >> R; 19 | 20 | cout << "Enter Amount of space used by an end-post assembly, in feet: "; 21 | cin >> E; 22 | 23 | cout << "Enter amount of space between the vines, in feet: "; 24 | cin >> S; 25 | 26 | V = (R - (2 * E)) / S; 27 | 28 | cout << "\nNumber of grapevines that will fit in the row: " << V << endl 29 | << endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /10th Edition/Chapter 03/025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Variables 8 | string name, 9 | city, 10 | college, 11 | profession, 12 | animal, 13 | pet_name, 14 | age; 15 | 16 | cout << "\nWhat is your name? "; 17 | getline(cin, name); 18 | 19 | cout << "Your age? "; 20 | getline(cin, age); 21 | 22 | cout << "What city to you live? "; 23 | getline(cin, city); 24 | 25 | cout << "What college? "; 26 | getline(cin, college); 27 | 28 | cout << "What do you do for a living? "; 29 | getline(cin, profession); 30 | 31 | cout << "What type of animal? "; 32 | getline(cin, animal); 33 | 34 | cout << "Pet's name? "; 35 | getline(cin, pet_name); 36 | 37 | 38 | cout << endl 39 | << "Name: " << name << endl 40 | << "City: " << city << endl 41 | << "Age: " << age << endl 42 | << "College: " << college << endl 43 | << "Profession: " << profession << endl 44 | << "Animal: " << animal << endl 45 | << "Pet Name: " << pet_name << endl 46 | << endl; 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 04/00.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /10th Edition/Chapter 04/007.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Time Calculator 4 | */ 5 | 6 | #include 7 | #include // for setprecision() and fixed 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int seconds; 13 | float num_mins; 14 | 15 | cout << "\nEnter a number of seconds: "; 16 | cin >> seconds; 17 | 18 | cout << setprecision(2) << fixed; 19 | 20 | if (seconds <= 59) 21 | { 22 | cout << seconds << " is less that 60 seconds." << endl; 23 | } 24 | else if (seconds >= 60 && seconds < 3600) 25 | { 26 | num_mins = seconds / 60.0; 27 | cout << num_mins << " minutes in "<< seconds << " seconds" << endl; 28 | } 29 | else if (seconds >= 3600 && seconds < 86400) 30 | { 31 | num_mins = seconds / 3600.0; 32 | cout << num_mins << " hours in " << seconds << " seconds" << endl; 33 | } 34 | else if (seconds >= 86400) 35 | { 36 | num_mins = seconds / 86400.0; 37 | cout << num_mins << " days in " << seconds << " seconds" << endl; 38 | } 39 | 40 | cout << endl; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /10th Edition/Chapter 04/009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Constant variables 7 | const float ONE_DOLLAR = 1.00, 8 | PENNY = 0.01, 9 | NICKEL = 0.05, 10 | DIME = 0.10, 11 | QUARTER = 0.25; 12 | 13 | // Variables 14 | float pennies, 15 | nickels, 16 | dimes, 17 | quarters, 18 | total_coins; 19 | 20 | // explain 21 | cout << "\nLet's see if your coins equal 1 dollar.\n"; 22 | cout << endl; 23 | 24 | // Ask for pennies, nickels, dimes, and quarters. 25 | cout << "How many pennies do you have? "; 26 | cin >> pennies; 27 | cout << "Nickels? "; 28 | cin >> nickels; 29 | cout << "Dimes? "; 30 | cin >> dimes; 31 | cout << "Quarters? "; 32 | cin >> quarters; 33 | 34 | // Format line break 35 | cout << endl; 36 | 37 | // Calculate total coins 38 | total_coins += ((pennies *= PENNY) + 39 | (nickels *= NICKEL) + 40 | (dimes *= DIME) + 41 | (quarters *= QUARTER)); 42 | 43 | // Decision statement 44 | if (total_coins == ONE_DOLLAR) 45 | { 46 | cout << '$' << total_coins; 47 | cout << " equal to $1." << endl; 48 | } 49 | else if (total_coins < ONE_DOLLAR) 50 | { 51 | cout << '$' << total_coins; 52 | cout << " is less than $1. Re-run program and "; 53 | cout << " Try again.\n"; 54 | } 55 | else if (total_coins > ONE_DOLLAR) 56 | { 57 | cout << '$' << total_coins; 58 | cout << " greater than $1. Re-run program and "; 59 | cout << " Try again.\n"; 60 | } 61 | 62 | // Format line break 63 | cout << endl; 64 | 65 | // Terminate program 66 | return 0; 67 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 04/01.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int num1, 8 | num2; 9 | 10 | cout << "\nEnter Number 1: "; 11 | cin >> num1; 12 | 13 | cout << "Enter Number 2: "; 14 | cin >> num2; 15 | 16 | int smallest = (num1 < num2) ? num1 : num2, 17 | largest = (num1 > num2) ? num1 : num2; 18 | 19 | cout << endl 20 | << "Num 1 = " << num1 << endl 21 | << "Num 2 = " << num2 << endl 22 | << "Smallest = " << smallest << endl 23 | << "Largest = " << largest << endl 24 | << endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /10th Edition/Chapter 04/010.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | int year, 8 | month; 9 | 10 | // Ask user to enter month 11 | cout << endl; 12 | cout << "Enter the month (1 - 12): "; 13 | cin >> month; 14 | cout << "Enter the year (up to 9000): "; 15 | cin >> year; 16 | 17 | // Line Break 18 | cout << endl; 19 | 20 | // Error check for valid year 21 | if (year >= 0 && year <= 9000) 22 | { 23 | // Decision statement for month 24 | switch (month) 25 | { 26 | case 1: 27 | case 3: 28 | case 5: 29 | case 7: 30 | case 8: 31 | case 10: 32 | case 12: 33 | cout << "31 days"; 34 | break; 35 | 36 | case 4: 37 | case 6: 38 | case 9: 39 | case 11: 40 | cout << "30 days"; 41 | break; 42 | 43 | case 2: 44 | // Decision statement for leap year 45 | if (year % 100 == 0) 46 | { 47 | if (year % 400 == 0) 48 | cout << "29 days" << endl; 49 | else 50 | cout << "28 days" << endl; 51 | } 52 | else if (year % 100 != 0) 53 | { 54 | if (year % 4 == 0) 55 | cout << "29 days" << endl; 56 | else 57 | cout << "28 days" << endl; 58 | } 59 | break; 60 | 61 | default: 62 | cout << "Invalid month. Rerun program. Try again." << endl; 63 | } 64 | } 65 | else 66 | { 67 | cout << "Invalid year. Rerun program. "; 68 | cout << "Try again." << endl; 69 | } 70 | 71 | // line break 72 | cout << endl; 73 | 74 | // Terminate program 75 | return 0; 76 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 04/011.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For rand() and srand() 3 | #include // For time() 4 | 5 | using namespace std; 6 | int main() 7 | { 8 | // Constant Variables 9 | const int MIN_NUM = 99, 10 | MAX_NUM = 999; 11 | 12 | // Variables 13 | int user_answer, 14 | rand_num_1, 15 | rand_num_2, 16 | rand_num_answer; 17 | 18 | // Assign system time to a variable. 19 | unsigned seed = time(0); 20 | 21 | // Seed random number generator with system time 22 | srand(seed); 23 | 24 | // Generate two random numbers 25 | rand_num_1 = (rand() % (MAX_NUM - MIN_NUM + 1)) + MIN_NUM; 26 | 27 | rand_num_2 = (rand() % (MAX_NUM - MIN_NUM + 1)) + MIN_NUM; 28 | 29 | // sum of the two random numbers. 30 | rand_num_answer = rand_num_1 + rand_num_2; 31 | 32 | // Display math problem & receive answer 33 | cout << "\n " << rand_num_1 << endl; 34 | cout << " +" << rand_num_2 << endl; 35 | cout << "------" << endl << " "; 36 | cin >> user_answer; 37 | 38 | // Decision statement & display 39 | if (user_answer == rand_num_answer) 40 | cout << "\nCongratulations!"; 41 | else 42 | { 43 | cout << "\nOops. Correct answer here: "; 44 | cout << rand_num_answer; 45 | } 46 | 47 | // Format line break. 48 | cout << endl << endl; 49 | 50 | // Terminate program 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /10th Edition/Chapter 04/013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | int books_purchased; 8 | 9 | // Get number of books user purchased this month. 10 | cout << "\nHow many books did you purchase this month: "; 11 | cin >> books_purchased; 12 | 13 | // Format a line break 14 | cout << endl; 15 | 16 | // Decision statement to determine points earned 17 | if (books_purchased == 0) 18 | cout << "0 points earned\n"; 19 | else if (books_purchased == 1) 20 | cout << "5 points earned\n"; 21 | else if (books_purchased == 2) 22 | cout << "15 points earned\n"; 23 | else if (books_purchased == 3) 24 | cout << "30 points earned\n"; 25 | else if (books_purchased >= 4) 26 | cout << "60 points earned\n"; 27 | else 28 | { 29 | cout << "Oops. A number 0 or greater must be entered."; 30 | cout << "\nPlease rerun the program and try again.\n"; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 04/018.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | float calories_from_fat, 6 | fat_grams, 7 | percentage_of_calories, 8 | total_calories; 9 | 10 | cout << "\nEnter number of calories: "; 11 | cin >> total_calories; 12 | 13 | if (total_calories > 0) 14 | { 15 | cout << "Enter number of fat grams: "; 16 | cin >> fat_grams; 17 | 18 | if (fat_grams > 0) 19 | { 20 | calories_from_fat = fat_grams * 9; 21 | percentage_of_calories = (calories_from_fat / total_calories) * 100; // .45 == 45 22 | 23 | if (calories_from_fat > total_calories) 24 | { 25 | cout << "\nCalories from fat cannot be greater\n"; 26 | cout << "then the total number of calories." << endl; 27 | cout << "Either the total calories or fat\n"; 28 | cout << "grams were entered incorrectly." << endl; 29 | cout << "Rerun the program and try again." << endl << endl; 30 | } 31 | else 32 | { 33 | cout << "Total calories = " << total_calories << " cal"<< endl; 34 | cout << "Total fat grams = " << fat_grams << " g" << endl; 35 | cout << "Total calories from fat = " << calories_from_fat << " cal from fat"<< endl; 36 | cout << "Total percentage of calories from fat = " << percentage_of_calories << '%' << endl; 37 | cout << endl; 38 | } 39 | } 40 | else 41 | { 42 | cout << "We're sorry. Fat grams must be more than 0.\n"; 43 | cout << "Please rerun the progarm and try again." << endl; 44 | } 45 | } 46 | else 47 | { 48 | cout << "We're sorry. Total calories must be more than 0.\n"; 49 | cout << "Please rerun the progarm and try again." << endl; 50 | } 51 | 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 04/019.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | double wavelength; 6 | 7 | cout << "\nEnter wavelength (in meters): "; 8 | cin >> wavelength; // .0001 9 | 10 | if (wavelength >= 1E-2) 11 | cout << "Radio Waves" << endl; 12 | else if (wavelength <= 1E-2 && wavelength >= 1E-3) 13 | cout << "Microwaves" << endl; 14 | else if (wavelength <= 1E-3 && wavelength >= 7E-7) 15 | cout << "Infrared" << endl; 16 | else if (wavelength <= 7E-7 && wavelength >= 4E-7) 17 | cout << "Visible Light" << endl; 18 | else if (wavelength <= 4E-7 && wavelength >= 1E-8) 19 | cout << "Ultraviolet" << endl; 20 | else if (wavelength <= 1E-8 && wavelength >= 1E-11) 21 | cout << "X Rays" << endl; 22 | else if (wavelength <= 1E-11) 23 | cout << "Gamma Rays" << endl; 24 | 25 | cout << endl; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 04/02.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | int userNum; 9 | 10 | cout << "Enter a number between 1 and 10: "; 11 | cin >> userNum; 12 | 13 | switch (userNum) 14 | { 15 | case 1: 16 | cout << "The Roman numeral for " << userNum << " is I\n"; 17 | break; 18 | 19 | case 2: 20 | cout << "The Roman numeral for " << userNum << " is II\n"; 21 | break; 22 | 23 | case 3: 24 | cout << "The Roman numeral for " << userNum << " is III\n"; 25 | break; 26 | 27 | case 4: 28 | cout << "The Roman numeral for " << userNum << " is IV\n"; 29 | break; 30 | 31 | case 5: 32 | cout << "The Roman numeral for " << userNum << " is V\n"; 33 | break; 34 | 35 | case 6: 36 | cout << "The Roman numeral for " << userNum << " is VI\n"; 37 | break; 38 | 39 | case 7: 40 | cout << "The Roman numeral for " << userNum << " is VII\n"; 41 | break; 42 | 43 | case 8: 44 | cout << "The Roman numeral for " << userNum << " is VIII\n"; 45 | break; 46 | 47 | case 9: 48 | cout << "The Roman numeral for " << userNum << " is XI\n"; 49 | break; 50 | 51 | case 10: 52 | cout << "The Roman numeral for " << userNum << " is X\n"; 53 | break; 54 | 55 | default: 56 | cout << "You must enter a number between 1 and 10" << endl 57 | << "Re-run the program and try again." << endl 58 | << endl; 59 | break; 60 | 61 | } 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 04/022.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | float user_temp; 6 | 7 | cout << "Enter temperature: "; 8 | cin >> user_temp; 9 | 10 | cout << "\nAt " 11 | << user_temp 12 | << " F these substances will either " 13 | << " freeze or boil.\n"; 14 | 15 | if (user_temp <= 32) 16 | { 17 | cout << "\nFreeze:\n"; 18 | cout << "--------" << endl; 19 | cout << "Water" << endl; 20 | 21 | if (user_temp <= -38) 22 | { 23 | cout << "Mercury" << endl; 24 | 25 | if (user_temp <= -173) 26 | { 27 | cout << "Ethyl alcohol" << endl; 28 | 29 | if (user_temp <= -362) 30 | { 31 | cout << "Oxygen" << endl; 32 | } 33 | } 34 | } 35 | cout << endl; 36 | } 37 | 38 | if (user_temp >= -306) 39 | { 40 | cout << "Boil: \n"; 41 | cout << "--------" << endl; 42 | cout << "Oxygen." << endl; 43 | 44 | if (user_temp >= 172) 45 | { 46 | cout << "Ethyl alcohol." << endl; 47 | 48 | if (user_temp >= 212) 49 | { 50 | cout << "Water" << endl; 51 | 52 | if (user_temp >= 676) 53 | { 54 | cout << "Mercury" << endl; 55 | } 56 | } 57 | } 58 | cout << endl; 59 | } 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /10th Edition/Chapter 04/03.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Variables 8 | int month, 9 | year, 10 | day; 11 | 12 | cout << "\nEnter the month (e.g. January = 1, June = 6): "; 13 | cin >> month; 14 | 15 | cout << "Enter the day (e.g. 1, 2, 3, 4, etc..): "; 16 | cin >> day; 17 | 18 | cout << "Enter the year (e.g. 1978 = 78, 1991 = 91, 2016 = 16): "; 19 | cin >> year; 20 | 21 | 22 | string magic = (month * day == year) ? "magic" : "not magic"; 23 | 24 | cout << "\nThe date is " << magic << endl 25 | << endl; 26 | 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /10th Edition/Chapter 04/04.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 4. Areas of Rectangles 3 | The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length and width of two rectangles. The program should tell the user which rectangle has the greater area, or if the areas are the same. 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | // Variables 12 | float rectangle1, 13 | l1, // rectangle 1 length 14 | w1, // rectangle 1 width 15 | 16 | rectangle2, 17 | l2, // rectangle 2 length 18 | w2; // rectangle 2 width 19 | 20 | 21 | cout << "\nWhat is the length of the first rectangle: "; 22 | cin >> l1; 23 | 24 | cout << "What is the width of the first rectangle : "; 25 | cin >> w1; 26 | 27 | cout << "What is the length of the second rectangle: "; 28 | cin >> l2; 29 | 30 | cout << "What is the width of the second rectangle : "; 31 | cin >> w2; 32 | 33 | 34 | rectangle1 = l1 * w1; 35 | rectangle2 = l2 * w2; 36 | 37 | 38 | cout << endl; 39 | 40 | if (rectangle1 > rectangle2) 41 | cout << "Area for rectangle 1 is greater." << endl; 42 | 43 | else if (rectangle1 < rectangle2) 44 | cout << "Area for rectangle 2 is greater." << endl; 45 | 46 | else if (rectangle1 == rectangle2) 47 | cout << "Area for both rectangles is the same." << endl; 48 | 49 | cout << endl; 50 | 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /10th Edition/Chapter 04/05.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Body Mass Index 4 | 5 | */ 6 | 7 | #include 8 | #include // for the pow() method 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | 14 | float BMI, // Body Mass Index 15 | weight, 16 | height; 17 | 18 | 19 | cout << "\nEnter weight(in pounds): " << endl; 20 | cin >> weight; 21 | 22 | cout << "Enter height(in inches, e.g. 5' 9\" = 69): " << endl; 23 | cin >> height; 24 | 25 | 26 | BMI = weight * (703 / pow(height, 2)); 27 | 28 | 29 | cout << "BMI = " << BMI << endl; 30 | 31 | 32 | if (BMI >= 18.5 && BMI <= 25) 33 | cout << "Weight optimal." << endl; 34 | 35 | else if (BMI >= 0 && BMI <= 18.5) 36 | cout << "Weight underweight." << endl; 37 | 38 | else if (BMI >= 25) 39 | cout << "Weight overweight." << endl; 40 | 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /10th Edition/Chapter 04/06.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Mass and Weight 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | float weight, 12 | mass; 13 | 14 | cout << "\nWhat is the object's mass: "; 15 | cin >> mass; 16 | 17 | 18 | weight = mass * 9.8; 19 | 20 | 21 | cout << "\nObject's weight = " << weight << endl; 22 | 23 | if (weight >= 1000) 24 | cout << "Too heavy." << endl; 25 | else if (weight <= 10) 26 | cout << "Too light." << endl; 27 | else 28 | cout << "Neither heavy or light." << endl; 29 | 30 | cout << endl; 31 | 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/00.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 00. TITLE 4 | * Problem. 5 | * 6 | * Jesus Hilario Hernandez 7 | * December 15th 2017 8 | * 9 | * 10 | ********************************************************************/ 11 | #include 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/01.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 01. SUM OF TWO NUMBERS 4 | * Write a program that stores the integers 50 and 100 in variables, 5 | * and stores the sum of these two in a variable named total. 6 | * 7 | * Jesus Hilario Hernandez 8 | * December 15th 2017 9 | * 10 | * 11 | ********************************************************************/ 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | int fifty = 50, 18 | onehundred = 100, 19 | total = fifty + onehundred; 20 | 21 | cout << "Total = " << total << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/02.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 02. SALES TAX 4 | * The East Coast sales division of a company generates 58 percent 5 | * of total sales. Based on that percentage, write a program 6 | * that will predict how much that East Coast division will 7 | * generate if the company has $8.6 million in sales this year. 8 | * 9 | * Jesus Hilario Hernandez 10 | * December 18th 2017 11 | * 12 | * 13 | ********************************************************************/ 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | float totalSalesPercentage = .58, 20 | totalSalesThisYear = 8.6E6; 21 | 22 | cout << endl << endl; 23 | cout << "The East Coast Division will generate "; 24 | cout << totalSalesPercentage * totalSalesThisYear << endl; 25 | cout << "percent of total sales this year.\n" << endl; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/03.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 03. SALES PREDICTION 4 | * Write a program that will compute the total sales tax on a 5 | * $95 purchase. Assume the state sales tax is 4% and the 6 | * county sales tax is 2 percent. 7 | * 8 | * Jesus Hilario Hernandez 9 | * December 17th 2017 10 | * 11 | * 12 | ********************************************************************/ 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | const float STATE_TAX = .04, 19 | COUNTY_TAX = .02; 20 | int purchase = 95; 21 | 22 | float total_state_purchase = purchase * STATE_TAX, 23 | total_county_purchase = purchase * COUNTY_TAX; 24 | 25 | cout << "The total state tax on $95 is $"; 26 | cout << total_state_purchase << endl; 27 | cout << "The total county tax on $95 is $"; 28 | cout << total_county_purchase << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/04.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 04. RESTAURANT BILL 4 | * Write a program that computes the tax and tip on a restaurant 5 | * bill for a patron with a $88.67 meal charge. The tax should be 6 | * 6.75 percent of the meal cost. The tip should be 20 percent 7 | * of the total after adding the tax. Display the meal cost, tax 8 | * amount, tip amount, and total bill on the screen. 9 | * 10 | * Jesus Hilario Hernandez 11 | * December 18th 2017 12 | * 13 | * 14 | ********************************************************************/ 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | const float TAX = .0675, 21 | TIP = .20; 22 | 23 | float meal_charge = 88.67, 24 | meal_tax_amount = meal_charge * TAX, 25 | meal_tip_amount = (meal_charge + meal_tax_amount) * TIP, 26 | total_bill = meal_charge + meal_tax_amount + meal_tip_amount; 27 | 28 | cout << "\nMeal cost: $" << meal_charge << endl; 29 | cout << "Tax amount: $" << meal_tax_amount << endl; 30 | cout << "Tip amount: $" << meal_tip_amount << endl; 31 | cout << "Total bill: $" << total_bill << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/05.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 05. AVERAGE OF VALUES 4 | * To get the average of a series of values, you add the values 5 | * up and then divide the sum by the number of values. Write a 6 | * program that stores the following values in five different 7 | * variables: 28, 32, 37, 24, and 33. The program should first 8 | * calculate the sum of these five variables and store the result 9 | * in a separate variable name sum. Then, the program should divide 10 | * the sum variable by 5 to get the average. Display the average on 11 | * the screen. 12 | * 13 | * Jesus Hilario Hernandez 14 | * December 18th 2017 15 | * 16 | * 17 | ********************************************************************/ 18 | #include 19 | using namespace std; 20 | 21 | int main() 22 | { 23 | const int FIVE = 5; 24 | int a = 28, 25 | b = 32, 26 | c = 37, 27 | d = 24, 28 | e = 33, 29 | sum = a + b + c + d + e, 30 | average = sum / FIVE; 31 | 32 | cout << endl; 33 | cout << "The average is " << average; 34 | cout << endl << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/06.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 06. ANNUAL PAY 4 | * Suppose an employee gets paid every two weeks and earns $2,200 5 | * each pay period. In a year the employee gets paid 26 times. 6 | * Write a program that defines the following variables: 7 | * 8 | * payAmount - 2200.0. 9 | * payPeriods - 26. 10 | * annualPay - to be calculated. 11 | * 12 | * The program should calculate the employee's total annual pay by 13 | * multiplying the employee's pay amount by the number of pay 14 | * periods in a year and store the result in the annualPay variable. 15 | * Display the total annual pay on the screen. 16 | * 17 | * Jesus Hilario Hernandez 18 | * December 18th 2017 19 | * 20 | * 21 | ********************************************************************/ 22 | #include 23 | using namespace std; 24 | 25 | int main() 26 | { 27 | const double PERIODS = 26; 28 | double pay_amount = 2200.0, 29 | annual_pay = pay_amount * PERIODS; 30 | 31 | cout << endl << endl; 32 | cout << "Total annual pay = $" << annual_pay << endl; 33 | cout << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/07.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 07. OCEAN LEVELS 4 | * Assuming the ocean's level is currently rising at about 1.5 5 | * millimeters per year, write a program the displays: 6 | * 7 | * - The number of millimeters higher than the current level that 8 | * the ocean's level will be in 5 years 9 | * 10 | * - The number of millimeters higher than the current level that 11 | * the ocean's level will be in 7 years 12 | * 13 | * - The number of millimeters higher than the current level that 14 | * the ocean's level will be in 10 years 15 | * 16 | * Jesus Hilario Hernandez 17 | * December 19th 2017 18 | * 19 | * 20 | ********************************************************************/ 21 | #include 22 | using namespace std; 23 | 24 | int main() 25 | { 26 | const double MIL_PER_YEAR = 1.5; 27 | int five_years = 5, 28 | seven_years = 7, 29 | ten_years = 10; 30 | float mils_after_5 = MIL_PER_YEAR * five_years, 31 | mils_after_7 = MIL_PER_YEAR * seven_years, 32 | mils_after_10 = MIL_PER_YEAR * ten_years; 33 | 34 | cout << endl; 35 | cout << "The ocean's level will grow " << mils_after_5; 36 | cout << " millimeters after 5 years." << endl; 37 | cout << "The ocean's level will grow " << mils_after_7; 38 | cout << " millimeters after 7 years." << endl; 39 | cout << "The ocean's level will grow " << mils_after_10; 40 | cout << " millimeters after 10 years." << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/08.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 08. TOTAL PURCHASE 4 | * A customer in a store is purchasing five items. The prices of 5 | * five items are 6 | * 7 | * Price of item 1 = $15.95 8 | * Price of item 2 = $24.95 9 | * Price of item 3 = $6.95 10 | * Price of item 4 = $12.95 11 | * Price of item 5 = $3.95 12 | * 13 | * Write a program that holds the prices of the five items in 14 | * five variables. Display each item's price, the subtotal of the 15 | * sale, the amount of sales tax, and the total. Assume the sales 16 | * tax is 7%. 17 | * 18 | * Jesus Hilario Hernandez 19 | * December 19th 2017 20 | * 21 | * 22 | ********************************************************************/ 23 | #include 24 | using namespace std; 25 | 26 | int main() 27 | { 28 | const float SALES_TAX = .07; 29 | 30 | float item_1 = 15.95, 31 | item_2 = 24.95, 32 | item_3 = 6.95, 33 | item_4 = 12.95, 34 | item_5 = 3.95, 35 | subtotal = item_1 + item_2 + item_3 + item_4 + item_5, 36 | total_sales_tax = subtotal * SALES_TAX, 37 | grand_total = subtotal + total_sales_tax; 38 | 39 | cout << endl; 40 | cout << "Item 1 = $" << item_1 << endl; 41 | cout << "Item 2 = $" << item_2 << endl; 42 | cout << "Item 3 = $" << item_3 << endl; 43 | cout << "Item 4 = $" << item_4 << endl; 44 | cout << "Item 5 = $" << item_5 << endl; 45 | cout << "Subtotal = $" << subtotal << endl; 46 | cout << "Sales Tax = $" << total_sales_tax << endl; 47 | cout << "Grand Total = $" << grand_total << endl; 48 | cout << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/09.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 09. CYBORG DATA TYPE SIZES 4 | * You have been given a job as a programmer on a Cyborg 5 | * supercomputer. In order to accomplish some calculations, you 6 | * need to know how many bytes the following data types use: 7 | * char, int, float, and double. 8 | * You don't have any manuals, so you can't look this information up. 9 | * Write a C++ program that will determine the amount of memory 10 | * used by these types and display the information on the screen. 11 | * 12 | * Jesus Hilario Hernandez 13 | * December 19th 2017 14 | * 15 | * 16 | ********************************************************************/ 17 | #include 18 | using namespace std; 19 | 20 | int main() 21 | { 22 | char letter; 23 | int number; 24 | float decimal; 25 | double another_decimal; 26 | 27 | cout << endl; 28 | cout << "A char uses " << sizeof(letter) << " byte(s)." << endl; 29 | cout << "An int uses " << sizeof(number) << " byte(s)." << endl; 30 | cout << "A float uses " << sizeof(decimal) << " byte(s)." << endl; 31 | cout << "A double uses " << sizeof(another_decimal) << " byte(s)." << endl; 32 | cout << endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/10.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 10. MILES PER GALLON 4 | * A car holds 15 gallons of gasoline and can travel 375 miles 5 | * before refueling. Write a program that calculates the number 6 | * of miles per gallon the car gets. Display the result on the 7 | * screen. 8 | * 9 | * Hint: Use the following formula to calculate miles per 10 | * gallon (MPG): 11 | * 12 | * MPG = Miles Driven/Gallons of Gas Used 13 | * 14 | * Jesus Hilario Hernandez 15 | * December 19th 2017 16 | * 17 | * 18 | ********************************************************************/ 19 | #include 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | const float MILES_TRAVELED = 375, 25 | TOTAL_GAL_OF_GAS = 15; 26 | 27 | float MPG = MILES_TRAVELED / TOTAL_GAL_OF_GAS; 28 | 29 | cout << endl; 30 | cout << "A car that holds 15 gallons of gas and that\n"; 31 | cout << "can travel 375 miles before refuling will\n"; 32 | cout << "use " << MPG << " Miles Per Gallon.\n"; 33 | cout << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/11.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 11. DISTANCE PER TANK OF GAS 4 | * A car with a 20-gallon gas tank averages 23.5 mile per gallon 5 | * when driven in town and 28.9 miles per gallon when driven on 6 | * the highway. Write a program that calculates and displays the 7 | * distance the car can travel on one tank of gas when driven in 8 | * town and when driven on the highway. 9 | * 10 | * Hint: The following formula can be used to calculate the distance: 11 | * 12 | * Distance = Number of Gallons X Average Miles per Gallon 13 | * 14 | * Jesus Hilario Hernandez 15 | * December 19th 2017 16 | * 17 | * 18 | ********************************************************************/ 19 | #include 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | const int NUM_OF_GALLONS = 20; 25 | 26 | float in_town_MPG = 23.5, 27 | out_of_town_MPG = 28.5, 28 | 29 | in_town_distance = NUM_OF_GALLONS * in_town_MPG, 30 | out_of_town_distance = NUM_OF_GALLONS * out_of_town_MPG; 31 | 32 | cout << endl; 33 | cout << "The distance a car can travel in town\n"; 34 | cout << "on a 20-gallon gas tank at "; 35 | cout << in_town_MPG << " MPG is "; 36 | cout << in_town_distance << " miles." << endl; 37 | cout << endl; 38 | cout << "The distance a car can travel on the highway\n"; 39 | cout << "on a 20-gallon tank of gas at "; 40 | cout << out_of_town_MPG << " MPG is "; 41 | cout << out_of_town_distance << " miles." << endl; 42 | cout << endl; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/12.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 12. LAND CALCULATION 4 | * One acre of land is equivalent to 43,560 square feet. Write 5 | * a program that calculates the number of acres in a tract of 6 | * land with 391,876 square feet. 7 | * 8 | * Jesus Hilario Hernandez 9 | * December 19th 2017 10 | * 11 | * 12 | ********************************************************************/ 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | const int ONE_ACRE_OF_LAND = 43560; 19 | int num_of_acres = 391876, 20 | total_acreage = num_of_acres / ONE_ACRE_OF_LAND; 21 | 22 | cout << endl; 23 | cout << "-------------------------------------" << endl; 24 | cout << "One acre of land is equvilent to "; 25 | cout << ONE_ACRE_OF_LAND << " sqft." << endl; 26 | cout << "-------------------------------------" << endl; 27 | cout << "There are approximately " << total_acreage; 28 | cout << " acres in a\ntract of land that has "; 29 | cout << num_of_acres << " square feet." << endl; 30 | cout << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/13.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 13. CIRCUIT BOARD PRICE 4 | * An electronic company sells circuit boards at a 35 percent 5 | * profit. Write a program that will calculate the selling price 6 | * of a circuit board that costs $14.95. Display the result on 7 | * the screen. 8 | * 9 | * Jesus Hilario Hernandez 10 | * December 27th 2017 11 | * 12 | * 13 | ********************************************************************/ 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | float profit = .35, 20 | cost = 14.95, 21 | selling_price = (cost * profit) + cost; 22 | 23 | cout << endl; 24 | cout << "An electronic company sells circuit boards\nat "; 25 | cout << (profit * 100); 26 | cout << " percent profit. Therefore, the selling price\n"; 27 | cout << "of a curciut board that costs $" << cost; 28 | cout << " is $" << selling_price << endl; 29 | cout << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/14.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 14. PERSONAL INFORMATION 4 | * Write a program that displays the following pieces of 5 | * information, each on a separate line: 6 | * 7 | * Your name 8 | * Your address, with city, state, and ZIP code 9 | * Your telephone number 10 | * Your college major 11 | * 12 | * Use only a single cout statement to display all of this 13 | * information. 14 | * 15 | * Jesus Hilario Hernandez 16 | * December 15th 2017 17 | * 18 | * 19 | ********************************************************************/ 20 | #include 21 | using namespace std; 22 | 23 | int main() 24 | { 25 | string name = "Jesus Hilario Hernandez", 26 | address = "1234 10 St. Texas", 27 | phone = "123-123-1234", 28 | major = "Comp. Sci."; 29 | 30 | cout << endl; 31 | cout << "Name: " << name << endl; 32 | cout << "Address: " << address << endl; 33 | cout << "Phone: " << phone << endl; 34 | cout << "Major: " << major << endl; 35 | cout << endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/15.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 15. TRIANGLE PATTERN 4 | * Write a program that displays the following pattern on the 5 | * screen: 6 | * 7 | * * 8 | * *** 9 | * ***** 10 | * ******* 11 | * 12 | * Jesus Hilario Hernandez 13 | * December 15th 2017 14 | * 15 | * 16 | ********************************************************************/ 17 | #include 18 | using namespace std; 19 | 20 | int main() 21 | { 22 | cout << endl << endl; 23 | cout << " * " << endl; 24 | cout << " *** " << endl; 25 | cout << " ***** " << endl; 26 | cout << " ******* " << endl; 27 | cout << endl << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/16.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 16. DIAMOND PATTERN 4 | * Write a program that displays the following pattern: 5 | * 6 | * * 7 | * *** 8 | * ***** 9 | * ******* 10 | * ***** 11 | * *** 12 | * * 13 | * 14 | * Jesus Hilario Hernandez 15 | * December 15th 2017 16 | * 17 | * 18 | ********************************************************************/ 19 | #include 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | cout << endl << endl; 25 | cout << " * " << endl; 26 | cout << " *** " << endl; 27 | cout << " ***** " << endl; 28 | cout << " ******* " << endl; 29 | cout << " ***** " << endl; 30 | cout << " *** " << endl; 31 | cout << " * " << endl; 32 | cout << '\n' << endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/17.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 17. STOCK COMMISSION 4 | * Kathryn bought 750 shares of stock at a price of $35.00 per 5 | * share. She must pay her stockbroker a 2 percent commission for 6 | * the transaction. Write a program that calculates and displays 7 | * the following: 8 | * 9 | * - The amount paid for the stock alone (without the commission) 10 | * - The amount of the commission 11 | * - The total amount paid (for the stock plus the commission) 12 | * 13 | * Jesus Hilario Hernandez 14 | * December 27th 2017 15 | * 16 | * 17 | ********************************************************************/ 18 | #include 19 | using namespace std; 20 | 21 | int main() 22 | { 23 | const float COMMISSION_PERCENT = .02; 24 | 25 | int shares_bought = 750, 26 | share_price = 35, 27 | total_paid = shares_bought * share_price; 28 | 29 | float total_commission = total_paid * COMMISSION_PERCENT, 30 | total_amount_paid = total_commission + total_paid; 31 | 32 | cout << endl << endl; 33 | cout << "Total amount paid without commission: $"; 34 | cout << total_paid << endl; 35 | 36 | cout << "Total amount of commission : $"; 37 | cout << total_commission << endl; 38 | 39 | cout << "Total amount paid including commission: $"; 40 | cout << total_amount_paid << endl; 41 | cout << endl << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 02/18.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | * 3 | * 18. ENERGY DRINK CONSUMPTION 4 | * 5 | * A soft drink company recently survayed 16,500 of its 6 | * customers and found that approximately 15% of those surveyed 7 | * purchase one or more energy drinks per week. Of those 8 | * customers who purchase energy drinks, approximately 58% 9 | * of them prefer citrus-flavored energy drinks. Write a 10 | * program that displays the following: 11 | * 12 | * - The approximate # of customers in the survey who purchase 13 | * one or more energy drinks per week 14 | * - The approximate # of customers in the survey who prefer 15 | * citrus-flavored energy drinks 16 | * 17 | * Jesus Hilario Hernandez 18 | * December 15th 2017 19 | * 20 | * 21 | ********************************************************************/ 22 | #include 23 | using namespace std; 24 | 25 | int main() 26 | { 27 | const int NUM_SURVEYED = 16500; 28 | 29 | float percent_1_or_more = .15, 30 | percent_pref_citrus = .58, 31 | total_1_or_more = NUM_SURVEYED * percent_1_or_more, 32 | grand_total = total_1_or_more * percent_pref_citrus; 33 | 34 | cout << endl << endl; 35 | cout << "The approximate # of customers in the survey who\n"; 36 | cout << "purchase on or more energy drinks per week is "; 37 | cout << total_1_or_more << endl; 38 | 39 | cout << "The approximate # of customers in the survey who\n"; 40 | cout << "prefer citrus-flavored energy drinks "; 41 | cout << grand_total << endl; 42 | cout << endl << endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/000.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | float num_of_gallons, 8 | full_tank_miles, 9 | MPG; 10 | 11 | // Ask user for num of gallons 12 | cout << endl; 13 | cout << "Enter number of gallons the car can hold: "; 14 | cin >> num_of_gallons; 15 | 16 | // Ask for num of miles car can drive on full tank 17 | cout << "Enter a number of miles the car can be \n"; 18 | cout << "driven on a full tank of gas: "; 19 | cin >> full_tank_miles; 20 | 21 | // Calculate num of miles the car can drive per gallon 22 | MPG = full_tank_miles / num_of_gallons; 23 | 24 | // Display calculation 25 | cout << "The car gets " << MPG << " miles per gallon."; 26 | cout << '\n' << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Variables 9 | const float CLASS_A_COST_PER_SEAT = 15.00, 10 | CLASS_B_COST_PER_SEAT = 12.00, 11 | CLASS_C_COST_PER_SEAT = 9.00; 12 | 13 | int seats_sold_A, 14 | seats_sold_B, 15 | seats_sold_C; 16 | 17 | float class_A_total, 18 | class_B_total, 19 | class_C_total, 20 | total_income_gen; 21 | 22 | // Ask user for num of seats sold in each class (A,B,C) 23 | cout << endl; 24 | cout << "Enter number of seats sold in Class A: "; 25 | cin >> seats_sold_A; 26 | 27 | cout << "Enter number of seats sold in Class B: "; 28 | cin >> seats_sold_B; 29 | 30 | cout << "Enter number of seats sold in Class C: "; 31 | cin >> seats_sold_C; 32 | 33 | // Calculate cost per section 34 | class_A_total = seats_sold_A * CLASS_A_COST_PER_SEAT; 35 | class_B_total = seats_sold_B * CLASS_B_COST_PER_SEAT; 36 | class_C_total = seats_sold_C * CLASS_C_COST_PER_SEAT; 37 | 38 | // Calculate total income for all seats 39 | total_income_gen = class_A_total + class_B_total + class_C_total; 40 | 41 | // Display income generated from all ticket sales 42 | cout << endl; 43 | cout << setprecision(2) << fixed; 44 | 45 | cout << seats_sold_A << " Class A seats @ $"; 46 | cout << setw(6) << CLASS_A_COST_PER_SEAT; 47 | cout << " = $" << class_A_total << endl; 48 | 49 | cout << seats_sold_B << " Class B seats @ $"; 50 | cout << setw(6) << CLASS_B_COST_PER_SEAT; 51 | cout << " = $" << class_B_total << endl; 52 | 53 | cout << seats_sold_C << " Class C seats @ $"; 54 | cout << setw(6) << CLASS_C_COST_PER_SEAT; 55 | cout << " = $" << class_C_total << endl; 56 | 57 | // Terminate Program 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Variables 9 | const int NUM_OF_SCORES = 5; 10 | 11 | float score_1, 12 | score_2, 13 | score_3, 14 | score_4, 15 | score_5, 16 | average, 17 | total; 18 | 19 | // Describe Program 20 | cout << "\nThis program will calculate the average of "; 21 | cout << "5 test scores.\n"; 22 | cout << "\nPress enter to continue"; 23 | cin.get(); 24 | cout << endl; 25 | 26 | // Ask user to enter 5 test scores 27 | cout << "Enter score #1: "; 28 | cin >> score_1; 29 | cout << "Enter score #2: "; 30 | cin >> score_2; 31 | cout << "Enter score #3: "; 32 | cin >> score_3; 33 | cout << "Enter score #4: "; 34 | cin >> score_4; 35 | cout << "Enter score #5: "; 36 | cin >> score_5; 37 | 38 | // Calculate average test score 39 | total = score_1 + score_2 + score_3 + score_4 + score_5; 40 | average = total / NUM_OF_SCORES; 41 | 42 | cout << setprecision(1) << fixed; 43 | cout << "\nThe average test score is "; 44 | cout << average << '\n' << endl; 45 | 46 | // Terminate Program 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Variables 9 | const int NUM_OF_MONTHS = 3; 10 | 11 | string month_1, 12 | month_2, 13 | month_3; 14 | 15 | float rainfall_month_1, 16 | rainfall_month_2, 17 | rainfall_month_3, 18 | total, 19 | average; 20 | 21 | // Explain program, ask user to press enter to continue 22 | cout << endl; 23 | cout << "---------------------------------------------"; 24 | cout << "\nThis program calculates the average"; 25 | cout << " rainfall for three months." << endl; 26 | cout << "---------------------------------------------"; 27 | cout << "\n\nPress Enter to continue." << endl; 28 | cin.get(); 29 | 30 | // Ask user for month and rainfall 31 | cout << "Enter the name for month 1: "; 32 | cin >> month_1; 33 | cout << "How many inches of rain fell for " << month_1; 34 | cout << ": "; 35 | cin >> rainfall_month_1; 36 | 37 | cout << "Enter the name for month 2: "; 38 | cin >> month_2; 39 | cout << "How many inches of rain fell for " << month_2; 40 | cout << ": "; 41 | cin >> rainfall_month_2; 42 | 43 | cout << "Enter the name for month 3: "; 44 | cin >> month_3; 45 | cout << "How many inches of rain fell for " << month_3; 46 | cout << ": "; 47 | cin >> rainfall_month_3; 48 | 49 | // Calculate total & average 50 | total = rainfall_month_1 + rainfall_month_2 + rainfall_month_3; 51 | average = total / NUM_OF_MONTHS; 52 | 53 | // Display rainfall average 54 | cout << setprecision(2) << fixed; 55 | cout << "\nThe average rainfall for "; 56 | cout << month_1 << ", "; 57 | cout << month_2 << ", and "; 58 | cout << month_3 << " is "; 59 | cout << average << " inches.\n" << endl; 60 | 61 | // Terminate Program 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Variables 9 | const int HUNDRED = 100; 10 | 11 | int males, 12 | females, 13 | total; 14 | 15 | float per_males, 16 | per_females; 17 | 18 | // Ask user for number of males and females 19 | cout << endl; 20 | cout << "How many males in the class: "; 21 | cin >> males; 22 | cout << "How many females: "; 23 | cin >> females; 24 | cout << endl; 25 | 26 | // Calculate percentages 27 | total = males + females; 28 | 29 | per_males = (males / static_cast(total)) * HUNDRED; 30 | 31 | per_females = (females / static_cast(total)) * HUNDRED; 32 | 33 | // Display percentage 34 | cout << setprecision(1) << fixed; 35 | cout << "There are " << total << " students." << endl; 36 | cout << "The percentage of males is %" << per_males; 37 | cout << ",\nwhile, the percentage of females is %"; 38 | cout << per_females << ".\n" << endl; 39 | 40 | // Terminate Program 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/006.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Variables 9 | const float COOKIES = 48, 10 | SUGAR = 1.5, 11 | BUTTER = 1, 12 | FLOUR = 2.75; 13 | 14 | float num_cookies, 15 | total_butter, 16 | total_sugar, 17 | total_flour; 18 | 19 | // Ask user how many cookies to make 20 | cout << "\nHow many cookies do you want to make? "; 21 | cin >> num_cookies; 22 | 23 | // Calculate ingredients 24 | total_sugar = (SUGAR * num_cookies) / COOKIES; 25 | total_flour = (FLOUR * num_cookies) / COOKIES; 26 | total_butter = (BUTTER * num_cookies) / COOKIES; 27 | 28 | // Display amount of ingredients 29 | cout << setprecision(2) << fixed; 30 | cout << "\nTo make " << num_cookies << " cookies "; 31 | cout << "you will need the following ingredients: \n"; 32 | cout << " - " << total_butter << " cups of butter\n"; 33 | cout << " - " << total_sugar << " cups of sugar\n"; 34 | cout << " - " << total_flour << " cups of flour\n"; 35 | cout << endl; 36 | 37 | // Terminate Program 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/007.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Constant variables 9 | const float ADULT_PRICE = 10, 10 | CHILD_PRICE = 6, 11 | PROFIT = .20; 12 | 13 | string movie_name; 14 | 15 | int adult_tkt, 16 | child_tkt; 17 | 18 | float total, 19 | theater_per; 20 | 21 | // Ask for movie name 22 | cout << "\nWhat was the name of the movie? "; 23 | getline(cin, movie_name); 24 | 25 | // Ask for number of adult and child tickets 26 | cout << "\nHow many adult tickets? "; 27 | cin >> adult_tkt; 28 | cout << "How many child tickets? "; 29 | cin >> child_tkt; 30 | 31 | // Calculate gross and net profit 32 | total = (adult_tkt * ADULT_PRICE) + (child_tkt * CHILD_PRICE); 33 | theater_per = total * PROFIT; 34 | 35 | // Display report 36 | cout << setprecision(2) << fixed; 37 | cout << "\nMovie Name: \""; 38 | cout << movie_name << '\"' < 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Variables 9 | const float INDV_WIDGET_WIEGHT = 12.5; 10 | 11 | float pallet_wt, 12 | pallet_with_widgets, 13 | widget_weight, 14 | num_widgets; 15 | 16 | // Ask how much pallet weighs by itself 17 | cout << endl; 18 | cout << "How much does the pallet weight by itself? "; 19 | cin >> pallet_wt; 20 | 21 | // Ask how much pallet weighs with widget stacked on 22 | cout << endl; 23 | cout << "How much does the pallet weight with widgets?"; 24 | cin >> pallet_with_widgets; 25 | 26 | // Calculate how many stacked on pallet 27 | widget_weight = pallet_with_widgets - pallet_wt; 28 | num_widgets = widget_weight / INDV_WIDGET_WIEGHT; 29 | 30 | // Display number of widgets stacked on the pallet. 31 | cout << setprecision(2) << fixed << right << endl; 32 | cout << "Individual widget weight = " << setw(6); 33 | cout << INDV_WIDGET_WIEGHT << endl; 34 | 35 | cout << "Pallet weight = " << setw(6); 36 | cout << pallet_wt << endl; 37 | 38 | cout << "Pallet weight with widgets = " << setw(6); 39 | cout << pallet_with_widgets << endl; 40 | 41 | cout << "Widget weight = " << setw(6); 42 | cout << widget_weight << endl; 43 | 44 | cout << "Number of widgets = " << setw(6); 45 | cout << num_widgets << endl << endl; 46 | 47 | // Terminate Program 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Constant Variables 8 | const int COOKIES_PER_BAG = 30, 9 | SERVINGS_PER_BAG = 10, 10 | CALORIES_PER_SERVING = 300; 11 | 12 | // Variables 13 | float cookies_eaten, 14 | servings_eaten, 15 | calories_consumed, 16 | 17 | cookies_per_serving = COOKIES_PER_BAG / SERVINGS_PER_BAG; 18 | 19 | // Ask how many cookies eaten 20 | cout << endl; 21 | cout << "How many cookies eaten? "; 22 | cin >> cookies_eaten; 23 | 24 | // Calculate number of servings 25 | servings_eaten = cookies_eaten / cookies_per_serving; 26 | 27 | // Calculate caloires consumed 28 | calories_consumed = servings_eaten * CALORIES_PER_SERVING; 29 | 30 | cout << '\n' << setprecision(2) << fixed << right; 31 | 32 | cout << "Number of cookies per bag = "; 33 | cout << setw(8) << COOKIES_PER_BAG << endl; 34 | 35 | cout << "Number of servings per bag = "; 36 | cout << setw(8) << SERVINGS_PER_BAG << endl; 37 | 38 | cout << "Number of calories per serving = "; 39 | cout << setw(8) << CALORIES_PER_SERVING << endl; 40 | 41 | cout << "Number of cookies per serving = "; 42 | cout << setw(8) << cookies_per_serving << endl; 43 | 44 | cout << "Number of cookies eaten = "; 45 | cout << setw(8) << cookies_eaten << endl; 46 | 47 | cout << "Number of servings eaten = "; 48 | cout << setw(8) << servings_eaten << endl; 49 | 50 | cout << "Number of calories consumed = "; 51 | cout << setw(8) << calories_consumed << endl; 52 | 53 | cout << endl; 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/010.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Constane variables 8 | const float INSURE_PERCENTAGE = .80; 9 | 10 | float replacement_cost, 11 | min_amount_insurance; 12 | 13 | // Explain the program 14 | cout << endl; 15 | cout << "------------------------------------" << endl; 16 | cout << "Many financial experts advise that " << endl; 17 | cout << "property owners should insure their " << endl; 18 | cout << "homes or buildings for at least 80 " << endl; 19 | cout << "percent of the amount it would cost " << endl; 20 | cout << " to replace the structure." << endl; 21 | cout << "------------------------------------\n\n"; 22 | cout << "Press \"Enter\" to continute.\n" << endl; 23 | cin.get(); 24 | 25 | cout << "This program will calculate the 80 " << endl; 26 | cout << "percent for you. " << endl; 27 | cout << "------------------------------------\n\n"; 28 | cout << "Enter the replacement cost of your " << endl; 29 | cout << "home or building : $"; 30 | cin >> replacement_cost; 31 | 32 | // Calculate Min amount for insurance 33 | min_amount_insurance = replacement_cost * INSURE_PERCENTAGE; 34 | 35 | // Display min amount to buy for property 36 | cout << setprecision(2) << fixed; 37 | 38 | cout << "The minimum amount of insurance " << endl; 39 | cout << "to buy at %80 is : $"; 40 | cout << min_amount_insurance << endl; 41 | cout << endl; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/011.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Constant 8 | const int TWELVE_MONTHS = 12; 9 | 10 | // Variables 11 | float loan_payment, 12 | insurance, 13 | gas, 14 | oil, 15 | tires, 16 | maintenance, 17 | total_monthly_exp, 18 | annual_cost; 19 | /** 20 | * asks the user to enter the monthly 21 | * costs for the following expenses incurred 22 | * from operating his or her automobile loan 23 | * payment, insurance, gas, oil, tires,and 24 | * maintenance. 25 | */ 26 | cout << "\nEnter the following monthly costs for your"; 27 | cout << "\nautomobile: " << endl; 28 | cout << "Loan Payment : $"; 29 | cin >> loan_payment; 30 | cout << "Insurance : $"; 31 | cin >> insurance; 32 | cout << "Gas : $"; 33 | cin >> gas; 34 | cout << "Oil : $"; 35 | cin >> oil; 36 | cout << "Tires : $"; 37 | cin >> tires; 38 | cout << "Maintenae : $"; 39 | cin >> maintenance; 40 | cout << endl; 41 | 42 | 43 | // Calculate total monthly cost of these expenses 44 | total_monthly_exp = loan_payment + insurance + gas + oil + tires + maintenance; 45 | 46 | // Calculate total annual cost of these expenses 47 | annual_cost = total_monthly_exp * TWELVE_MONTHS; 48 | 49 | // display the total monthly cost of these expenses 50 | cout << "\nMonthly car expenses = $" << total_monthly_exp; 51 | 52 | // display the total annual cost of these expenses 53 | cout << "\nTotal annual cost = $" << annual_cost; 54 | cout << endl; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/012.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Variables 8 | float F, 9 | C; 10 | 11 | // Ask user Celsius Temp 12 | cout << "\nEnter the Celsius Temp: "; 13 | cin >> C; 14 | 15 | // Calculate Fahrenheit 16 | F = ((9.0 / 5.0) * C) + 32; 17 | 18 | // Display Fahrenheit 19 | cout << setprecision(1) << fixed << right << endl; 20 | cout << "Celsius Temp = " << setw(6) << C << endl; 21 | cout << "Fahrenheit Temp = " << setw(6) << F << endl; 22 | cout << endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // constants YEN_PER_DOLLAR and EUROS_PER_DOLLAR 8 | const float YEN_PER_DOLLAR = 111.51, 9 | EUROS_PER_DOLLAR = .85; 10 | 11 | // Variable 12 | float user_amount, 13 | dollar_to_yen, 14 | dollar_to_euro; 15 | 16 | // Ask user for dollar amount 17 | cout << endl; 18 | cout << "Enter U.S. dollar amount: "; 19 | cin >> user_amount; 20 | 21 | // convert U.S. dollar amounts to yen and to euros 22 | dollar_to_yen = user_amount * YEN_PER_DOLLAR; 23 | dollar_to_euro = user_amount *EUROS_PER_DOLLAR; 24 | 25 | // Format currency, fixed-point with two decimal places 26 | cout << setprecision(2) << fixed << endl; 27 | cout << "Dollar amount = $" << user_amount; 28 | cout << "\nYen amount = ¥" << dollar_to_yen; 29 | cout << "\nEuro amount = €" << dollar_to_euro; 30 | cout << '\n' << endl; 31 | 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/014.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Constant 8 | const float STATE_TAX = .04, // 4% 9 | COUNTY_TAX = .02, // 2% 10 | SALES_TAX = .06; // 6% 11 | 12 | // Variables 13 | string month; 14 | int year; 15 | float T, // Total 16 | S, // product sales 17 | total_state_tax, 18 | total_sales_tax, 19 | total_county_tax; 20 | 21 | // Ask user for month, year, and total collected (sales plus sales tax) 22 | cout << "Enter the month for report: "; 23 | cin >> month; 24 | cout << "Enter the year: "; 25 | cin >> year; 26 | cout << "Enter total amount collected for the month: "; 27 | cin >> T; 28 | 29 | // Calculation 30 | S = T / 1.06; 31 | total_county_tax = S * COUNTY_TAX; 32 | total_state_tax = S * STATE_TAX; 33 | total_sales_tax = S * SALES_TAX; 34 | 35 | // Display 36 | cout << setprecision(2) << fixed << right << endl; 37 | cout << "---------------------------------" << endl; 38 | cout << "Month: " << month << endl; 39 | cout << "---------------------------------" << endl; 40 | cout << "Total Collected: $" << setw(10); 41 | cout << T << endl; 42 | cout << "Sales: $" << setw(10); 43 | cout << S << endl; 44 | cout << "County Sales Tax: $" << setw(10); 45 | cout << total_county_tax << endl; 46 | cout << "State Sales Tax: $" << setw(10); 47 | cout << total_state_tax << endl; 48 | cout << "Total Sales Tax: $" << setw(10); 49 | cout << total_sales_tax << endl; 50 | cout << "---------------------------------" << endl; 51 | cout << endl; 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/015.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Constant Variables 8 | const float ASSESSMENT_PER = 0.60, 9 | PROPERTY_TAX_PER = 0.75, 10 | HUNDRED = 100; 11 | 12 | // Variables 13 | float total_assessment_value, 14 | total_property_tax, 15 | value_of_property; 16 | 17 | // Ask user for actual value of property 18 | cout << endl; 19 | cout << "What is that actual value of a property?"; 20 | cin >> value_of_property; 21 | 22 | total_assessment_value = value_of_property * ASSESSMENT_PER; 23 | total_property_tax = (total_assessment_value / HUNDRED) * PROPERTY_TAX_PER; 24 | 25 | // Display assessment value and property tax. 26 | cout << setprecision(2) << fixed << right << endl; 27 | cout << "Property Value: $"; 28 | 29 | cout << setw(12) << value_of_property << endl; 30 | cout << "Assessment value: $"; 31 | 32 | cout << setw(12) << total_assessment_value << endl; 33 | cout << "Property Tax: $"; 34 | 35 | cout << setw(12) << total_property_tax << endl; 36 | 37 | cout << endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/016.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Constant Variables 8 | const float ASSESSMENT_PER = 0.60; 9 | 10 | const int FIVE_THOUSAND = 5000, 11 | HUNDRED = 100, 12 | FOUR = 4; 13 | 14 | // Variables 15 | float value_of_property, 16 | current_tax_rate, 17 | total_annual_property_tax, 18 | quarterly_tax_bill; 19 | 20 | cout << endl; 21 | cout << "------------------------------------------\n"; 22 | cout << "What is the value of the property? "; 23 | cin >> value_of_property; 24 | cout << "What is tha current tax rate? "; 25 | cin >> current_tax_rate; 26 | cout << "------------------------------------------\n"; 27 | 28 | // Calculate annual property tax for senior homeowner 29 | total_annual_property_tax = (((value_of_property * ASSESSMENT_PER) - FIVE_THOUSAND) / HUNDRED) * current_tax_rate; 30 | 31 | // Calculate the quarterly tax bill 32 | quarterly_tax_bill = total_annual_property_tax / FOUR; 33 | 34 | // Display annual property tax and quarterly tax bill. 35 | cout << setprecision(2) << fixed << right; 36 | cout << "-------------------------------------------\n"; 37 | cout << "Value of the property: $"; 38 | cout << setw(11) << value_of_property; 39 | 40 | cout << "\nAnnual property tax: $"; 41 | cout << setw(11) << total_annual_property_tax; 42 | 43 | cout << "\nQuarterly tax bill: $"; 44 | cout << setw(11) << quarterly_tax_bill; 45 | 46 | cout << "\n-----------------------------------------\n"; 47 | cout << endl; 48 | 49 | // Terminate Program 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/017.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For rand and srand functions 3 | #include // For the time function 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Constant Variables 9 | const int MIN_VALUE = 100, 10 | MAX_VALUE = 1000; 11 | 12 | // Variables 13 | int num_1, 14 | num_2, 15 | answer; 16 | 17 | // Get system time 18 | unsigned seed = time(0); 19 | 20 | // Seed the random number generator. 21 | srand(seed); 22 | 23 | // Explain Program 24 | cout << endl; 25 | cout << "-----------------------------------------\n\n"; 26 | cout << "This program displays two random numbers\n"; 27 | cout << "to be added.\n" << endl; 28 | 29 | // Generate two random numbers 30 | num_1 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE; 31 | num_2 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE; 32 | 33 | 34 | answer = num_1 + num_2; 35 | // Display the problem 36 | cout << num_1 << " + " << num_2 << " = ? \n" << endl; 37 | 38 | // Ask user to press ENTER to continue 39 | cout << "Press \"Enter\" to see solution"; 40 | cin.get(); 41 | cout << endl; 42 | 43 | // Display the answer to the problem 44 | cout << num_1 << " + " << num_2 << " = "; 45 | cout << answer << endl; 46 | cout << "\n-----------------------------------------\n"; 47 | cout << endl; 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/018.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // pow(5, 10); 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Constant 9 | const int ONE = 1, 10 | HUNDRED = 100; 11 | 12 | // Variables 13 | float principal, // Principal 14 | interest_rate, 15 | amount_after_a_year, // Amount 16 | total_interest, 17 | amount_in_savings; 18 | 19 | int times_interest_compounded; // T 20 | 21 | // asks for the principal, interest rate, and number of times the interest is compounded 22 | cout << endl; 23 | cout << "What is the pricipal balance in the" << endl; 24 | cout << "savings account? "; 25 | cin >> principal; 26 | 27 | cout << "What is the interest rate? %"; 28 | cin >> interest_rate; 29 | 30 | cout << "What is the number of times the interest\n"; 31 | cout << "is compounded during a year? "; 32 | cin >> times_interest_compounded; 33 | 34 | // Calculate 35 | interest_rate /= HUNDRED; // 4.25% == .0425 36 | // Amount = Principal * (1 + Rate/T)^T 37 | amount_after_a_year = principal * pow((ONE + interest_rate/times_interest_compounded), times_interest_compounded); 38 | 39 | total_interest = amount_after_a_year - principal; 40 | 41 | interest_rate *= HUNDRED; 42 | 43 | // Display 44 | cout << setprecision(2) << fixed << right << endl; 45 | 46 | cout << "Interest Rate: " << setw(10); 47 | cout << interest_rate << '%' << endl; 48 | 49 | cout << "Times Compounded: " << setw(10); 50 | cout << times_interest_compounded << endl; 51 | 52 | cout << "Principal: $" << setw(10); 53 | cout << principal << endl; 54 | 55 | cout << "Interest: $" << setw(10); 56 | cout << total_interest << endl; 57 | 58 | cout << "Amount after a year: $" << setw(10); 59 | cout << amount_after_a_year << endl; 60 | 61 | // Terminate Program 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/020.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // pow() 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Constants 9 | const float SLICE_AREA_SIZE = 14.125, 10 | PI = 3.14159; 11 | 12 | const int TWO = 2; 13 | 14 | // Variables 15 | float d, // Diameter 16 | r, // Radius 17 | Area, 18 | num_of_slices; 19 | 20 | // Ask the user for the diameter of the pizza in inches. 21 | cout << endl; 22 | cout << "What is the diameter of the pizza in inches? "; 23 | cin >> d; 24 | 25 | // Calculate the # of slices that may be taken from a pizza 26 | // of that size 27 | r = d / TWO; // Radius 28 | Area = PI * pow(r, TWO); 29 | num_of_slices = Area / SLICE_AREA_SIZE; 30 | 31 | // Display a message telling the number of slices. 32 | cout << setprecision(1) << fixed; 33 | cout << "Number of slices = "; 34 | cout << num_of_slices << endl; 35 | cout << endl; 36 | 37 | // Terminate Program 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/021.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // setprecision(3) == .000 3 | #include // pow(6, 3) == 6^3 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Constants 9 | const float SLICE_AREA_SIZE = 14.125, 10 | PI = 3.14159; 11 | 12 | const int TWO = 2, 13 | FOUR = 4; 14 | 15 | // Variables 16 | float d, 17 | r, 18 | Area, 19 | num_of_slices, 20 | slices_needed, 21 | num_of_pizzas; 22 | 23 | int num_of_people; 24 | 25 | // Ask the user for the number of people who will be at the party 26 | cout << "\nWhat is the number of people who will be at the party? "; 27 | cin >> num_of_people; 28 | 29 | // Ask the user for the diameter of the pizza in inches. 30 | cout << "What is the diameter of the pizza in inches? "; 31 | cin >> d; 32 | 33 | 34 | // Calculate the # of slices 35 | // Num Slices needed 36 | slices_needed = num_of_people * FOUR; 37 | 38 | // Calculate Radius 39 | r = d / TWO; 40 | 41 | // Calculate Area 42 | Area = PI * pow(r, TWO); 43 | 44 | // Num Slices per pizza 45 | num_of_slices = Area / SLICE_AREA_SIZE; 46 | 47 | // Num pizzas needed 48 | num_of_pizzas = slices_needed / num_of_slices; 49 | 50 | 51 | // Display a message telling the number of slices. 52 | cout << setprecision(1) << fixed; 53 | cout << "Number of people = "; 54 | cout << num_of_people << endl; 55 | 56 | cout << "Number of slices needed = "; 57 | cout << slices_needed << endl; 58 | 59 | cout << "Number of slices per Pizza = "; 60 | cout << num_of_slices << endl; 61 | 62 | cout << "Number of pizzas to order = "; 63 | cout << num_of_pizzas << endl << endl; 64 | 65 | // Terminate Program 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/022.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // setprecision() 3 | #include // sin(), tan(), and cos() 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | // Variables 10 | double angle; 11 | 12 | // Ask the user for an angle in radians 13 | cout << "\nWhat is an angle in radians? "; 14 | cin >> angle; 15 | 16 | // Display the sine, cosine, and tangent of the angle 17 | cout << setprecision(4) << fixed; 18 | cout << "Angle in radians = " << angle << endl; 19 | cout << "Sine of the angle = " << sin(angle) << endl; 20 | cout << "Cosine of angle = " << cos(angle) << endl; 21 | cout << "Tangent of angle = " << tan(angle) << endl; 22 | cout << endl; 23 | 24 | // Terminate Program 25 | return 0; 26 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 03/024.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Variables 8 | string username, 9 | city, 10 | college, 11 | profession, 12 | animal, 13 | pet_name, 14 | age; 15 | 16 | // ask the user to enter info 17 | cout << endl; 18 | cout << "What is your name? "; 19 | getline(cin, username); 20 | 21 | cout << "Your age? "; 22 | getline(cin, age); 23 | 24 | cout << "What city to you live? "; 25 | getline(cin, city); 26 | 27 | cout << "What college? "; 28 | getline(cin, college); 29 | 30 | cout << "What do you do for a living? "; 31 | getline(cin, profession); 32 | 33 | cout << "What type of animal? "; 34 | getline(cin, animal); 35 | 36 | cout << "Pet's name? "; 37 | getline(cin, pet_name); 38 | 39 | cout << endl; 40 | 41 | // Display user info in a sentence 42 | cout << "There once was a person named "; 43 | cout << username; 44 | 45 | cout << " who lived in "; 46 | cout << city; 47 | 48 | cout << ". At the age of " << age << ", "; 49 | cout << username; 50 | 51 | cout << " went to college at " << college << ". "; 52 | cout << username; 53 | 54 | cout << " graduated and went to work as a "; 55 | cout << profession; 56 | 57 | cout << ". Then, " << username << " adopted a(n) "; 58 | cout << animal << " named " << pet_name; 59 | 60 | cout << ". They both lived happily ever after!\n" << endl; 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/000.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | int num1, num2; 8 | 9 | // Ask the user to enter two numbers 10 | cout << endl; 11 | cout << "Enter Number 1: "; 12 | cin >> num1; 13 | cout << "Enter Number 2: "; 14 | cin >> num2; 15 | 16 | // Formatting 17 | cout << endl; 18 | // Use a decision structure to determine largest and smallest num 19 | if (num1 < num2) 20 | { 21 | cout << "Number 1 (" << num1 << ")"; 22 | cout << " is smallest" << endl; 23 | 24 | cout << "Number 2 (" << num2 << ")"; 25 | cout << " is largest" << endl; 26 | } 27 | else 28 | { 29 | cout << "Number 1 (" << num1 << ")"; 30 | cout << "is largest" << endl; 31 | 32 | cout << "Number 2 (" << num2 << ")"; 33 | cout << " is smallest" << endl; 34 | } 35 | cout << endl; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | int userNum; 8 | 9 | // Ask the user to enter a number between 1 and 10. (within range) 10 | cout << "Enter a number between 1 and 10: "; 11 | cin >> userNum; 12 | 13 | // Use switch statement to display the Roman numeral version of that #. 14 | switch (userNum) 15 | { 16 | case 1: 17 | cout << "The Roman numeral for "; 18 | cout << userNum << " is I\n"; 19 | break; 20 | case 2: 21 | cout << "The Roman numeral for "; 22 | cout << userNum << " is II\n"; 23 | break; 24 | case 3: 25 | cout << "The Roman numeral for "; 26 | cout << userNum << " is III\n"; 27 | break; 28 | case 4: 29 | cout << "The Roman numeral for "; 30 | cout << userNum << " is IV\n"; 31 | break; 32 | case 5: 33 | cout << "The Roman numeral for "; 34 | cout << userNum << " is V\n"; 35 | break; 36 | case 6: 37 | cout << "The Roman numeral for "; 38 | cout << userNum << " is VI\n"; 39 | break; 40 | case 7: 41 | cout << "The Roman numeral for "; 42 | cout << userNum << " is VII\n"; 43 | break; 44 | case 8: 45 | cout << "The Roman numeral for "; 46 | cout << userNum << " is VIII\n"; 47 | break; 48 | case 9: 49 | cout << "The Roman numeral for "; 50 | cout << userNum << " is XI\n"; 51 | break; 52 | case 10: 53 | cout << "The Roman numeral for "; 54 | cout << userNum << " is X\n"; 55 | break; 56 | default: 57 | cout << "You must enter a number between 1 and 10\n"; 58 | cout << "Re-run the program and try again.\n"; 59 | break; 60 | } 61 | 62 | // Terminate program 63 | return 0; 64 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | int month, 8 | year, 9 | day; 10 | 11 | string magic; 12 | 13 | // Ask user to enter month, day, and year 14 | cout << "Enter the month "; 15 | cout << "(e.g. January = 1, June = 6): "; 16 | cin >> month; 17 | 18 | cout << "Enter the day "; 19 | cout << "(e.g. 1, 2, 3, 4, etc..): "; 20 | cin >> day; 21 | 22 | cout << "Enter the year "; 23 | cout << "(e.g. 1978 = 78, 1991 = 91, 2016 = 16): "; 24 | cin >> year; 25 | 26 | // Calculation 27 | magic = (month * day == year) ? "magic" : "not magic"; 28 | 29 | // if (month * day == year) 30 | // magic = "magic"; 31 | // else 32 | // magic = "not magic"; 33 | 34 | // Determine whether month * day = year 35 | cout << "\nThe date is " << magic << endl; 36 | 37 | // Terminate program 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | float l1, 8 | l2, 9 | w1, 10 | w2, 11 | rectangle1, 12 | rectangle2; 13 | 14 | // Ask the user for l and w of 2 rectangles 15 | cout << endl; 16 | cout << "What is the length of the first rectangle: "; 17 | cin >> l1; 18 | cout << "What is the width: "; 19 | cin >> w1; 20 | cout << "What is the length of the second rectangle: "; 21 | cin >> l2; 22 | cout << "The width: "; 23 | cin >> w2; 24 | cout << endl; 25 | 26 | // Calculate area for each rectangle 27 | rectangle1 = l1 * w1; 28 | rectangle2 = l2 * w2; 29 | 30 | // Decide & display rectangle with greatest area 31 | if (rectangle1 > rectangle2) 32 | cout << "Area for rectangle 1 is greater." << endl; 33 | else if (rectangle1 < rectangle2) 34 | cout << "Area for rectangle 2 is greater." << endl; 35 | else if (rectangle1 == rectangle2) 36 | cout << "Area for both rectangles is the same." << endl; 37 | 38 | // Formatting last line. 39 | cout << endl; 40 | 41 | // Terminate program 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // pow(8, 2) == 8^2 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Variables 8 | float BMI, 9 | weight, 10 | height; 11 | 12 | // Ask user for height & weight 13 | cout << "Enter weight(in pounds): " << endl; 14 | cin >> weight; 15 | 16 | cout << "Enter height(in inches, e.g. 5' 9\" = 69): " << endl; 17 | cin >> height; 18 | 19 | // Calculate & display the person's (BMI) 20 | BMI = weight * (703 / pow(height, 2)); 21 | 22 | // Display BMI 23 | cout << "BMI = " << BMI << endl; 24 | 25 | // Decision Structure 26 | if (BMI >= 18.5 && BMI <= 25) 27 | cout << "Weight optimal." << endl; 28 | else if (BMI >= 0 && BMI <= 18.5) 29 | cout << "Weight underweight." << endl; 30 | else if (BMI >= 25) 31 | cout << "Weight overweight." << endl; 32 | 33 | // Terminate program 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/006.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | float weight, 8 | mass; 9 | 10 | // Ask the user for object's mass 11 | cout << endl; 12 | cout << "What is the object's mass: "; 13 | cin >> mass; 14 | 15 | // Calculate object's weight 16 | weight = mass * 9.8; 17 | 18 | // Display weight. 19 | cout << "\nObject's weight = "; 20 | cout << weight << endl << endl; 21 | 22 | // Decision Statement 23 | if (weight >= 1000) 24 | cout << "Too heavy."; 25 | else if (weight <= 10) 26 | cout << "Too light."; 27 | else 28 | cout << "Neither heavy or light."; 29 | 30 | // Formating 31 | cout << endl << endl; 32 | 33 | // Terminate program 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/007.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // setprecision() and fixed 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Variables 8 | int seconds; 9 | float num_mins; 10 | 11 | // Ask the user to enter a number of seconds 12 | cout << "\nEnter a number of seconds: "; 13 | cin >> seconds; 14 | 15 | // Format decimal percision at 2 decimal places. 16 | cout << setprecision(2) << fixed; 17 | 18 | // calculate 19 | if (seconds <= 59) 20 | { 21 | cout << seconds; 22 | cout << " is less that 60 seconds." << endl; 23 | } 24 | else if (seconds >= 60 && seconds < 3600) 25 | { 26 | num_mins = seconds / 60.0; 27 | cout << num_mins << " minutes in "; 28 | cout << seconds << " seconds" << endl; 29 | } 30 | else if (seconds >= 3600 && seconds < 86400) 31 | { 32 | num_mins = seconds / 3600.0; 33 | cout << num_mins << " hours in "; 34 | cout << seconds << " seconds" << endl; 35 | } 36 | else if (seconds >= 86400) 37 | { 38 | num_mins = seconds / 86400.0; 39 | cout << num_mins << " days in "; 40 | cout << seconds << " seconds" << endl; 41 | } 42 | 43 | // Formatting 44 | cout << endl << endl; 45 | 46 | // Terminate program 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Constant variables 7 | const float ONE_DOLLAR = 1.00, 8 | PENNY = 0.01, 9 | NICKEL = 0.05, 10 | DIME = 0.10, 11 | QUARTER = 0.25; 12 | 13 | // Variables 14 | float pennies, 15 | nickels, 16 | dimes, 17 | quarters, 18 | total_coins; 19 | 20 | // explain 21 | cout << "\nLet's see if your coins equal 1 dollar.\n"; 22 | cout << endl; 23 | 24 | // Ask for pennies, nickels, dimes, and quarters. 25 | cout << "How many pennies do you have? "; 26 | cin >> pennies; 27 | cout << "Nickels? "; 28 | cin >> nickels; 29 | cout << "Dimes? "; 30 | cin >> dimes; 31 | cout << "Quarters? "; 32 | cin >> quarters; 33 | 34 | // Format line break 35 | cout << endl; 36 | 37 | // Calculate total coins 38 | total_coins += ((pennies *= PENNY) + 39 | (nickels *= NICKEL) + 40 | (dimes *= DIME) + 41 | (quarters *= QUARTER)); 42 | 43 | // Decision statement 44 | if (total_coins == ONE_DOLLAR) 45 | { 46 | cout << '$' << total_coins; 47 | cout << " equal to $1." << endl; 48 | } 49 | else if (total_coins < ONE_DOLLAR) 50 | { 51 | cout << '$' << total_coins; 52 | cout << " is less than $1. Re-run program and "; 53 | cout << " Try again.\n"; 54 | } 55 | else if (total_coins > ONE_DOLLAR) 56 | { 57 | cout << '$' << total_coins; 58 | cout << " greater than $1. Re-run program and "; 59 | cout << " Try again.\n"; 60 | } 61 | 62 | // Format line break 63 | cout << endl; 64 | 65 | // Terminate program 66 | return 0; 67 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/010.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | int year, 8 | month; 9 | 10 | // Ask user to enter month 11 | cout << endl; 12 | cout << "Enter the month (1 - 12): "; 13 | cin >> month; 14 | cout << "Enter the year (up to 9000): "; 15 | cin >> year; 16 | 17 | // Line Break 18 | cout << endl; 19 | 20 | // Error check for valid year 21 | if (year >= 0 && year <= 9000) 22 | { 23 | // Decision statement for month 24 | switch (month) 25 | { 26 | case 1: 27 | case 3: 28 | case 5: 29 | case 7: 30 | case 8: 31 | case 10: 32 | case 12: 33 | cout << "31 days"; 34 | break; 35 | 36 | case 4: 37 | case 6: 38 | case 9: 39 | case 11: 40 | cout << "30 days"; 41 | break; 42 | 43 | case 2: 44 | // Decision statement for leap year 45 | if (year % 100 == 0) 46 | { 47 | if (year % 400 == 0) 48 | cout << "29 days" << endl; 49 | else 50 | cout << "28 days" << endl; 51 | } 52 | else if (year % 100 != 0) 53 | { 54 | if (year % 4 == 0) 55 | cout << "29 days" << endl; 56 | else 57 | cout << "28 days" << endl; 58 | } 59 | break; 60 | 61 | default: 62 | cout << "Invalid month. Rerun program. Try again." << endl; 63 | } 64 | } 65 | else 66 | { 67 | cout << "Invalid year. Rerun program. "; 68 | cout << "Try again." << endl; 69 | } 70 | 71 | // line break 72 | cout << endl; 73 | 74 | // Terminate program 75 | return 0; 76 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/011.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For rand() and srand() 3 | #include // For time() 4 | 5 | using namespace std; 6 | int main() 7 | { 8 | // Constant Variables 9 | const int MIN_NUM = 99, 10 | MAX_NUM = 999; 11 | 12 | // Variables 13 | int user_answer, 14 | rand_num_1, 15 | rand_num_2, 16 | rand_num_answer; 17 | 18 | // Assign system time to a variable. 19 | unsigned seed = time(0); 20 | 21 | // Seed random number generator with system time 22 | srand(seed); 23 | 24 | // Generate two random numbers 25 | rand_num_1 = (rand() % (MAX_NUM - MIN_NUM + 1)) + MIN_NUM; 26 | 27 | rand_num_2 = (rand() % (MAX_NUM - MIN_NUM + 1)) + MIN_NUM; 28 | 29 | // sum of the two random numbers. 30 | rand_num_answer = rand_num_1 + rand_num_2; 31 | 32 | // Display math problem & receive answer 33 | cout << "\n " << rand_num_1 << endl; 34 | cout << " +" << rand_num_2 << endl; 35 | cout << "------" << endl << " "; 36 | cin >> user_answer; 37 | 38 | // Decision statement & display 39 | if (user_answer == rand_num_answer) 40 | cout << "\nCongratulations!"; 41 | else 42 | { 43 | cout << "\nOops. Correct answer here: "; 44 | cout << rand_num_answer; 45 | } 46 | 47 | // Format line break. 48 | cout << endl << endl; 49 | 50 | // Terminate program 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Variables 7 | int books_purchased; 8 | 9 | // Get number of books user purchased this month. 10 | cout << "\nHow many books did you purchase this month: "; 11 | cin >> books_purchased; 12 | 13 | // Format a line break 14 | cout << endl; 15 | 16 | // Decision statement to determine points earned 17 | if (books_purchased == 0) 18 | cout << "0 points earned\n"; 19 | else if (books_purchased == 1) 20 | cout << "5 points earned\n"; 21 | else if (books_purchased == 2) 22 | cout << "15 points earned\n"; 23 | else if (books_purchased == 3) 24 | cout << "30 points earned\n"; 25 | else if (books_purchased >= 4) 26 | cout << "60 points earned\n"; 27 | else 28 | { 29 | cout << "Oops. A number 0 or greater must be entered."; 30 | cout << "\nPlease rerun the program and try again.\n"; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/018.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | float calories_from_fat, 6 | fat_grams, 7 | percentage_of_calories, 8 | total_calories; 9 | 10 | cout << "\nEnter number of calories: "; 11 | cin >> total_calories; 12 | 13 | if (total_calories > 0) 14 | { 15 | cout << "Enter number of fat grams: "; 16 | cin >> fat_grams; 17 | 18 | if (fat_grams > 0) 19 | { 20 | calories_from_fat = fat_grams * 9; 21 | percentage_of_calories = (calories_from_fat / total_calories) * 100; // .45 == 45 22 | 23 | if (calories_from_fat > total_calories) 24 | { 25 | cout << "\nCalories from fat cannot be greater\n"; 26 | cout << "then the total number of calories." << endl; 27 | cout << "Either the total calories or fat\n"; 28 | cout << "grams were entered incorrectly." << endl; 29 | cout << "Rerun the program and try again." << endl << endl; 30 | } 31 | else 32 | { 33 | cout << "Total calories = " << total_calories << " cal"<< endl; 34 | cout << "Total fat grams = " << fat_grams << " g" << endl; 35 | cout << "Total calories from fat = " << calories_from_fat << " cal from fat"<< endl; 36 | cout << "Total percentage of calories from fat = " << percentage_of_calories << '%' << endl; 37 | cout << endl; 38 | } 39 | } 40 | else 41 | { 42 | cout << "We're sorry. Fat grams must be more than 0.\n"; 43 | cout << "Please rerun the progarm and try again." << endl; 44 | } 45 | } 46 | else 47 | { 48 | cout << "We're sorry. Total calories must be more than 0.\n"; 49 | cout << "Please rerun the progarm and try again." << endl; 50 | } 51 | 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/019.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | double wavelength; 6 | 7 | cout << "\nEnter wavelength (in meters): "; 8 | cin >> wavelength; // .0001 9 | 10 | if (wavelength >= 1E-2) 11 | cout << "Radio Waves" << endl; 12 | else if (wavelength <= 1E-2 && wavelength >= 1E-3) 13 | cout << "Microwaves" << endl; 14 | else if (wavelength <= 1E-3 && wavelength >= 7E-7) 15 | cout << "Infrared" << endl; 16 | else if (wavelength <= 7E-7 && wavelength >= 4E-7) 17 | cout << "Visible Light" << endl; 18 | else if (wavelength <= 4E-7 && wavelength >= 1E-8) 19 | cout << "Ultraviolet" << endl; 20 | else if (wavelength <= 1E-8 && wavelength >= 1E-11) 21 | cout << "X Rays" << endl; 22 | else if (wavelength <= 1E-11) 23 | cout << "Gamma Rays" << endl; 24 | 25 | cout << endl; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 04/022.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | float user_temp; 6 | 7 | cout << "Enter temperature: "; 8 | cin >> user_temp; 9 | 10 | cout << "\nAt " 11 | << user_temp 12 | << " F these substances will either " 13 | << " freeze or boil.\n"; 14 | 15 | if (user_temp <= 32) 16 | { 17 | cout << "\nFreeze:\n"; 18 | cout << "--------" << endl; 19 | cout << "Water" << endl; 20 | 21 | if (user_temp <= -38) 22 | { 23 | cout << "Mercury" << endl; 24 | 25 | if (user_temp <= -173) 26 | { 27 | cout << "Ethyl alcohol" << endl; 28 | 29 | if (user_temp <= -362) 30 | { 31 | cout << "Oxygen" << endl; 32 | } 33 | } 34 | } 35 | cout << endl; 36 | } 37 | 38 | if (user_temp >= -306) 39 | { 40 | cout << "Boil: \n"; 41 | cout << "--------" << endl; 42 | cout << "Oxygen." << endl; 43 | 44 | if (user_temp >= 172) 45 | { 46 | cout << "Ethyl alcohol." << endl; 47 | 48 | if (user_temp >= 212) 49 | { 50 | cout << "Water" << endl; 51 | 52 | if (user_temp >= 676) 53 | { 54 | cout << "Mercury" << endl; 55 | } 56 | } 57 | } 58 | cout << endl; 59 | } 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/000.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | // Declare input file stream object. 8 | ifstream inputFile; 9 | 10 | // Variables 11 | double sum = 0, 12 | total = 0, 13 | average, 14 | number; 15 | 16 | // 1. Open file 17 | // 2. Process file 18 | // 3. Close file 19 | 20 | inputFile.open("Random.txt"); 21 | 22 | // Check if file opens successfully 23 | if (inputFile.fail()) 24 | { 25 | // Display error if file did not open 26 | cout << "Error opening the file." << endl; 27 | } 28 | else 29 | { 30 | // 2. Process file 31 | // read from "Random.txt" file until the eof is reached. 32 | while (inputFile >> number) 33 | { 34 | // Add each number in the file to the sum variable 35 | sum += number; 36 | // Keep a running total of the number of numbers in "Random.txt" file 37 | total++; 38 | } 39 | 40 | // Display the total number of numbers in "Random.txt" file 41 | cout << "There are " << total << " numbers in " 42 | << "the Random.txt file" 43 | << endl; 44 | 45 | // Display the sum of all number in "Random.txt" file 46 | cout << "The sum of all numbers in Random.txt is " 47 | << sum 48 | << endl; 49 | 50 | // Find the average of all the numbers in the file "Random.txt" 51 | average = sum / total; 52 | 53 | // Display average 54 | cout << "The average of those numbers is " 55 | << average 56 | << endl; 57 | 58 | // 3. Close file 59 | inputFile.close(); 60 | } 61 | 62 | // Terminate 63 | return 0; 64 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int user_number, 6 | total = 0; 7 | 8 | cout << "\nEnter a positive number: "; 9 | cin >> user_number; 10 | 11 | while (user_number < 0) 12 | { 13 | cout << "ERROR: a positive number must be chosen\n"; 14 | cout << "Enter a positive number: "; 15 | cin >> user_number; // < 0 16 | } 17 | 18 | for (int i = 1; i <= user_number; i++) 19 | { 20 | cout << total 21 | << "+" 22 | << i 23 | << " = "; 24 | total += i; 25 | cout << total << endl; 26 | } 27 | 28 | cout << "Total = " 29 | << total 30 | << endl 31 | << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | char ascii_code_number; 6 | 7 | for (int i = 0; i <= 127; i++) 8 | { 9 | ascii_code_number = i; 10 | cout << ascii_code_number << " "; 11 | 12 | if (i % 16 == 0) 13 | cout << endl; 14 | } 15 | cout << endl 16 | << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | const float RISE_PER_YEAR = 1.5; 6 | 7 | float number_of_millimeters_each_year = 0; 8 | 9 | cout << "--------------------------------------------------\n"; 10 | for (int i = 1; i <= 25; i++) 11 | { 12 | number_of_millimeters_each_year += RISE_PER_YEAR; 13 | cout << "Year " << i << ": " 14 | << number_of_millimeters_each_year 15 | << endl; 16 | } 17 | cout << "--------------------------------------------------\n"; 18 | 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | const float CALORIES_BURNED_PER_MIN = 3.6; 6 | 7 | float calories_burned = 0; 8 | 9 | for (int i = 5; i <= 30; i += 5) 10 | { 11 | calories_burned = (i * CALORIES_BURNED_PER_MIN); 12 | 13 | cout << "Calories burned after " << i << " minutes = " 14 | << calories_burned << endl; 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | const float CURRENT_CHARGE_PER_YEAR = 2500, 6 | INCREASE_PERCENTAGE_PER_YEAR = .04; 7 | 8 | float fee = CURRENT_CHARGE_PER_YEAR; 9 | 10 | for(int i = 0; i < 6; i++) 11 | { 12 | cout << "Fee for year " << (i + 1) << " = " 13 | << fee 14 | << endl; 15 | // fee += (fee * INCREASE_PERCENTAGE_PER_YEAR); 16 | // or 17 | fee += (CURRENT_CHARGE_PER_YEAR * 18 | INCREASE_PERCENTAGE_PER_YEAR); 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/006.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int speed, 6 | hours_traveled, 7 | distance = 0; 8 | 9 | cout << "What is the speed? "; 10 | while(!(cin >> speed) || (speed < 0)) 11 | { 12 | cout << "ERROR: number of speed must be \n" 13 | << "greater that 0 \n" 14 | << endl; 15 | cin.clear(); 16 | cin.ignore(1200, '\n'); 17 | } 18 | 19 | cout << "What is the hours traveled? "; 20 | while(!(cin >> hours_traveled) || (hours_traveled < 1)) 21 | { 22 | cout << "ERROR: number of hours traveled must be \n" 23 | << "greater that 1 \n" 24 | << endl; 25 | cin.clear(); 26 | cin.ignore(1200, '\n'); 27 | } 28 | 29 | cout << "Hour Distance Traveled" << endl; 30 | cout << "------------------------" << endl; 31 | 32 | for(int i = 0; i < hours_traveled; i++) 33 | { 34 | distance += speed; 35 | cout << " " << (i + 1) << " " 36 | << distance 37 | << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/007.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | const int INCREMENTAL_AMOUNT = 2; 8 | 9 | int num_of_days; 10 | 11 | float daily_salary = .01, 12 | total_salary = 0; 13 | 14 | cout << "\nEnter number of days: "; 15 | while(!(cin >> num_of_days) || (num_of_days < 1)) 16 | { 17 | cout << "ERROR: number of days must be \n" 18 | << "greater than 1 \n" 19 | << endl; 20 | cin.clear(); 21 | cin.ignore(1200, '\n'); 22 | } 23 | 24 | cout << "\n Day Amount Earned" << endl; 25 | cout << "--------------------------------------\n"; 26 | cout << setprecision(2) << fixed; 27 | 28 | for(int i = 0; i < num_of_days; i++) 29 | { 30 | total_salary += daily_salary; 31 | cout << " " << (i + 1) << " $"; 32 | cout << daily_salary << endl; 33 | daily_salary *= INCREMENTAL_AMOUNT; 34 | } 35 | 36 | cout << "\nTotal salary for " << num_of_days; 37 | cout << " days = $" << total_salary << endl; 38 | cout << endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/010.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | const int NUM_OF_MONTHS = 12; 8 | 9 | int num_of_years, 10 | total_num_of_months = 0; 11 | 12 | double rainfall, 13 | total_rainfall, 14 | average_rainfall_per_month; 15 | 16 | cout << "Enter number of years: "; 17 | cin >> num_of_years; 18 | cout << "You've entered " 19 | << num_of_years 20 | << " years." 21 | << endl; 22 | 23 | for(int i = 0; i < num_of_years; i++) 24 | { 25 | cout << "Year " << (i + 1) << ": " << endl; 26 | for(int j = 0; j < NUM_OF_MONTHS; j++) 27 | { 28 | cout << "How much rainfall (inches)\n" 29 | << "for month " << (j + 1) << ": "; 30 | cin >> rainfall; 31 | 32 | total_rainfall += rainfall; 33 | 34 | total_num_of_months++; 35 | } 36 | } 37 | 38 | average_rainfall_per_month = total_rainfall / 39 | total_num_of_months; 40 | 41 | cout << setprecision(2) << fixed; 42 | cout << "Total number of months = " 43 | << total_num_of_months << endl; 44 | 45 | cout << "Total inches of rainfall = " 46 | << total_rainfall << endl; 47 | 48 | cout << "Average rainfall per month = " 49 | << average_rainfall_per_month << endl; 50 | 51 | 52 | return 0; 53 | } 54 | // Blog post: https://www.jesushilarioh.com/chapter-5-10-average-rainfall-tony-gaddis-starting-out-with-c-plus-plus 55 | // YouTube video: https://youtu.be/eWzFQKSXhwE 56 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/012.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | float F = 0; 8 | 9 | cout << endl; 10 | cout << "--------------------------------------\n"; 11 | cout << "Celsius\t\tFahrenheit" << endl; 12 | cout << "======================================\n"; 13 | 14 | cout << setprecision(1) << fixed << right; 15 | cout.fill('0'); 16 | 17 | for (int C = 0; C <= 20; C++) 18 | { 19 | // Calculate Fahrenheit 20 | F = ((9.0 / 5) * C) + 32; 21 | cout << setw(2) << C << "\t\t" << F << endl; 22 | } 23 | cout << "--------------------------------------\n"; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | const int SENTINEL_VALUE = -99; 6 | int num, 7 | min, 8 | max; 9 | 10 | cout << "This program will display the greatest\n" 11 | << "and the least values.\n" 12 | << "Enter a number to begin and enter -99\n" 13 | << "to quit the program." 14 | << "Enter a value: "; 15 | cin >> num; // Error Checking needed 16 | 17 | min = max = num; 18 | 19 | while (num != SENTINEL_VALUE) 20 | { 21 | if (num < min) 22 | min = num; 23 | 24 | if (num > max) 25 | max = num; 26 | 27 | cout << "Enter a value: "; 28 | cin >> num; // Error Checking needed 29 | 30 | } 31 | 32 | cout << "Smallest number is: " << min << endl; 33 | cout << "Largest number is: " << max << endl; 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/014.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | string student, 9 | first, 10 | last; 11 | int num_of_students; 12 | 13 | cout << "Enter the number of students: "; 14 | 15 | while (!(cin >> num_of_students) || 16 | (num_of_students < 1 || num_of_students > 25)) 17 | { 18 | cout << "ERROR: Enter a number between " 19 | << "1 and 25: "; 20 | cin.clear(); 21 | cin.ignore(123, '\n'); 22 | } 23 | 24 | cout << "Enter the name of student No. 1: "; 25 | cin >> student; 26 | 27 | first = last = student; 28 | 29 | if (student < first) 30 | first = student; 31 | 32 | if (student > last) 33 | last = student; 34 | 35 | for (int i = 1; i < num_of_students; i++) 36 | { 37 | cout << "Enter the name of student No. " 38 | << (i + 1) << ": "; 39 | cin >> student; 40 | 41 | if (student < first) 42 | first = student; 43 | 44 | if (student > last) 45 | last = student; 46 | } 47 | 48 | cout << "First student in line = " 49 | << first << endl; 50 | 51 | cout << "Last student in line = " 52 | << last << endl; 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/017.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | const int MIN_NUMBER = 0, 6 | NUM_OF_STORES = 5, 7 | HUNDRED = 100; 8 | 9 | int store_1, 10 | store_2, 11 | store_3, 12 | store_4, 13 | store_5, 14 | store_1_100s, 15 | store_2_100s, 16 | store_3_100s, 17 | store_4_100s, 18 | store_5_100s; 19 | 20 | cout << "\nEnter today's sales for store 1: "; 21 | cin >> store_1; 22 | store_1_100s = store_1 / HUNDRED; // 200/100 = 2 23 | 24 | cout << "Enter today's sales for store 2: "; 25 | cin >> store_2; 26 | store_2_100s = store_2 / HUNDRED; // 300/100 = 3 27 | 28 | cout << "Enter today's sales for store 3: "; 29 | cin >> store_3; 30 | store_3_100s = store_3 / HUNDRED; 31 | 32 | cout << "Enter today's sales for store 4: "; 33 | cin >> store_4; 34 | store_4_100s = store_4 / HUNDRED; 35 | 36 | cout << "Enter today's sales for store 5: "; 37 | cin >> store_5; 38 | store_5_100s = store_5 / HUNDRED; 39 | 40 | cout << "\nSALES BAR CHART" << endl; 41 | cout << "(Each * = $100)" << endl; 42 | cout << "Store 1: "; 43 | for (int i = MIN_NUMBER; i < store_1_100s; i++) 44 | cout << '*'; 45 | 46 | cout << endl; 47 | 48 | cout << "Store 2: "; 49 | for (int i = MIN_NUMBER; i < store_2_100s; i++) 50 | cout << '*'; 51 | 52 | cout << endl; 53 | 54 | cout << "Store 3: "; 55 | for (int i = MIN_NUMBER; i < store_3_100s; i++) 56 | cout << '*'; 57 | 58 | cout << endl; 59 | 60 | cout << "Store 4: "; 61 | for (int i = MIN_NUMBER; i < store_4_100s; i++) 62 | cout << '*'; 63 | 64 | cout << endl; 65 | 66 | cout << "Store 5: "; 67 | for (int i = MIN_NUMBER; i < store_5_100s; i++) 68 | cout << '*'; 69 | 70 | cout << '\n' << endl; 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/018.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | * 3 | * 018. POPULATION BAR CHART 4 | * 5 | * Write a program that produces a bar chart 6 | * showing the population growth of Prairieville, 7 | * a small town in the Midwest, at 20-year intervals 8 | * during the past 100 years. The program should 9 | * read in the population figures (rounded to the 10 | * nearest 1,000 people) for 1900, 1920, 1940, 1960, 11 | * 1980, and 2000 from a file. For each year it should 12 | * display the date and a bar consisting of one 13 | * asterisk for each 1,000 people. The data can be 14 | * found in the People.txt file. 15 | * 16 | * Here is an example of how the chart might begin: 17 | * 18 | * PRAIRIEVILLE POPULATION GROWTH 19 | * (each * represents 1,000 people) 20 | * 1900 ** 21 | * 1920 **** 22 | * 1940 ***** 23 | * 24 | ***************************************************/ 25 | #include 26 | #include // ifstream 27 | #include 28 | 29 | using namespace std; 30 | 31 | int main() 32 | { 33 | ifstream input_file; 34 | string years; 35 | 36 | cout << "\nPRAIRIEVILLE POPULATION GROWTH\n"; 37 | cout << "(each * represents 1,000 people)\n\n"; 38 | 39 | input_file.open("People.txt"); 40 | 41 | while (getline(input_file, years)) 42 | cout << years << endl; 43 | 44 | input_file.close(); 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/019.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | float budget_amount, 6 | expense_total = 0, // running total 7 | expense, 8 | budget_result; 9 | int num_of_expenses; 10 | 11 | cout << "/nEnter the amount budgeted " 12 | << "for this month: "; 13 | cin >> budget_amount; 14 | 15 | cout << "How many expenses do you have? "; 16 | cin >> num_of_expenses; //10 17 | 18 | for (int i = 0; i < num_of_expenses; i++) 19 | { 20 | cout << "Enter expense #" << (i + 1) << ": "; 21 | cin >> expense; 22 | expense_total += expense; 23 | } 24 | 25 | budget_result = budget_amount - expense_total; 26 | 27 | if (expense_total > budget_amount) 28 | { 29 | cout << "You are over budget by $"; 30 | cout << budget_result << '.' << endl; 31 | } 32 | else if (expense_total < budget_amount) 33 | { 34 | cout << "You are under budget by $"; 35 | cout << budget_result << '.' << endl; 36 | } 37 | else if (expense_total == budget_amount) 38 | { 39 | cout << "Your budget is 0 based." << endl; 40 | } 41 | 42 | cout << endl; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/020.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For rand and srand 3 | #include // For the time method 4 | 5 | using namespace std; 6 | int main() 7 | { 8 | const int MIN_VALUE = 1, 9 | MAX_VALUE = 10; 10 | 11 | int random_num, 12 | user_num; 13 | 14 | unsigned seed = time(0); 15 | 16 | srand(seed); 17 | 18 | random_num = (rand() % 19 | (MAX_VALUE - MIN_VALUE + 1)) + 20 | MIN_VALUE; 21 | 22 | // cout << "Random number is: " 23 | // << random_num 24 | // << endl 25 | // << endl; 26 | 27 | cout << "Guess a number between 1 and 10." << endl; 28 | cin >> user_num; 29 | 30 | while (user_num != random_num) 31 | { 32 | cout << "Your number is " 33 | << (user_num < random_num ? 34 | "lower." : "higher."); 35 | 36 | cout << "Try again: "; 37 | 38 | cin >> user_num; 39 | } 40 | 41 | cout << "\nCorrect! " << endl; 42 | cout << "Random number = " << random_num << endl; 43 | cout << "Your guess = " << user_num << endl; 44 | cout << endl; 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/021.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For rand and srand 3 | #include // For the time method 4 | 5 | using namespace std; 6 | int main() 7 | { 8 | const int MIN_VALUE = 1, 9 | MAX_VALUE = 10; 10 | 11 | int random_num, 12 | user_num, 13 | number_of_guesses = 0; 14 | 15 | unsigned seed = time(0); 16 | 17 | srand(seed); 18 | 19 | random_num = (rand() % 20 | (MAX_VALUE - MIN_VALUE + 1)) + 21 | MIN_VALUE; 22 | 23 | // cout << "Random number is: " 24 | // << random_num 25 | // << endl 26 | // << endl; 27 | 28 | cout << "Guess a number between 1 and 10." << endl; 29 | cin >> user_num; 30 | number_of_guesses++; 31 | 32 | while (user_num != random_num) 33 | { 34 | cout << "Your number is " 35 | << (user_num < random_num ? 36 | "lower." : "higher."); 37 | 38 | cout << "Try again: "; 39 | 40 | cin >> user_num; 41 | number_of_guesses++; 42 | } 43 | 44 | cout << "\nCorrect! " << endl; 45 | cout << "Random number = " << random_num << endl; 46 | cout << "Your guess = " << user_num << endl; 47 | cout << "Number of guesses = " << number_of_guesses << endl; 48 | cout << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/022.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int user_integer; 6 | 7 | cout << "Enter a positive integer no greater than 15: "; 8 | while(!(cin >> user_integer) || 9 | user_integer > 15 || 10 | user_integer < 0 11 | ) 12 | { 13 | cout << "Error. A positive integer no \n" 14 | << "greater than 15 must be entered: "; 15 | cin.clear(); 16 | cin.ignore(1200, '\n'); 17 | } 18 | 19 | for (int i = 0; i < user_integer; i++) 20 | { 21 | for (int j = 0; j < user_integer; j++) 22 | { 23 | cout << "Y"; 24 | } 25 | cout << endl; 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/023.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | const int MINIMUM = 0, 6 | MAXIMUM = 10; 7 | 8 | char symbol = '+'; 9 | 10 | cout << "\nPatter A: " << endl; 11 | 12 | for(int i = MINIMUM; i < MAXIMUM; i++) 13 | { 14 | for(int j = MINIMUM; j <= i; j++) 15 | { 16 | cout << symbol; 17 | } 18 | cout << endl; 19 | } 20 | 21 | cout << "\nPatter B: " << endl; 22 | for(int i = MINIMUM; i < MAXIMUM; i++) 23 | { 24 | for(int j = MAXIMUM; j > i; j--) 25 | { 26 | cout << symbol; 27 | } 28 | cout << endl; 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/024.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | ifstream inputFile; 8 | 9 | double sum = 0, 10 | total = 0, 11 | average, 12 | number; 13 | 14 | // 1. Open file 15 | inputFile.open("Random.txt"); 16 | 17 | if (inputFile.fail()) 18 | { 19 | cout << "Error opening the file." << endl; 20 | } 21 | else 22 | { 23 | // Valid 24 | // 2. Process file (eof = end of file) 25 | while(inputFile >> number) 26 | { 27 | sum += number; 28 | total++; 29 | // cout << "Number : " << number << endl; 30 | } 31 | average = sum / total; 32 | 33 | // 3. Close file 34 | inputFile.close(); 35 | 36 | cout << "Sum : " << sum << endl; 37 | cout << "Total: " << total << endl; 38 | cout << "Average: " << average << endl; 39 | 40 | } 41 | 42 | cout << "File closed. Program Ended." << endl; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | ifstream inputFile; 10 | 11 | string student, 12 | first, 13 | last; 14 | 15 | int num_of_students; 16 | 17 | // 1. Open the file 18 | inputFile.open("LineUp.txt"); 19 | 20 | if (inputFile) 21 | { 22 | // 2. Process the file 23 | inputFile >> student; 24 | 25 | first = last = student; 26 | 27 | while (inputFile >> student) 28 | { 29 | if (student < first) 30 | first = student; 31 | 32 | if (student > last) 33 | last = student; 34 | } 35 | 36 | // 3. Close File 37 | inputFile.close(); 38 | } 39 | else 40 | { 41 | cout << "Error opening file." << endl; 42 | } 43 | 44 | cout << "First student in line = " 45 | << first << endl; 46 | 47 | cout << "Last student in line = " 48 | << last << endl << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/LineUp.txt: -------------------------------------------------------------------------------- 1 | James 2 | Jeremiah 3 | Xavier 4 | Sofia 5 | Mayra 6 | Liliana 7 | Jacob 8 | Jose 9 | Christina 10 | Jesus 11 | Francesca -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/Random.txt: -------------------------------------------------------------------------------- 1 | 87 2 | 78 3 | 98 4 | 99 5 | 100 6 | 105 -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 05/savings_account_report.txt: -------------------------------------------------------------------------------- 1 | 2 | Starting balance = $5000.00 3 | Ending balance = $47841.85 4 | Total amount in deposits = $11300.00 5 | Total amount in withdrawals = $1200.00 6 | Total interest earned = $32741.85 7 | 8 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | double calculateRetail(double, double); 7 | double inputValidate(double); 8 | 9 | int main() 10 | { 11 | double wholesale_cost, 12 | markup_percent, 13 | retail_price; 14 | 15 | cout << "Enter wholesale cost: "; 16 | wholesale_cost = inputValidate(wholesale_cost); 17 | 18 | cout << "Enter it's markup percentage: "; 19 | markup_percent = inputValidate(markup_percent) * .01; 20 | 21 | retail_price = calculateRetail(wholesale_cost, markup_percent); 22 | 23 | cout << "Markup percentage = %" 24 | << (markup_percent / .01) 25 | << endl; 26 | 27 | cout << setprecision(2) << fixed 28 | 29 | << "Wholesale cost = $" 30 | << wholesale_cost 31 | << endl 32 | 33 | << "Retail price = $" 34 | << retail_price 35 | << endl; 36 | 37 | return 0; 38 | } 39 | 40 | double calculateRetail(double num1, double num2) 41 | { 42 | return (num1 * num2) + num1; 43 | } 44 | 45 | double inputValidate(double num) 46 | { 47 | while(!(cin >> num) || (num < 0)) 48 | { 49 | cout << "Error. An integer above 0 must be entered: "; 50 | cin.clear(); 51 | cin.ignore(123, '\n'); 52 | } 53 | return num; 54 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | double inputValidate(double num); 6 | double getLength(double); 7 | double getWidth(double); 8 | double getArea(double, double); 9 | void displayData(double, double, double); 10 | 11 | int main() 12 | { 13 | double length, 14 | width, 15 | area; 16 | 17 | length = getLength(length); 18 | width = getWidth(width); 19 | area = getArea(length, width); 20 | displayData(length, width, area); 21 | 22 | return 0; 23 | } 24 | 25 | double getLength(double num) 26 | { 27 | cout << "Enter rectangle's length: "; 28 | num = inputValidate(num); 29 | return num; 30 | } 31 | 32 | double getWidth(double num) 33 | { 34 | cout << "Enter rectangle's width: "; 35 | num = inputValidate(num); 36 | return num; 37 | } 38 | 39 | double getArea(double length, double width) 40 | { 41 | return length * width; 42 | } 43 | 44 | void displayData(double l, double w, double a) 45 | { 46 | cout << "Length = " << l << endl; 47 | cout << "Width = " << w << endl; 48 | cout << "Area = " << a << endl; 49 | } 50 | 51 | double inputValidate(double num) 52 | { 53 | while(!(cin >> num)) 54 | { 55 | cout << "Error. An integer above 0 must be entered: "; 56 | cin.clear(); 57 | cin.ignore(1200, '\n'); 58 | } 59 | return num; 60 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // for pow() function 3 | using namespace std; 4 | 5 | const double g = 9.8; 6 | 7 | double fallingDistance(double); 8 | 9 | int main() 10 | { 11 | double d, 12 | t; 13 | 14 | for (int i = 0; i < 10; i++) 15 | { 16 | t = i + 1; 17 | d = fallingDistance(t); 18 | 19 | cout << t << " seconds = " 20 | << d << " meters" 21 | << endl; 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | double fallingDistance(double falling_time) 28 | { 29 | return (0.5) * g * pow(falling_time, 2); 30 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // for pow() function 3 | #include 4 | 5 | using namespace std; 6 | 7 | double inputValidate(double); 8 | double kineticEnergy(double, double); 9 | void displayInfo(double, double, double); 10 | 11 | int main() 12 | { 13 | double KINETIC_ENERGY, 14 | mass, 15 | velocity; 16 | 17 | cout << "Enter the object's mass: "; 18 | mass = inputValidate(mass); 19 | 20 | cout << "Enter the object's velocity: "; 21 | velocity = inputValidate(velocity); 22 | 23 | KINETIC_ENERGY = kineticEnergy(mass, velocity); 24 | 25 | displayInfo(mass, velocity, KINETIC_ENERGY); 26 | 27 | return 0; 28 | } 29 | 30 | /******************************************************** 31 | * Definition of inputValidate() * * 32 | ********************************************************/ 33 | double inputValidate(double num) 34 | { 35 | while(!(cin >> num)) 36 | { 37 | cout << "Error. A number must be entered. Try again: "; 38 | cin.clear(); 39 | cin.ignore(numeric_limits::max(), '\n'); 40 | } 41 | return num; 42 | } 43 | 44 | /******************************************************** 45 | * Definition of kineticEnergy function: * 46 | ********************************************************/ 47 | double kineticEnergy(double mass, double velocity) 48 | { 49 | return (0.5) * mass * pow(velocity, 2); 50 | } 51 | 52 | /******************************************************** 53 | * Definition of displayInfo() * 54 | ********************************************************/ 55 | void displayInfo(double mass, double velocity, double KINETIC_ENERGY) 56 | { 57 | cout << setprecision(2) << fixed; 58 | cout << endl; 59 | cout << "Mass = " << mass << endl; 60 | cout << "Velocity = " << velocity << endl; 61 | cout << "Kinetic Energy = " << KINETIC_ENERGY << endl; 62 | cout << endl; 63 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double celsius(double); 5 | 6 | int main() 7 | { 8 | const int MIN_TEMP = 0, 9 | MAX_TEMP = 20; 10 | double C; 11 | 12 | cout << "-----------------------------------|\n"; 13 | cout << "Fahrenheit | Celsius\n"; 14 | cout << "-----------------------------------|\n"; 15 | 16 | for (int F = MIN_TEMP; F <= MAX_TEMP; F++) 17 | { 18 | C = celsius(F); 19 | 20 | cout << F << " "; 21 | cout << C << endl; 22 | 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | /******************************************************** 29 | * celsius() * 30 | ********************************************************/ 31 | double celsius(double F) 32 | { 33 | return (5.0/9.0) * (F - 32.0); 34 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For rand() and srand() 3 | #include // For time() function 4 | using namespace std; 5 | 6 | void coinToss(); 7 | int inputValidate(int); 8 | 9 | int main() 10 | { 11 | int choice; 12 | unsigned seed = time(0); 13 | srand(seed); 14 | 15 | cout << "How many times should the coin be tossed? "; 16 | choice = inputValidate(choice); 17 | 18 | for (int i = 0; i < choice; i++) 19 | coinToss(); 20 | 21 | return 0; 22 | } 23 | 24 | /******************************************************** 25 | * inputValidate() * 26 | ********************************************************/ 27 | int inputValidate(int num) 28 | { 29 | while(!(cin >> num) || num < 0) 30 | { 31 | cout << "Error. An integer above 0 must be entered: "; 32 | cin.clear(); 33 | cin.ignore(numeric_limits::max(), '\n'); 34 | } 35 | return num; 36 | } 37 | 38 | /******************************************************** 39 | * coinToss() * 40 | ********************************************************/ 41 | void coinToss() 42 | { 43 | const int MIN_VALUE = 1, 44 | MAX_VALUE = 2; 45 | 46 | int coin; 47 | 48 | coin = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE; 49 | 50 | cout << (coin == MIN_VALUE ? "Heads" : "Tails") << endl; 51 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double calcOfPopulation(double, double, double); 5 | double inputValidate(double, int); 6 | 7 | int main() 8 | { 9 | double P, // population size 10 | B, // birth rate % 11 | D, // death rate % 12 | num_years; 13 | 14 | cout << "Starting size of population: "; 15 | P = inputValidate(P, 2); 16 | 17 | cout << "Annual birth rate: %"; 18 | B = inputValidate(B, 0); 19 | 20 | cout << "Annual death rate: %"; 21 | D = inputValidate(D, 0); 22 | 23 | cout << "Number of years to display: "; 24 | num_years = inputValidate(num_years, 1); 25 | 26 | cout << "Population size for " 27 | << num_years << " years " 28 | << " = " 29 | << (calcOfPopulation(P, B, D) * num_years) 30 | << endl; 31 | 32 | return 0; 33 | } // END int main() 34 | 35 | double inputValidate(double number, int limit) 36 | { 37 | 38 | while(!(cin >> number) || number < limit) 39 | { 40 | cout << "Error. Number must not be " 41 | << " 0 or greater:"; 42 | cin.clear(); 43 | cin.ignore(numeric_limits::max(), '\n'); 44 | } 45 | 46 | return number; 47 | } 48 | 49 | double calcOfPopulation(double P, double B, double D) 50 | { 51 | B *= .01; // 3.33% = .0333 52 | D *= .01; // 4.44% = .0444 53 | 54 | return P + (B * P) - (D * P); 55 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double calcOfPopulation(double, double, double, double, double); 5 | double inputValidate(double, int); 6 | 7 | int main() 8 | { 9 | double P, // population size 10 | B, // birth rate % 11 | D, // death rate % 12 | num_years, 13 | arrivals, 14 | departures; 15 | 16 | cout << "Starting size of population: "; 17 | P = inputValidate(P, 2); 18 | 19 | cout << "Annual birth rate: %"; 20 | B = inputValidate(B, 0); 21 | 22 | cout << "Annual death rate: %"; 23 | D = inputValidate(D, 0); 24 | 25 | cout << "Arrivals each year: "; 26 | arrivals = inputValidate(arrivals, 0); 27 | 28 | cout << "Departures each year: "; 29 | departures = inputValidate(departures, 0); 30 | 31 | cout << "Number of years to display: "; 32 | num_years = inputValidate(num_years, 1); 33 | 34 | cout << "Population size for " 35 | << num_years << " years " 36 | << " = " 37 | << (calcOfPopulation(P, B, D, arrivals, departures) 38 | * num_years) 39 | << endl; 40 | 41 | return 0; 42 | } // END int main() 43 | 44 | double inputValidate(double number, int limit) 45 | { 46 | 47 | while (!(cin >> number) || number < limit) 48 | { 49 | cout << "Error. Number must not be " 50 | << " 0 or greater:"; 51 | cin.clear(); 52 | cin.ignore(numeric_limits::max(), '\n'); 53 | } 54 | 55 | return number; 56 | } 57 | 58 | double calcOfPopulation(double P, 59 | double B, 60 | double D, 61 | double arrivals, 62 | double departures) 63 | { 64 | B *= .01; // 3.33% = .0333 65 | D *= .01; // 4.44% = .0444 66 | 67 | return ((P + (B * P) - (D * P)) + arrivals) - departures; 68 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void getInfo(string, double &); 7 | double profit(double, double, double, double, double); 8 | double inputValidate(double); 9 | void displayCalculation(double); 10 | 11 | int main() 12 | { 13 | double NS, // Number of shares 14 | PP, // Purchase price per share 15 | PC, // Purchase commission paid 16 | SP, // Sale price per share 17 | SC, // Sale commission paid 18 | profit_or_loss; 19 | 20 | getInfo("Number of shares: ", NS); 21 | getInfo("Purchase price per share: ", PP); 22 | getInfo("Purchase commission paid: ", PC); 23 | getInfo("Sale price per share: ", SP); 24 | getInfo("Sale commission paid: ", SC); 25 | 26 | profit_or_loss = profit(NS, PP, PC, SP, SC); 27 | 28 | displayCalculation(profit_or_loss); 29 | 30 | return 0; 31 | } // END int main() 32 | 33 | void getInfo(string prompt, double &user_input) 34 | { 35 | cout << prompt; 36 | user_input = inputValidate(user_input); 37 | } 38 | 39 | double inputValidate(double num1) 40 | { 41 | 42 | while(!(cin >> num1) || num1 < 0) 43 | { 44 | cout << "Error. Number must not be " 45 | << " 0 or greater:"; 46 | cin.clear(); 47 | cin.ignore(numeric_limits::max(), '\n'); 48 | } 49 | 50 | return num1; 51 | } 52 | 53 | double profit(double NS, 54 | double PP, 55 | double PC, 56 | double SP, 57 | double SC) 58 | { 59 | cout << "NS = " << NS << endl; 60 | cout << "PP = " << PP << endl; 61 | cout << "PC = " << PC << endl; 62 | cout << "SP = " << SP << endl; 63 | cout << "SC = " << SC << endl; 64 | 65 | return ((NS * SP) - SC) - ((NS * PP) + PC); 66 | } 67 | 68 | void displayCalculation(double profit_or_loss) 69 | { 70 | cout << "The sale of the stock resulted in " 71 | << (profit_or_loss < 0 ? "LOSS." : "PROFIT.") 72 | << "At $" << profit_or_loss 73 | << endl; 74 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int inputValidate(int); 5 | bool isPrime(int); 6 | 7 | int main() 8 | { 9 | int number; 10 | 11 | cout << "Enter a number: "; 12 | number = inputValidate(number); 13 | 14 | cout << "Your number is " 15 | << (isPrime(number) ? "PRIME." : "NOT PRIME.") 16 | << endl; 17 | 18 | return 0; 19 | } // END int main() 20 | 21 | int inputValidate(int num) 22 | { 23 | 24 | while(!(cin >> num) || num < 0) 25 | { 26 | cout << "Error. Number must not be " 27 | << " 0 or greater:"; 28 | cin.clear(); 29 | cin.ignore(numeric_limits::max(), '\n'); 30 | } 31 | 32 | return num; 33 | } 34 | 35 | bool isPrime(int number) 36 | { 37 | int isPrime = 0; 38 | 39 | for (int i = 1; i <= number; i++) 40 | { 41 | if (number % i == 0) 42 | isPrime++; 43 | } 44 | 45 | return isPrime == 2 ? 1 : 0; 46 | 47 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool isPrime(int); 7 | 8 | int main() 9 | { 10 | // 1. Open 11 | ofstream outputFile; 12 | outputFile.open("primeNumber.txt"); 13 | 14 | // 2. Process 15 | if (outputFile) 16 | { 17 | for (int i = 1; i <= 100; i++) 18 | { 19 | if(isPrime(i)) 20 | outputFile << i << endl; 21 | } 22 | 23 | // 3. Close 24 | outputFile.close(); 25 | cout << "Numbers written to file." << endl; 26 | } 27 | else 28 | { 29 | cout << "Error opening the file.\n"; 30 | } 31 | 32 | return 0; 33 | } // END int main() 34 | 35 | bool isPrime(int number) 36 | { 37 | int isPrime = 0; 38 | 39 | for (int i = 1; i <= number; i++) 40 | { 41 | if (number % i == 0) 42 | isPrime++; 43 | } 44 | 45 | return isPrime == 2 ? 1 : 0; 46 | 47 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/hospital_report.txt: -------------------------------------------------------------------------------- 1 | total_charges = $340 2 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 06/primeNumber.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 3 3 | 5 4 | 7 5 | 11 6 | 13 7 | 17 8 | 19 9 | 23 10 | 29 11 | 31 12 | 37 13 | 41 14 | 43 15 | 47 16 | 53 17 | 59 18 | 61 19 | 67 20 | 71 21 | 73 22 | 79 23 | 83 24 | 89 25 | 97 26 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/01.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | * 3 | * 15. vector Modification 4 | * 5 | * Modify the National Commerce Bank case study presented 6 | * in Program 7-23 so pin1, pin2, and pin3 are vectors 7 | * instead of arrays. You must also modify the testPIN 8 | * function to accept a vector instead of an array. 9 | * 10 | *************************************************************/ 11 | // This program is a driver that tests a function 12 | // comparing the contents of two int arrays. 13 | #include 14 | #include // Added 15 | 16 | using namespace std; 17 | 18 | bool testPIN(const vector, const vector, int); // Changed 19 | 20 | int main() 21 | { 22 | const int NUMBER_OF_DIGITS = 7; 23 | vector pin_1 {2, 4, 1, 8, 7, 9, 0}; // Changed 24 | vector pin_2 {2, 4, 6, 8, 7, 9, 0}; // Changed 25 | vector pin_3 {1, 2, 3, 4, 5, 6, 7}; // Changed 26 | 27 | 28 | if (testPIN(pin_1, pin_2, NUMBER_OF_DIGITS)) 29 | cout << "ERROR: pin_1 and pin_2 report to be the same.\n"; 30 | else 31 | cout << "SUCCESS: pin_1 and pin 2 are different.\n"; 32 | 33 | 34 | if (testPIN(pin_1, pin_3, NUMBER_OF_DIGITS)) 35 | cout << "ERROR: pin_1 and pin_3 report to be the same.\n"; 36 | else 37 | cout << "SUCCESS: pin_1 and pin_3 are different.\n"; 38 | 39 | 40 | if (testPIN(pin_1, pin_1, NUMBER_OF_DIGITS)) 41 | cout << "SUCCESS: pin_1 and pin_1 report to be the same.\n"; 42 | else 43 | cout << "ERROR: pin_1 and pin_1 report to be different.\n"; 44 | 45 | 46 | return 0; 47 | } 48 | 49 | // Changed 50 | bool testPIN(const vector customerPIN, const vector databasePIN, int size) 51 | { 52 | for(int index = 0; index < size; index++) // Changed 53 | { 54 | if (customerPIN[index] != databasePIN[index]) 55 | return false; 56 | } 57 | return true; 58 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void greaterNumbers(const int[], int, int); 5 | 6 | int main() 7 | { 8 | const int ARRAY_SIZE = 30; 9 | int numbers_array[ARRAY_SIZE] = {1, 2, 3, 4, 5, 10 | 6, 7, 8, 9, 10, 11 | 11, 12, 13, 14, 15, 12 | 16, 17, 18, 19, 20, 13 | 21, 22, 23, 24, 25, 14 | 26, 27, 28, 29, 30}; 15 | int n = 20; 16 | 17 | cout << "All numbers greater that " << n << " are: "; 18 | greaterNumbers(numbers_array, ARRAY_SIZE, n); 19 | cout << endl; 20 | 21 | return 0; 22 | } // END int main() 23 | 24 | void greaterNumbers(const int array[], int ARRAY_SIZE, int n) 25 | { 26 | for (int i = 0; i < ARRAY_SIZE; i++) 27 | { 28 | if (array[i] > n) 29 | { 30 | if(i == ARRAY_SIZE - 1) 31 | cout << array[i]; 32 | else 33 | cout << array[i] << ", "; 34 | 35 | } 36 | 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | vector numbers; 10 | ifstream inputFile; 11 | 12 | string user_file_name; 13 | cout << "What is the file name? "; 14 | cin >> user_file_name; 15 | 16 | inputFile.open(user_file_name); 17 | 18 | if (inputFile) 19 | { 20 | // cout << "Valid file." << endl; 21 | 22 | int file_number; 23 | 24 | while(inputFile >> file_number) 25 | numbers.push_back(file_number); 26 | 27 | inputFile.close(); 28 | } 29 | else 30 | { 31 | cout << "Invalid file." << endl; 32 | exit(EXIT_SUCCESS); 33 | } 34 | 35 | double lowest_number = numbers[0]; 36 | for (int i = 1; i < numbers.size(); i++) 37 | if (numbers[i] <= lowest_number) 38 | lowest_number = numbers[i]; 39 | cout << "Lowest Number = " << lowest_number << endl; 40 | 41 | double highest_number = numbers[0]; 42 | for (int i = 1; i < numbers.size(); i++) 43 | if (numbers[i] >= highest_number) 44 | highest_number = numbers[i]; 45 | cout << "Highest Number = " << highest_number << endl; 46 | 47 | 48 | double total = 0; 49 | for (int i = 0; i < numbers.size(); i++) 50 | total += numbers[i]; 51 | cout << "Total = " << total << endl; 52 | 53 | double average = total / numbers.size(); 54 | cout << "Average = " << average << endl; 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/15.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | * 3 | * 15. vector Modification 4 | * 5 | * Modify the National Commerce Bank case study presented 6 | * in Program 7-23 so pin1, pin2, and pin3 are vectors 7 | * instead of arrays. You must also modify the testPIN 8 | * function to accept a vector instead of an array. 9 | * 10 | *************************************************************/ 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | bool testPIN(const vector, const vector); 17 | 18 | int main() 19 | { 20 | vector pin_1 {2, 4, 1, 8, 7, 9, 0}; // C++ 11 21 | vector pin_2 {2, 4, 6, 8, 7, 9, 0}; 22 | vector pin_3 {1, 2, 3, 4, 5, 6, 7}; 23 | 24 | 25 | if (testPIN(pin_1, pin_2)) 26 | cout << "Error: pin_1 and pin_2 report to be the same.\n"; 27 | else 28 | cout << "Success: pin_1 and pin 2 are different.\n"; 29 | 30 | 31 | if (testPIN(pin_1, pin_3)) 32 | cout << "ERROR: pin_1 and pin_3 report to be the same.\n"; 33 | else 34 | cout << "SUCCESS: pin_1 and pin_3 are different.\n"; 35 | 36 | 37 | if (testPIN(pin_1, pin_2)) 38 | cout << "SUCCESS: pin_1 and pin_1 report to be the same.\n"; 39 | else 40 | cout << "ERROR: pin_1 and pin_1 report to be different.\n"; 41 | 42 | 43 | return 0; 44 | } 45 | 46 | bool testPIN(const vector customerPIN, const vector databasePIN) 47 | { 48 | for(int index = 0; index < customerPIN.size(); index++) 49 | { 50 | if (customerPIN[index] != databasePIN[index]) 51 | return false; 52 | } 53 | return true; 54 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/7-23.cpp: -------------------------------------------------------------------------------- 1 | // This program is a driver that tests a function 2 | // comparing the contents of two int arrays. 3 | #include 4 | using namespace std; 5 | 6 | bool testPIN(const int [], const int [], int); 7 | 8 | int main() 9 | { 10 | const int NUMBER_OF_DIGITS = 7; 11 | int pin_1[NUMBER_OF_DIGITS] = {2, 4, 1, 8, 7, 9, 0}, 12 | pin_2[NUMBER_OF_DIGITS] = {2, 4, 6, 8, 7, 9, 0}, 13 | pin_3[NUMBER_OF_DIGITS] = {1, 2, 3, 4, 5, 6, 7}; 14 | 15 | 16 | if (testPIN(pin_1, pin_2, NUMBER_OF_DIGITS)) 17 | cout << "Error: pin_1 and pin_2 report to be the same.\n"; 18 | else 19 | cout << "Success: pin_1 and pin 2 are different.\n"; 20 | 21 | 22 | if (testPIN(pin_1, pin_3, NUMBER_OF_DIGITS)) 23 | cout << "ERROR: pin_1 and pin_3 report to be the same.\n"; 24 | else 25 | cout << "SUCCESS: pin_1 and pin_3 are different.\n"; 26 | 27 | 28 | if (testPIN(pin_1, pin_2, NUMBER_OF_DIGITS)) 29 | cout << "SUCCESS: pin_1 and pin_1 report to be the same.\n"; 30 | else 31 | cout << "ERROR: pin_1 and pin_1 report to be different.\n"; 32 | 33 | 34 | return 0; 35 | } 36 | 37 | //******************************************************************* 38 | // The following function accepts two int arrays. The arrays are * 39 | // compared. If they contain the same values, true is returned. * 40 | // If they contain different values, false is returned. * 41 | //******************************************************************* 42 | bool testPIN(const int customerPIN[], const int databasePIN[], int size) 43 | { 44 | for(int index = 0; index < size; index++) 45 | { 46 | if (customerPIN[index] != databasePIN[index]) 47 | return false; 48 | } 49 | return true; 50 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/BoyNames.txt: -------------------------------------------------------------------------------- 1 | Jacob 2 | Michael 3 | Joshua 4 | Matthew 5 | Andrew 6 | Christopher 7 | Daniel 8 | Joseph 9 | Ethan 10 | Nicholas 11 | William 12 | Anthony 13 | David 14 | Ryan 15 | Tyler 16 | Alexander 17 | John 18 | James 19 | Brandon 20 | Zachary 21 | Jonathan 22 | Dylan 23 | Christian 24 | Samuel 25 | Benjamin 26 | Nathan 27 | Justin 28 | Noah 29 | Logan 30 | Austin 31 | Jose 32 | Kevin 33 | Gabriel 34 | Robert 35 | Caleb 36 | Thomas 37 | Jordan 38 | Elijah 39 | Cameron 40 | Hunter 41 | Jack 42 | Jason 43 | Isaiah 44 | Luke 45 | Angel 46 | Kyle 47 | Connor 48 | Evan 49 | Aaron 50 | Isaac 51 | Jackson 52 | Mason 53 | Juan 54 | Aidan 55 | Gavin 56 | Brian 57 | Eric 58 | Charles 59 | Luis 60 | Adam 61 | Sean 62 | Alex 63 | Nathaniel 64 | Bryan 65 | Carlos 66 | Ian 67 | Jesus 68 | Lucas 69 | Adrian 70 | Steven 71 | Cole 72 | Timothy 73 | Cody 74 | Jayden 75 | Aiden 76 | Julian 77 | Seth 78 | Owen 79 | Devin 80 | Chase 81 | Richard 82 | Diego 83 | Sebastian 84 | Jeremiah 85 | Blake 86 | Patrick 87 | Xavier 88 | Antonio 89 | Hayden 90 | Miguel 91 | Landon 92 | Dominic 93 | Jared 94 | Trevor 95 | Jesse 96 | Alejandro 97 | Carter 98 | Jaden 99 | Wyatt 100 | Carson -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/CorrectAnswers.txt: -------------------------------------------------------------------------------- 1 | A 2 | B 3 | C 4 | D 5 | A 6 | B 7 | C 8 | D 9 | A 10 | B 11 | C 12 | D 13 | A 14 | B 15 | C 16 | D 17 | A 18 | B 19 | C 20 | D -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/GirlNames.txt: -------------------------------------------------------------------------------- 1 | Emily 2 | Madison 3 | Emma 4 | Hannah 5 | Abigail 6 | Olivia 7 | Ashley 8 | Samantha 9 | Alexis 10 | Sarah 11 | Elizabeth 12 | Isabella 13 | Alyssa 14 | Grace 15 | Lauren 16 | Taylor 17 | Jessica 18 | Brianna 19 | Kayla 20 | Sophia 21 | Anna 22 | Natalie 23 | Victoria 24 | Chloe 25 | Sydney 26 | Jasmine 27 | Hailey 28 | Megan 29 | Rachel 30 | Morgan 31 | Julia 32 | Destiny 33 | Ava 34 | Jennifer 35 | Kaitlyn 36 | Mia 37 | Katherine 38 | Alexandra 39 | Haley 40 | Savannah 41 | Nicole 42 | Maria 43 | Allison 44 | Mackenzie 45 | Stephanie 46 | Brooke 47 | Amanda 48 | Ella 49 | Makayla 50 | Faith 51 | Kaylee 52 | Jenna 53 | Andrea 54 | Katelyn 55 | Mary 56 | Jordan 57 | Gabrielle 58 | Rebecca 59 | Paige 60 | Madeline 61 | Kimberly 62 | Trinity 63 | Zoe 64 | Michelle 65 | Sara 66 | Lily 67 | Kylie 68 | Alexa 69 | Caroline 70 | Vanessa 71 | Amber 72 | Angelina 73 | Gabriella 74 | Lillian 75 | Riley 76 | Sierra 77 | Danielle 78 | Leah 79 | Jada 80 | Autumn 81 | Erin 82 | Maya 83 | Ariana 84 | Audrey 85 | Isabel 86 | Sofia 87 | Marissa 88 | Bailey 89 | Jacqueline 90 | Melissa 91 | Claire 92 | Evelyn 93 | Shelby 94 | Jocelyn 95 | Mariah 96 | Avery 97 | Leslie 98 | Melanie 99 | Arianna 100 | Aaliyah -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/RainOrShine.txt: -------------------------------------------------------------------------------- 1 | R 2 | R 3 | R 4 | C 5 | S 6 | R 7 | C 8 | S 9 | R 10 | C 11 | R 12 | C 13 | S 14 | C 15 | S 16 | R 17 | C 18 | S 19 | R 20 | C 21 | C 22 | S 23 | R 24 | C 25 | S 26 | R 27 | C 28 | S 29 | R 30 | C 31 | C 32 | S 33 | R 34 | C 35 | S 36 | R 37 | C 38 | S 39 | R 40 | C 41 | R 42 | C 43 | S 44 | C 45 | S 46 | R 47 | C 48 | S 49 | R 50 | C 51 | R 52 | C 53 | S 54 | C 55 | S 56 | R 57 | C 58 | S 59 | C 60 | S 61 | C 62 | S 63 | R 64 | C 65 | S 66 | R 67 | C 68 | S 69 | R 70 | C 71 | R 72 | C 73 | S 74 | C 75 | S 76 | C 77 | S 78 | R 79 | C 80 | S 81 | C 82 | S 83 | R 84 | C 85 | S 86 | R 87 | C 88 | S 89 | R 90 | C -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/StudentAnswers.txt: -------------------------------------------------------------------------------- 1 | C 2 | B 3 | B 4 | B 5 | B 6 | C 7 | C 8 | C 9 | D 10 | D 11 | D 12 | A 13 | D 14 | A 15 | A 16 | A 17 | C 18 | B 19 | B 20 | D -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/Teams.txt: -------------------------------------------------------------------------------- 1 | Anaheim Angels 2 | Arizona Diamondbacks 3 | Atlanta Braves 4 | Baltimore Orioles 5 | Boston Americans 6 | Boston Braves 7 | Boston Red Sox 8 | Brooklyn Dodgers 9 | Chicago Cubs 10 | Chicago White Sox 11 | Cincinnati Reds 12 | Cleveland Indians 13 | Detroit Tigers 14 | Florida Marlins 15 | Kansas City Royals 16 | Los Angeles Dodgers 17 | Milwaukee Braves 18 | Minnesota Twins 19 | New York Giants 20 | New York Mets 21 | New York Yankees 22 | Oakland Athletics 23 | Philadelphia Athletics 24 | Philadelphia Phillies 25 | Pittsburgh Pirates 26 | San Francisco Giants 27 | St. Louis Cardinals 28 | Toronto Blue Jays 29 | Washington Senators -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/numbers.txt: -------------------------------------------------------------------------------- 1 | 47 2 | 89 3 | 65 4 | 36 5 | 12 6 | 25 7 | 17 8 | 8 9 | 62 10 | 10 11 | 87 12 | 62 13 | 9834 14 | 98 15 | 94 16 | 43 17 | 19 18 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/seating_chart.txt: -------------------------------------------------------------------------------- 1 | ***###***###*########*****#### 2 | ####*************####*******## 3 | **###**********########****### 4 | **######**************##****** 5 | ********#####*********######## 6 | ##############************#### 7 | #######************########### 8 | ************##****############ 9 | #########*****############**** 10 | #####*************############ 11 | #**********#################** 12 | #############********########* 13 | ###***********########**###### 14 | ############################## 15 | ############################## -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 07/seating_prices.txt: -------------------------------------------------------------------------------- 1 | 20 2 | 20 3 | 20 4 | 20 5 | 20 6 | 15 7 | 15 8 | 15 9 | 15 10 | 15 11 | 10 12 | 10 13 | 10 14 | 10 15 | 10 -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 08/00.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jesushilarioh/Starting-Out-With-C-Plus-Plus-Programming-Challenges/d8cfbf891dac4ae2fa2f0aa6d88caf15a3ab5593/8th Edition Challenges/Chapter 08/00.txt -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 08/1994_Gas_Averages_Lowest_to_Highest.txt: -------------------------------------------------------------------------------- 1 | 2 | 0.998: (1) -> January 3 | 1.008: (3) -> March 4 | 1.009: (2) -> February 5 | 1.027: (4) -> April 6 | 1.047: (5) -> May 7 | 1.078: (6) -> June 8 | 1.106: (7) -> July 9 | 1.114: (10) -> October 10 | 1.119: (11) -> November 11 | 1.129: (12) -> December 12 | 1.144: (9) -> September 13 | 1.155: (8) -> August 14 | 15 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 08/1994_Weekly_Gas_Averages.txt: -------------------------------------------------------------------------------- 1 | 01 0.992 2 | 01 0.995 3 | 01 1.001 4 | 01 0.999 5 | 01 1.005 6 | 02 1.007 7 | 02 1.016 8 | 02 1.009 9 | 02 1.004 10 | 03 1.007 11 | 03 1.005 12 | 03 1.007 13 | 03 1.012 14 | 04 1.011 15 | 04 1.028 16 | 04 1.033 17 | 04 1.037 18 | 05 1.04 19 | 05 1.045 20 | 05 1.046 21 | 05 1.05 22 | 05 1.056 23 | 06 1.065 24 | 06 1.073 25 | 06 1.079 26 | 06 1.095 27 | 07 1.097 28 | 07 1.103 29 | 07 1.109 30 | 07 1.114 31 | 08 1.13 32 | 08 1.157 33 | 08 1.161 34 | 08 1.165 35 | 08 1.161 36 | 09 1.156 37 | 09 1.15 38 | 09 1.14 39 | 09 1.129 40 | 10 1.12 41 | 10 1.114 42 | 10 1.106 43 | 10 1.107 44 | 10 1.121 45 | 11 1.123 46 | 11 1.122 47 | 11 1.113 48 | 11 1.117 49 | 12 1.127 50 | 12 1.131 51 | 12 1.134 52 | 12 1.125 -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 08/names.txt: -------------------------------------------------------------------------------- 1 | Collins, Bill 2 | Smith, Bart 3 | Allen, Jim 4 | Griffin, Jim 5 | Stamey, Marty 6 | Rose, Geri 7 | Taylor, Terri 8 | Johnson, Jill 9 | Allison, Jeff 10 | Looney, Joe 11 | Wolfe, Bill 12 | James, Jean 13 | Weaver, Jim 14 | Pore, Bob 15 | Rutherford, Greg 16 | Javens, Renee 17 | Harrison, Rose 18 | Setzer, Cathy 19 | Pike, Gordon 20 | Holland, Beth -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 08/program_8-8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | const int NUM_NAMES = 20; 8 | string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim", 9 | "Griffin, Jim", "Stamey, Marty", "Rose, Geri", 10 | "Taylor, Terri", "Johnson, Jill", 11 | "Allison, Jeff", "Looney, Joe", "Wolfe, Bill", 12 | "James, Jean", "Weaver, Jim", "Pore, Bob", 13 | "Rutherford, Greg", "Javens, Renee", 14 | "Harrison, Rose", "Setzer, Cathy", 15 | "Pike, Gordon", "Holland, Beth" }; 16 | // Insert your code to complete this program 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 09/00.txt: -------------------------------------------------------------------------------- 1 | Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program. -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 09/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int *dynAllocateArray(int); 5 | 6 | int main() 7 | { 8 | const int ARRAY_SIZE = 5; 9 | int *numbers = dynAllocateArray(ARRAY_SIZE); 10 | 11 | cout << "Enter #s: " << endl; 12 | for (int i = 0; i < ARRAY_SIZE; i++) 13 | { 14 | cout << "#" << (i + 1) << ": "; 15 | cin >> *(numbers + i); // numbers[i] 16 | } 17 | 18 | for (int i = 0; i < ARRAY_SIZE; i++) 19 | cout << *(numbers + i) << endl; 20 | 21 | 22 | delete [] numbers; 23 | numbers = nullptr; 24 | 25 | return 0; 26 | } 27 | 28 | int *dynAllocateArray(int SIZE) 29 | { 30 | return new int[SIZE]; 31 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 09/05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int doSomething(int *, int *); 5 | 6 | int main() 7 | { 8 | int z = 2, 9 | *x = &z, 10 | *y = &z; 11 | 12 | z = doSomething(x, y); 13 | 14 | cout << "z = " << z << endl; 15 | 16 | x = nullptr; 17 | z = nullptr; 18 | 19 | return 0; 20 | } 21 | 22 | int doSomething(int *x, int *y) 23 | { 24 | int temp = *x; 25 | *x = *y * 10; 26 | *y = temp * 10; 27 | return *x + *y; 28 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 09/08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findMode(int [], const int); 5 | 6 | int main() 7 | { 8 | const int SIZE = 10; 9 | int numbers[SIZE] = { 1, 2, 3, 4, 5, 7, 7, 8 ,7, 10 }; 10 | 11 | int mode = findMode(numbers, SIZE); 12 | 13 | cout << "\nMode = " << mode << endl; 14 | 15 | return 0; 16 | } 17 | int findMode(int array[], const int SIZE) 18 | { 19 | int maxValue = 0, 20 | maxCount = 0, 21 | i, j; 22 | 23 | for (i = 0; i < SIZE; i++) 24 | { 25 | int count = 0; 26 | 27 | for (j = 0; j < SIZE; j++) 28 | { 29 | if (*(array + j) == *(array + i)) // array[j] == array[i] 30 | count++; 31 | } 32 | 33 | if (count > maxCount) 34 | { 35 | maxCount = count; 36 | maxValue = *(array + i); // array[i] 37 | } 38 | else if (maxCount == 1) 39 | maxValue = -1; 40 | } 41 | 42 | return maxValue; 43 | } 44 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 09/09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double findMedian(int [], const int); 5 | 6 | int main() 7 | { 8 | const int SIZE = 10; 9 | int numbers[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 10 | 11 | double median = findMedian(numbers, SIZE); 12 | 13 | cout << "Median = " << median << endl; 14 | 15 | return 0; 16 | } 17 | double findMedian(int array[], const int SIZE) 18 | { 19 | double median = 0.0; 20 | 21 | if (SIZE % 2 != 0) 22 | { 23 | median = *(array + (SIZE / 2)); 24 | // median = array[SIZE / 2]; 25 | } 26 | else 27 | median = (*(array + ((SIZE - 1) / 2)) + *(array + (SIZE / 2))) / 2.0; 28 | 29 | return median; 30 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 09/10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int *reverseArray(int [], const int); 5 | void displayArray(int *, const int); 6 | 7 | int main() 8 | { 9 | const int ARRAY_SIZE = 10; 10 | int numbers[ARRAY_SIZE] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; 11 | 12 | int *numbers_reversed = reverseArray(numbers, ARRAY_SIZE); 13 | 14 | cout << endl; 15 | displayArray(numbers, ARRAY_SIZE); 16 | cout << endl; 17 | displayArray(numbers_reversed, ARRAY_SIZE); 18 | 19 | delete [] numbers_reversed; 20 | numbers_reversed = nullptr; // 0x0 21 | 22 | return 0; 23 | } 24 | int *reverseArray(int array[], const int SIZE) 25 | { 26 | int *newArray = new int[SIZE]; 27 | 28 | int j = (SIZE - 1); 29 | 30 | for (int i = 0; i < SIZE; i++, j--) 31 | *(newArray + i) = array[j]; 32 | 33 | return newArray; 34 | } 35 | void displayArray(int *array, const int SIZE) 36 | { 37 | for (int i = 0; i < SIZE; i++) 38 | cout << *(array + i) << " "; 39 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 09/11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int *doubleArraySize(int [], const int); 5 | void displayArray(int *, const int); 6 | 7 | int main() 8 | { 9 | const int SIZE = 5; 10 | int numbers[SIZE] = { 2, 4, 6, 8, 10 }; 11 | 12 | int *numbers_doubled = doubleArraySize(numbers, SIZE); 13 | 14 | displayArray(numbers, SIZE); 15 | cout << endl; 16 | displayArray(numbers_doubled, (SIZE * 2)); 17 | cout << endl << endl; 18 | 19 | delete [] numbers_doubled; 20 | numbers_doubled = nullptr; // 0x0 21 | 22 | return 0; 23 | } 24 | int *doubleArraySize(int array[], const int SIZE) 25 | { 26 | const int NEW_SIZE = SIZE * 2; 27 | int *newArray = new int[NEW_SIZE]; 28 | 29 | for (int i = 0; i < NEW_SIZE; i++) 30 | { 31 | if (i >= SIZE) 32 | *(newArray + i) = 0; 33 | else 34 | *(newArray + i) = array[i]; 35 | } 36 | 37 | return newArray; 38 | } 39 | void displayArray(int *array, const int SIZE) 40 | { 41 | for (int i = 0; i < SIZE; i++) 42 | cout << *(array + i) << " "; // array[i] 43 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 09/12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int *createOneNewElement(int *, const int); 5 | void displayArray(int *, const int); 6 | 7 | int main() 8 | { 9 | const int SIZE = 5; 10 | int numbers[SIZE] = { 2, 4, 6, 8, 10}; 11 | 12 | int *oneElementAdded = createOneNewElement(numbers, SIZE); 13 | 14 | displayArray(numbers, SIZE); 15 | cout << endl; 16 | displayArray(oneElementAdded, (SIZE + 1)); 17 | cout << endl; 18 | 19 | delete [] oneElementAdded; 20 | oneElementAdded = nullptr; // 0x0 21 | 22 | return 0; 23 | } 24 | 25 | int *createOneNewElement(int *array, const int SIZE) 26 | { 27 | const int NEW_SIZE = SIZE + 1; 28 | int *newArray = new int[NEW_SIZE]; 29 | 30 | for(int i = 0; i < NEW_SIZE; i++) // 6 31 | { 32 | if (i == 0) 33 | *(newArray + i) = 20; // newArray[i] = 0; 34 | else 35 | *(newArray + i) = array[i - 1]; 36 | } 37 | // 20, 2, 4, 6, 8, 10 38 | 39 | return newArray; 40 | } 41 | 42 | void displayArray(int *array, const int SIZE) 43 | { 44 | for (int i = 0; i < SIZE; i++) 45 | cout << *(array + i) << " "; // array[i] 46 | } -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 10/00.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * 3. Word Counter 3 | * 4 | * Write a function that accepts a pointer to 5 | * a C-string as an argument and returns the number 6 | * of words contained in the string. For instance, 7 | * if the string argument is “Four score and seven 8 | * years ago” the function should return the number 9 | * 6. Demonstrate the function in a program that 10 | * asks the user to input a string and then passes 11 | * it to the function. The number of words in the 12 | * string should be displayed on the screen. 13 | * Optional Exercise: Write an overloaded version of this 14 | * function that accepts a string class object as its 15 | * argument. 16 | * 17 | */ 18 | #include 19 | 20 | using namespace std; 21 | 22 | int numberOfWords(const char *); 23 | 24 | int main() 25 | { 26 | const int SIZE = 80; 27 | char user_input[SIZE]; 28 | 29 | cout << "\nEnter a sentence: "; 30 | cin.getline(user_input, SIZE); 31 | 32 | cout << "Your sentence has " << numberOfWords(user_input) 33 | << " words.\n" 34 | << endl; 35 | 36 | return 0; 37 | } 38 | 39 | int numberOfWords(const char *c_string) 40 | { 41 | int number_of_words = 0; 42 | int length = 0; 43 | 44 | while (*c_string != '\0') 45 | { 46 | 47 | if (*c_string == ' ') 48 | length = 0; 49 | else if(++length == 1) 50 | number_of_words++; 51 | 52 | c_string++; 53 | } 54 | 55 | 56 | return number_of_words; 57 | } 58 | 59 | // RESOURCE USED: https://www.youtube.com/watch?v=e_k5ReiYcuw 60 | // RESOURCE USED: https://www.youtube.com/watch?v=e_k5ReiYcuw 61 | // RESOURCE USED: https://www.youtube.com/watch?v=e_k5ReiYcuw -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 10/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // strlen() 3 | 4 | using namespace std; 5 | 6 | int countNumberOfChars(const char *); 7 | 8 | int main() 9 | { 10 | const int SIZE = 18; 11 | char user_string[SIZE]; // 11111111111111111/0 12 | 13 | cout << "\nEnter a string with " << SIZE - 1 << " characters: "; 14 | cin.getline(user_string, SIZE); 15 | 16 | int number_of_characters = countNumberOfChars(user_string); 17 | 18 | cout << "user_sting has " << number_of_characters 19 | << " characters." << endl; 20 | cout << endl; 21 | 22 | return 0; 23 | } 24 | int countNumberOfChars(const char *c_string) 25 | { 26 | return strlen(c_string); 27 | } 28 | -------------------------------------------------------------------------------- /8th Edition Challenges/Chapter 10/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // strlen() 3 | 4 | using namespace std; 5 | 6 | void reverseArray(const char *); 7 | void displayCString(const char *); 8 | void getUserInfo(char *, const int); 9 | 10 | int main() 11 | { 12 | const int SIZE = 8; 13 | char user_c_string[SIZE]; 14 | 15 | getUserInfo(user_c_string, SIZE); 16 | 17 | cout << endl; 18 | displayCString(user_c_string); 19 | cout << endl; 20 | 21 | cout << endl; 22 | reverseArray(user_c_string); 23 | cout << endl << endl; 24 | 25 | return 0; 26 | } 27 | void getUserInfo(char *user_c_string, const int SIZE) 28 | { 29 | cout << "\nEnter a string with no more than " 30 | << (SIZE - 1) << " characters: "; 31 | cin.getline(user_c_string, SIZE); 32 | } 33 | 34 | void displayCString(const char *c_string) 35 | { 36 | while(*c_string != '\0') 37 | { 38 | cout << *c_string; 39 | c_string++; 40 | } 41 | } 42 | 43 | void reverseArray(const char *c_string) 44 | { 45 | for (int i = strlen(c_string); i >= 0; i--) 46 | cout << *(c_string + i); // c_string[i] 47 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jesus Hilario H. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Starting-Out-With-C-Plus-Plus-Programming-Challenges 2 | 3 | This repository is a collection of programming challenge solutions that I have completed while studying Tony Gaddis book "Starting Out with C++ from Control Structures to Objects" Each solution is my own. It's also worth mentioning, that I've completed each programming challenge without advancing to the next chapters. 4 | 5 | CHAPTER 2 VIDEO PLAYLIST: 6 | https://www.youtube.com/playlist?list=PL-ZkhVVp9xcPo1sZwWhHXJEEmW09bpa_C 7 | 8 | --------------------------------------------------------------------------------