Question

C++ HW Question Your program will simulate a simple change maker for a vending machine. It...

C++ HW Question

Your program will simulate a simple change maker for a vending machine. It will start with a stock of coins and dollars. It will then repeatedly request the price for an item to be purchased or to quit. If given a price, it will accept nickels, dimes, quarters, one-dollar and five-dollar bills—deposited one at a time—in payment. When the user has deposited enough to cover the cost of the item, the program will calculate the coins to be dispensed in change. Alternately, the user can cancel the purchase up until full payment has been deposited, in which case, your program will calculate the coins to be dispensed to refund any partial payment already deposited. With each purchase, the program will update the stock of coins and dollars. Before quitting, it will output the remaining stock of coins and dollars. The specifications are spelled out more thoroughly below. An example interaction with our program appears at the end of this description. All change and refunds must be in coins only, and must use the minimum number of coins possible.

Background

The algorithm for calculating the numbers of coins of each denomination to dispense so as to minimize the total number of coins is an example of a greedy algorithm. Since each coin is at least twice the value of the previous coin, you can start by figuring out the most number of quarters you can dispense (without exceeding the amount), then the most number of dimes, and then the number of nickels. Knowledge of greedy algorithms is not required to complete this assignment. If you are curious, you can read http://en.wikipedia.org/wiki/Greedy_algorithm.

Description / Specification

Your program must meet the following specifications:

1. At program start, assume a stock of 25 nickels, 25 dimes, and 25 quarters. Print the contents of the stock.

2. Repeatedly prompt the user for a price in the form xx.xx, where x denotes a digit, or to enter ‘q’ to quit.

3. When a price is entered:

a. Check that the price entered is a (non-negative) multiple of .05 (i.e., it is payable in nickels). If not, then print an error message and start over requesting either a new price or to quit (indicated by entering a ‘q’).

b. Print a menu for indicating the coin/bill deposited or to cancel payment.

c. Prompt for a selection from this menu.

d. If the user enters an illegal selection, re-prompt for a correct one.

e. Following each deposit, print the remaining amount owed (indicate the number of dollars and the number of cents).

f. When full payment has been deposited or a ‘c’ has been entered, determine the coins to be dispensed in change or as refund. This calculation will depend on the amount to be dispensed and also on the number of coins left in the stock. For example, the least number of coins needed to make up $1.30 is 6 — 5 quarters and 1 nickel. But if there are only 3 quarters, 3 dimes, and 10 nickels left in the stock, then the least number is 11 — 3 quarters, 3 dimes, and 5 nickels.

g. Print the numbers of the coins to be dispensed and their denominations. (Omit a denomination if no coins of that denomination will be dispensed.)

h. In case exact payment is deposited, print a message such as “No change.”

i. If the change cannot be made up with the coins remaining, dispense the coins available without exceeding the change amount and indicate the amount still due the user, which will have to be collected from a store manager. For example, if the stock contains one nickel, no dimes, and a quarter and if the change amount is 15 cents, dispense just the nickel and indicate the user should collect 10 cents from a store manager.

j. Print the contents of the stock following the transaction.

4. Just before quitting, print the total amount (the number of dollars and number of cents) left in the stock.

Notes and Hints:

1. Real numbers can be difficult to work with due to imprecision. To avoid imprecision in this program, you can multiply the price by 100, round, and convert to an whole number. For example, $1.15 is the same as 115 cents.

2. The quotient operation will be useful for finding the numbers of each coin. For example, 195/25 is 7, the most number of quarters in 195 cents. But be careful—if the stock has fewer than 7 quarters left, you will only be able to dispense the number left in the stock. For example, if there are only 6 quarters left, then you can dispense only 6 quarters and must use dimes and nickels to make up any remaining change.

3. You do not need to check for any input errors other than those mentioned in this description.

4. The logic for this project is sufficiently complex that you will need to break it into small steps that you can implement incrementally. For example, you could:

a. First implement the logic to initialize the stock, print it, and repeatedly prompt for a price until a ‘q’ is entered; just print the value that was input. Test and back up your code.

b. Next, add code to check that the input is a legal price and, if it is not, to prompt for a new value. Again, just print the value that was input. Test and back up your code.

c. Next, add code to print the menu of selections. Test and back up your code.

d. Next, add code to repeatedly prompt the user to deposit a coin until the full amount is collected or a ‘c’ is entered and to print the remaining amount owed after each deposit. Test and back up your code.

e. Next, add code to update the stock. Test and back up your code.

f. Next, add code to calculate the change or refund to be dispensed.

g. Next, add code to calculate the number of coins of each denomination to be dispensed and to update the stock.

h. Etc. It is not required that you develop the project exactly in this way. But if you don’t find a way to incrementally build and test your projects, they generally will be more difficult and take you longer to do. If you hand in a program that we can run and that meets some, but not all, of the requirements, we can grade what you managed to get working.

Sample Interaction (user inputs shown in italics):


Welcome to the vending machine change maker program
Change maker initialized.
Stock contains:
25 nickels
25 dimes
25 quarters
0 ones
0 fives

Enter the purchase price (xx.xx) or `q' to quit: 1.96
Illegal price: Must be a non-negative multiple of 5 cents.
Enter the purchase price (xx.xx) or `q' to quit: 1.95
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase

Payment due: 1 dollars and 95 cents
Indicate your deposit: 1
Illegal selection: 1
Payment due: 1 dollars and 95 cents
Indicate your deposit: o
Payment due: 95 cents
Indicate your deposit: o

Please take the change below.
1 nickels
Stock contains:
24 nickels
25 dimes
25 quarters
2 ones
0 fives

Enter the purchase price (xx.xx) or `q' to quit: 3.25
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill

'c' - cancel the purchase

Payment due: 3 dollars and 25 cents
Indicate your deposit: o
Payment due: 2 dollars and 25 cents
Indicate your deposit: d
Payment due: 2 dollars and 15 cents
Indicate your deposit: d
Payment due: 2 dollars and 5 cents
Indicate your deposit: o
Payment due: 1 dollars and 5 cents
Indicate your deposit: d
Payment due: 95 cents
Indicate your deposit: c
Please take the change below.
9 quarters
1 nickels
Stock contains:
23 nickels
28 dimes
16 quarters
4 ones
0 fives

Enter the purchase price (xx.xx) or `q' to quit: .05
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase

Payment due: 5 cents
Indicate your deposit: f
Please take the change below.
16 quarters
9 dimes
1 nickels
Stock contains:
22 nickels
19 dimes
0 quarters
4 ones
1 fives

Enter the purchase price (xx.xx) or `q' to quit: 25
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 25 dollars and 0 cents
Indicate your deposit: f
Payment due: 20 dollars and 0 cents
Indicate your deposit: f
Payment due: 15 dollars and 0 cents
Indicate your deposit: f
Payment due: 10 dollars and 0 cents
Indicate your deposit: f
Payment due: 5 dollars and 0 cents
Indicate your deposit: c

Please take the change below.
19 dimes
22 nickels
Machine is out of change.
See store manager for remaining refund.
Amount due is: 17 dollars and 0 cents
Stock contains:
0 nickels
0 dimes
0 quarters
4 ones
5 fives
Enter the purchase price (xx.xx) or `q' to quit: .35
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 35 cents
Indicate your deposit: q
Payment due: 10 cents
Indicate your deposit: d

Please take the change below.
No change due.
Stock contains:
0 nickels
1 dimes
1 quarters
4 ones
5 fives
Enter the purchase price (xx.xx) or `q' to quit: .
35
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 35 cents
Indicate your deposit:
q
Payment due: 10 cents
Indicate your deposit:
q

Please take the change below.
1 dimes
Machine is out of change.
See store manager for remaining refund.
Amount due is: 5 cents
Stock contains:
0 nickels
0 dimes
3 quarters
4 ones
5 fives
Enter the purchase price (xx.xx) or `q' to quit: q
Total: 29 dollars and 75 cents

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

ANSWER:

CODE:

#declare and intialize the variables

pennies = 10

nickels = 10

dimes = 10

quarters = 10

quarters_spent = 0

dimes_spent = 0

nickels_spent = 0

pennies_spent = 0

#Display the message

print("\nWelcome to change-making program.")

#print the initial values

print("\nStock: {} quarters, {} dimes, {} nickels, and {} pennies".format(quarters,dimes,nickels,pennies))

#prompt and read the input price

in_str = input("Enter the purchase price (xx.xx) or `q' to quit: ")

#Using while loop to iterate the loop until user enters q

while in_str.lower() != 'q':

  

    #dollar_str, cents_str = in_str.split(".")

    if in_str.lower() == 'q':

        quit()

    #round the values as int

    in_int = round(float(in_str) * 100)

    #if the input is negativeprint a message

    if in_int < 0:

        print("Error: purchase price must be non-negative.")

        in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

        print("\nStock: {} quarters, {} dimes, {} nickels, and {} pennies".format(quarters,dimes,nickels,pennies))

        in_int = round(float(in_str) * 100)

    #otherwise prompt for dollars paid

    elif in_int > 0:

        payment = input("\nInput dollars paid(int): ")

        payment_int = int(float(payment) * 100)

        change = payment_int - in_int

        #If the change is 0

        if change == 0:

            print("No change.")

            #print the actual change coins

            print("\nStock: {} quarters, {} dimes, {} nickels, and {} pennies".format(quarters,dimes,nickels,pennies))

            in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

        #prompt for the input dollars

        elif payment_int < in_int:

            print("Error: Insufficient payment.")

            payment = input("\nInput dollars paid: ")

            payment_int = round(float(payment) * 100)

            change = payment_int - in_int

        #otherwise

        else:

           

            #determines how many quarters, dimes, nickels, and pennies are left

            while change >= 25 and quarters>0:

                quarters=quarters-1

                change = change-25   

            while change >= 10 and dimes>0:            

                dimes=dimes-1

                change = change - 10

    

            while change >= 5 and nickels>0:

                nickels=nickels-1

                change = change - 5

    

            while change >= 1 and pennies > 0:

               pennies=pennies-1

                change = change - 1

            #if all the coins are empty,

            if quarters == 0 and dimes == 0 and nickels == 0 and pennies == 0:

                #then end the program

               print("Error: ran out of coins.")

               quit()

            #compute the remaining changes

            quarters_spent = 10-quarters

            dimes_spent = 10-dimes

            nickels_spent = 10-nickels

            pennies_spent = 10-pennies

                   

            print("\nCollect change Below:")   

            if quarters_spent > 0:

                print("Quarters: ",quarters_spent)

            if dimes_spent > 0:

                print("Dimes: ",dimes_spent)

            if nickels_spent > 0:

                print( "Nickels: ",nickels_spent)

            if pennies_spent > 0:

                print("Pennies: ",pennies_spent)

            print("\nStock: {} quarters, {} dimes, {} nickels, and {} pennies". format(quarters,dimes,nickels,pennies))

            in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

            pennies = pennies

            nickels = nickels

            dimes = dimes

            quarters = quarters

RATE THUMBSUP PLEASE HOPE I T HELPS YOU

THANKS

Add a comment
Know the answer?
Add Answer to:
C++ HW Question Your program will simulate a simple change maker for a vending machine. It...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Your program must meet the following specifications: 1. At program start, assume a stock of 10 nickels, 10 dimes, 10...

    Your program must meet the following specifications: 1. At program start, assume a stock of 10 nickels, 10 dimes, 10 quarters, and 10 pennies. 2. Repeatedly prompt the user for a price in the form xX.xx, where X denotes a digit, or to enter q' to quit 3. When a price is entered a. If the price entered is negative, print an error message and start over requesting either a new price or to quit (indicated by entering a 'q)...

  • Write the algorithm that determines the change to be dispensed from a vending machine. An item...

    Write the algorithm that determines the change to be dispensed from a vending machine. An item in the machine can cost between 5 cents and 1 dollar, in 5-cent increments (5,10,15,......90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. The user will provide the price and the program will output the price and calculate and output the total change and how many quarters, dimes and nickels need to be dispensed. DO...

  • (In Python 3) Write a program with total change amount as an integer input, and output...

    (In Python 3) Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes So...

  • All items at a "Penny Fair" are priced less than $1.00. Write a program that makes...

    All items at a "Penny Fair" are priced less than $1.00. Write a program that makes change with a minimum number of coins when a customer pays for an item with a one dollar bill. Prompt for the item price in cents, report the change due, and the number of coins of each denomination required. Use only quarters, dimes, nickels, and pennies for change (no 50 cent pieces). See sample output but your program should work for any item price....

  • Vending Machine (Python 3) Thinking Outside the box Write a program that asks the user to...

    Vending Machine (Python 3) Thinking Outside the box Write a program that asks the user to enter a bill value (1 = $1 bill, 5 = $5 bill, etc.) and the price of an item they want to buy in pennies and calculate their change amount in dollars, quarters, dimes, nickels, and pennies. Your code should start like this: ## # This program simulates a vending machine that gives change # Define constants PENNIES PER DOLLAR = 100 PENNIES PER...

  • it c++ coding. please write on atom software. 1. Write a program that converts dollars into coins a. Request a dolla...

    it c++ coding. please write on atom software. 1. Write a program that converts dollars into coins a. Request a dollar amount as an integer: $5.34 is input as 534 b. Use a constant variable to represent each coin as a fixed value: const int NICKEL 5; c. Use division and the mod function to calculate the number of each coin. Change Calculator Enter dollar amount (as an integer): $534 The equivalent in coins: 21 Quarters 0 Dimes 1 Nickels...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • Write a C program that calculates exact change. In order to receive full credit, please remember...

    Write a C program that calculates exact change. In order to receive full credit, please remember that only int arithmetic is exact, so you’ll need to break up your double into two ints, the one before the decimal point and the one after the decimal point. Another point worth mentioning is that the % operator gives the remainder. In other words, when working with int values, 9 / 5 = 1 whereas 9 % 5 = 4. Keep this in...

  • I made this C program to count change and make a slip saying the exact dollar...

    I made this C program to count change and make a slip saying the exact dollar and change amount given. every time I run the program it says the change given is 477256577 and i'm not sure what I have done wrong? #include<stdio.h> int main(void) { char first, last; int pennies; int nickels; int dimes; int quarters; int loonies; int change; int t_dollars; int t_cents; printf("Type in your 2 initials and press return> "); scanf("%c%c",&first,&last); printf("%c%c please enter in your...

  • The California student chapter is co-sponsoring a tea vending machine with a heavily subsidized price of...

    The California student chapter is co-sponsoring a tea vending machine with a heavily subsidized price of 15 cents a cup. Design a Mealy FSM for the vending machine assuming the students can load nickels (input N) and dimes (input D), only one coin at a time. If the user tries to enter two coins at the same time, the vending machine would not allow it, i.e. it will stay in the same state. If the user pays more than 15...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT