Programming Projects require more problem-solving than Practice Programs and can usually be solved many different ways. Visit www.myprogramminglab.com to complete many of these Programming Projects online and get instant feedback.
Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and one for output. Record the AM/PM information as a value of type char, ‘A’ for AM and ‘P’ for PM. Thus, the function for doing the conversions will have a call-by-reference formal parameter of type char
to record whether it is AM or PM. (The function will have other parameters as well.) Include a loop that lets the user repeat this computation for new input values again and again until the user says he or she wants to end the program.
Write a program that requests the current time and a waiting time as two integers for the number of hours and the number of minutes to wait. The program then outputs what the time will be after the waiting period. Use 24-hour notation for the times. Include a loop that lets the user repeat this calculation for additional input values until the user says she or he wants to end the program.
Modify your program for Programming Project 2 so that it uses 12-hour notation, such as 3:45 PM.
Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cents. For example, if the amount is 86 cents, the output would be something like the following:
86 cents can be given as
3 quarter(s) 1 dime(s) and 1 penny(pennies)
Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies). Do not use nickel and half-dollar coins. Your program will use the following function (among others):
void computeCoins(int coinValue, int& num, int& amountLeft);
//Precondition: 0 < coinValue < 100; 0 <= amountLeft < 100.
//Postcondition: num has been set equal to the maximum number
//of coins of denomination coinValue cents that can be obtained
//from amountLeft. Additionally, amountLeft has been decreased
//by the value of the coins, that is, decreased by
//num * coinValue.
For example, suppose the value of the variable amountLeft
is 86
. Then, after the following call, the value of number will be 3
and the value of amountLeft
will be 11
(because if you take 3 quarters from 86 cents, that leaves 11 cents):
computeCoins(25, number, amountLeft);
Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. (Hint: Use integer division and the % operator to implement this function.)
In cold weather, meteorologists report an index called the windchill factor, that takes into account the wind speed and the temperature. The index provides a measure of the chilling effect of wind at a given air temperature. Windchill may be approximated by the formula:
where
v = wind speed in m/sec
t = temperature in degrees Celsius: t <= 10
W = windchill index (in degrees Celsius)
Write a function that returns the windchill index. Your code should ensure that the restriction on the temperature is not violated. Look up some weather reports in back issues of a newspaper in your university library and compare the windchill index you calculate with the result reported in the newspaper.
In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.
To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. And that man would be remembered as the greatest puzzler of all time.
Write a function to simulate a single shot. It should use the following declaration:
void shoot(bool& targetAlive, double accuracy);
This would simulate someone shooting at targetAlive
with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy
, then the target is hit and targetAlive
should be set to false. Chapter 4 illustrates how to generate random numbers.
For example, if Bob is shooting at Charlie, this could be invoked as:
shoot(charlieAlive, 0.5);
Here, charlieAlive
is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.
An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel
that uses the shoot
function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot
function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.
In your main function, invoke the startDuel
function 1000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.
A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.
Write a program that inputs a date (for example, July 4, 2008) and outputs the day of the week that corresponds to that date. The following algorithm is from http:/
bool isLeapYear(int year);
This function should return true
if year
is a leap year and false
if it is not. Here is pseudocode to determine a leap year:
leapYear = (year divisible by 400) or (year divisible by 4 and year not divisible by 100))
int getCenturyValue(int year);
This function should take the first two digits of the year (that is, the century), divide by 4, and save the remainder. Subtract the remainder from 3 and return this value multiplied by 2. For example, the year 2008 becomes: (20/4) = 5 with a remainder of 0. 3 - 0 = 3. Return 3 * 2 = 6.
int getYearValue(int year);
This function computes a value based on the years since the beginning of the century. First, extract the last two digits of the year. For example, 08 is extracted for 2008. Next, factor in leap years. Divide the value from the previous step by 4 and discard the remainder. Add the two results together and return this value. For example, from 2008 we extract 08. Then (8/4) = 2 with a remainder of 0. Return 2 + 8 = 10.
int getMonthVa0lue(int month, int year);
This function should return a value based on the table below and will require invoking the isLeapYear
function.
Finally, to compute the day of the week, compute the sum of the date’s day plus the values returned by getMonthValue, getYearValue,
and getCenturyValue.
Divide the sum by 7 and compute the remainder. A remainder of 0 corresponds to Sunday, 1 corresponds to Monday, etc., up to 6, which corresponds to Saturday. For example, the date July 4, 2008 should be computed as (day of month) 1 (getMonthValue)
1 (getYearValue)
1 (getCenturyValue)
= 4 + 6 + 10 + 6 = 26. 26/7 = 3 with a remainder of 5. The fifth day of the week corresponds to Friday.
Your program should allow the user to enter any date and output the corresponding day of the week in English.
This program should include a void
function named getInput
that prompts the user for the date and returns the month, day, and year using pass-by-reference parameters. You may choose to have the user enter the date’s month as either a number (1–12) or a month name.
Complete the previous Programming Project and create a top-level function named dayOfWeek
with the header:
int dayOfWeek(int month, int day, int year);
The function should encapsulate the necessary logic to return the day of the week of the specified date as an int
(Sunday = 0, Monday = 1, etc.) You should add validation code to the function that tests if any of the inputs are invalid. If so, the function should return –1 as the day of the week. In your main function write a test driver that checks if dayOfWeek
is returning the correct values. Your set of test cases should include at least two cases with invalid inputs.
Write a program that plays a simplified dice version of blackjack. Instead of selecting from a deck of cards, a 14-sided die is rolled to represent a card. The rules of the game for one round, as played against the house, are as follows:
A roll randomly selects from the values 2–10 or Jack, Queen, King, or Ace. An Ace always has the value of 11. Jack, Queen, and King all have the value of 10. In other words, with equal probability, a roll selects one of 2,3,4,5,6,7,8,9,10,10,10,10,11.
The human player enters a wager. The wager must be positive and must not exceed the amount of money possessed by the player.
The player is given two rolls. The house, played by the computer, is given one roll. The player is shown the value of the house’s roll.
If the total of the rolls is 22 or greater than the player “busts”, loses the wager, and the round is over.
If the total of the rolls is 21 or less then the player has the option to “hit” or “stand”. If the player opts to “hit” then the player is given another roll. Return to step 4. If the player opts to “stand” then continue to step 6.
It is now the house’s turn. The house is given an additional roll and the values are summed. If the total is less than 17 then the house must “hit”. If the house has a total of 22 or greater than the house “busts”, the player wins the wager, and the round is over. The house must continue to hit until it either “busts” or has a value between 17 and 21.
If the house did not “bust” then its value is compared to the player’s total. If the totals are the same then it is a “push” and the player does not win or lose anything. If the house’s total is greater than the player’s, then the player loses the wager. If the player’s total is greater than the house’s, then the player wins the wager.
Write a program that repeats a round until the player decides to quit or runs out of money. Start the player with $100.
Write your program using the bottom-up development methodology. You should write drivers to test your functions before writing higher-level code to implement the game logic. At a minimum, you should start with the following functions:
int rollDice(); |
Rolls a 2–10, Jack, Queen, King, or Ace and returns the value. |
int getWager(int money) |
The parameter money is the amount of money possessed by the player. The function inputs a wager from the keyboard and returns the amount. The function should ensure that the amount entered is valid (positive and <= money). If the amount is invalid, the human should be prompted to enter another value. |
After those functions have been tested using your drivers, move up the chain of abstraction to implement these functions:
|
Implements the game logic for one round for the human player (steps 2–5 above). The parameter money is the amount of money possessed by the player. wager returns the amount of money wagered by the player. bust returns |
|
Implements the logic for one round for the house or computer player (step 6 above). houseRoll is the value of the first roll made by the house that was made visible to the player. bust returns true if the house busted, false otherwise. total returns the sum of the rolls by the house. The logic to test the winner of the round (step 7 above) is handled in the code that calls houseTurn, not inside houseTurn. |
Test these functions to ensure that they are working properly. Finally, write the logic for the final game. If desired, you can write additional functions as you deem necessary.
Do Programming Project 9 except write it using the top-down methodology instead of the bottom-up methodology. This means writing the game logic in the main method first, with stubs for playerTurn and houseTurn. A simple way to implement the stubs that still gives you flexibility in testing is to just input values from the keyboard for the reference variables. For example, to simulate the player wagering $50, the house rolling a 6, and the player staying at a total of 15, the function would input 50 into wager, set bust to false
, input 15 into total, and input 6 into houseRoll
.
After the logic in the main function is working, implement the code in playerTurn and houseTurn using stubs for rollDice and getWager. Once again, a simple technique to implement the stubs is to input values from the keyboard.
After playerTurn and houseTurn are working, implement the logic in rollDice
and getWager
to complete the program.