Realcatherinedo.files.wordpress.com



Chapter 4 Programming Challenges, Questions not assigned5. Book Club PointsAn online book club awards points to its customers based on the number of books purchased each month. Points are awarded as follows: Books purchasedPoints Earned00152153304 or more60// Chapter 4 - Programming Challenge 5, Book Club Points// This program awards points to book club members // based on the number of books purchased in a month.// An if / else if statement is used for the branching logic.// A switch statement could also have been used.#include <iostream>#include <iomanip>using namespace std;int main(){int numBooks, // Number of books purchased in a monthpoints; // Number of points earned// Input the number of books purchasedcout << "How many books did you purchase through the book club this month? ";cin >> numBooks;// Determine the number of points awardedif (numBooks <= 0)points = 0;else if (numBooks == 1)points = 5;else if (numBooks == 2)points = 15;else if (numBooks == 3)points = 30;else // numBooks must be 4 or morepoints = 60;// Display the resultcout << "You earned " << points << " points this month. \n";return 0;}Write a program that asks the user to enter the number of books purchased this month and then displays the number of points awarded.6. Change for a Dollar GameCreate a change-counting game that asks the user to enter what coins to use to make exactly one dollar. The program should ask the user to enter the number of pennies, nickels, dimes, and quarters. If the total value of the coins entered is equal to one dollar, the program should congratulate the user for winning the game. Otherwise, the program should display a message indicating whether the amount entered was more or less than one dollar. Use constant variables to hold the coin values.InputProcessingOutputPenniesNickelsDimesquartersTotal value = pennies * 0.01Total value = nickels * 0.05Total value = dimes * 0.10Total value = quarters * 0.25Total value = one dollarTotal value > one dollarTotal value < one dollar// Chapter 4 - Programming Challenge 6, Change for a Dollar Game// This program challenges the user to enter a combination of coins// that equals exactly one dollar.#include <iostream>using namespace std;int main(){const double PENNY_VALUE = .01, NICKEL_VALUE = .05, DIME_VALUE = .10, QUARTER_VALUE = .25, GOAL = 1.00; int numPennies, numNickels, numDimes, numQuarters;double total;// Input the number of each denomination to use.cout << "The goal of this game is to enter a combination of quarters,\n" << "dimes, nickels, and pennies that add up to exactly one dollar. \n\n";cout << "How many quarters should I use? ";cin >> numQuarters;cout << "How many dimes should I use? ";cin >> numDimes;cout << "How many nickels should I use? ";cin >> numNickels;cout << "How many pennies should I use? ";cin >> numPennies;total = numQuarters * QUARTER_VALUE + numDimes * DIME_VALUE + numNickels * NICKEL_VALUE + numPennies * PENNY_VALUE;if (total > GOAL)cout << "\nYour coins total $" << total << ". That is more than a dollar. \n";else if (total < GOAL)cout << "\nYour coins total $" << total << ". That is less than a dollar. \n";elsecout << "\nCongratulations. Your coins total exactly a dollar. \n";return 0;}7. Time CalculatorWrite a program that asks the user to enter a number of seconds. ? There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86400, the program should display the number of days in that many seconds.? There are 3600 seconds in an hour. If the number of seconds entered by the user is less than 86400, but is greater than or equal to 3600, the program should display the number of hours in that many seconds.? There are 60 seconds in a minute. If the number of seconds entered by the user is less than 3600, but is greater than or equal to 60, the program should display the number of minutes in that many seconds.8. Math Tutor Version 2This is a modi?cation of the math tutor problem in Chapter 3. Write a program that can be used as a math tutor for a young student. The program should display two random numbers between 10 and 50 that are to be added, such as: 24+ 12 ——The program should then wait for the student to enter the answer. If the answer is correct, a message of congratulations should be printed. If the answer is incorrect, a message should be printed showing the correct answer.InputProcessingOutputRandom1Random2Answer = random1 + random2If correct the message is congratulationIf incorrect, the correct answer will be printed// Chapter 4 - Programming Challenge 8, Math Tutor Version 2// This program creates a random addition problem and allows an answer// to be entered. It then reports whether or not the entered answer is correct.// It is an enhanced version of the Chapter 3 Math Tutor Programming Challenge.#include <iostream>#include <iomanip>#include <cstdlib> // Needed to use rand() and srand()#include <ctime> // Needed to "seed" the random number generatorusing namespace std;int main(){const int MIN_NUM = 10, // Valid numbers are between 10 and 50 MAX_NUM = 50; unsigned seed; // Seed for the random number generatorint rangeSize, // The size of the range of valid numbers number1, // The two randomly generated numbers to be added number2, correctAnswer, // Correct answer to the problem studentAnswer; // Student's entered answerseed = time(0); // Assigns a system-generated seed // Alternatively, student can be prompted to enter a seedsrand(seed);// Produce and display a problemrangeSize = MAX_NUM - MIN_NUM + 1; number1 = rand() % rangeSize + MIN_NUM;number2 = rand() % rangeSize + MIN_NUM;cout << "\n " << setw(2) << number1 << endl;cout << "+ " << setw(2) << number2 << endl;cout << " -- \n "; // Calculate the correct answer correctAnswer = number1 + number2; // Input student answercin >> studentAnswer;// Display whether it is right or wrongif (studentAnswer == correctAnswer)cout << "\n\nCongratulations! That's right.\n";elsecout << "\n\nSorry, the correct answer is " << correctAnswer << endl;return 0;}9. Software Sales A software company sells a package that retails for $99. Quantity discounts are given according to the following table.Write a program that asks for the number of units purchased and computes the total cost of the purchase. Input Validation: Decide how the program should handle an input of less than 0.QuantityDiscount10-1920%20-4930%50-9940%110 or more50%// Chapter 4 - Programming Challenge 9, Software Sales// This program computes the total cost of a purchase with // a discount based on the number of units purchased.#include <iostream>#include <iomanip>using namespace std;int main(){const double RETAIL = 99.00; // Normal retail price per unit before discountint unitsPurchased; // Number of units purchaseddouble discountPct, // % discount based on sales volume totalCost; // Total cost (for all units purchased) after discount // Input number of units soldcout << "How many units are being purchased? ";cin >> unitsPurchased;// Determine discount percentageif (unitsPurchased <= 0)cout << "Units purchased must be greater than zero.\n";else{if (unitsPurchased < 10)discountPct = 0.00;else if (unitsPurchased <= 19)discountPct = 0.20;else if (unitsPurchased <= 49)discountPct = 0.30;else if (unitsPurchased <= 99)discountPct = 0.40;else // unitsPurchased was 100 or morediscountPct = 0.50; // Calculate total costtotalCost = unitsPurchased * RETAIL * (1 - discountPct);// Display formatted resultcout << fixed << showpoint << setprecision(2);cout << "The total cost of the purchase is $" << totalCost << endl;}return 0;}10. Bank ChargesA bank charges $10 per month plus the following check fees for a commercial checking account:$.10 each for fewer than 20 checks$.08 each for 20–39 checks$.06 each for 40–59 checks$.04 each for 60 or more checksWrite a program that asks for the number of checks written during the past month, then computes and displays the bank’s fees for the month.Input Validation: Decide how the program should handle an input of less than 0.11. Geometry CalculatorWrite a program that displays the following menu:Geometry Calculator1. Calculate the Area of a Circle2. Calculate the Area of a Rectangle3. Calculate the Area of a Triangle4. QuitEnter your choice (1-4):If the user enters 1, the program should ask for the radius of the circle and then display its area. Use 3.14159 for π. If the user enters 2, the program should ask for the length and width of the rectangle, and then display the rectangle’s area. If the user enters 3, the program should ask for the length of the triangle’s base and its height, and then display its area. If the user enters 4, the program should end.Input Validation: Decide how the program should handle an illegal input for the menu choice or a negative value for any of the other inputs.12. Running the Race Write a program that asks for the names of three runners and the time it took each of them to ?nish a race. The program should display who came in ?rst, second, and third place.Think about how many test cases are needed to verify that your problem works correctly.(That is, how many different ?nish orders are possible?)Input Validation: Only allow the program to accept positive numbers for the times.13. Personal BestWrite a program that asks for the name of a pole vaulter and the dates and vault heights (in meters) of the athlete’s three best vaults. It should then report in height order (best ?rst), the date on which each vault was made, and its height.Input Validation: Only allow the program to accept values between 2.0 and 5.0 for the heights.14. Body Mass IndexWrite a program that calculates and displays a person’s body mass index (BMI). TheBMI is often used to determine whether a person with a sedentary lifestyle is overweight or underweight for his or her height. A person’s BMI is calculated with the following formula:BMI = weight × 703/height2 where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A sedentary person’s weight is considered to be optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI value is greater than 25, the person is considered to be overweight.Input Validation: Determine what inputs the program needs the user to enter and what legal values the program should accept for these inputs.15. Fat Gram CalculatorWrite a program that asks for the number of calories and fat grams in a food. The program should display the percentage of calories that come from fat. If the calories from fat are less than 30 percent of the total calories of the food, it should also display a message indicating the food is low in fat.One gram of fat has 9 calories, soCalories from fat = fat grams * 9The percentage of calories from fat can be calculated asCalories from fat ÷ total caloriesInput Validation: The program should make sure that the number of calories is greater than 0, the number of fat grams is 0 or more, and the number of calories from fat is not greater than the total number of calories.16. The Speed of Sound The speed of sound varies depending on the medium through which it travels. In general, sound travels fastest in rigid media, such as steel, slower in liquid media, such as water, and slowest of all in gases, such as air. The following table shows the approximate speed of sound, measured in feet per second, in air, water, and steel. Write a program that displays a menu allowing the user to select air water, or steel. After the user has made a selection, the number of feet a sound wave will travel in the selected medium should be entered. The program will then display the amount of time it will take.(Round the answer to four decimal places.)Input Validation: Decide how the program should handle an illegal input for the menu choice or a negative value for the distance.17. The Speed of Sound in GasesWhen traveling through a gas, the speed of sound depends primarily on the density of the medium. The less dense the medium, the faster the speed will be. The following table shows the approximate speed of sound at 0 degree celsius, measured in meters per second, when traveling through carbon dioxide, air, helium, and hydrogen. Write a program that displays a menu allowing the user to select one of these 4 gases. After a valid selection has been made, the program should ask the user to enter the number of seconds (0 to 30) it took for the sound to travel in this medium from its source to the location at which it was detected. The program should then report how far away (in meters) the source of the sound was from the detection location.Input Validation: The program should ensure that the user has selected one of the available menu choices and should only prompt for the number of seconds if the menu choice is legal. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download