Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Assignment1 ASSIGNMENT1 XXXXXXXXXXCredit:William 50 points 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...

1 answer below »
Assignment1
ASSIGNMENT1

XXXXXXXXXXCredit:William
50 points
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
epeatedly 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
emaining 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.
Project 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
3. When a price is entered:
• Check that the price entered is a (non-negative) multiple of .
05 (i.e., it is payable in nickels). If not, then print an e
or
message and start over requesting either a new price.
• Print a menu for indicating the coin
ill deposited or to
cancel payment.
• Prompt for a selection from this menu.
• If the user enters an illegal selection, re-prompt for a co
ect
one.
• Following each deposit, print the remaining amount owed
(indicate the number of dollars and the number of cents). 

• When full payment has been deposited or a ‘c’ has been
entered, determine the coins to be dispensed in change or as
efund. 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.
• Print the numbers of the coins to be dispensed and their
denominations. (Omit a denomination if no coins of that
denomination will be dispensed.) 

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

• 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. 

• Print the contents of the stock following the transaction. 

Sample Interaction (user inputs shown in red):
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) : 1.96
Illegal price: Must be a non-negative multiple
of 5 cents.
Enter the purchase price (xx.xx) : 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
Do you want to continue: yes
Enter the purchase price (xx.xx) : 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
Do you want to continue: yes
Enter the purchase price (xx.xx) : .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
Do you want to continue: yes
Enter the purchase price (xx.xx) : 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
Do you want to continue: yes
Enter the purchase price (xx.xx) : .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
Do you want to continue: yes
Enter the purchase price (xx.xx) : .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
Do you want to continue: no
Answered 1 days After Sep 22, 2021

Solution

Vaibhav answered on Sep 23 2021
144 Votes

nickel = $0.05
dime = $0.10
quater = $0.25
for easy calculations, nickel = 5, dime = 10, quater = 25 , dollar = 100 and
fives = 500 cents.
When program starts, assume a stock of 25 nickels, 25 dimes, and 25
quarters.Print the contents of the stock.
#include using namespace std;
**
* @
ief Program execution begins here.
*
* @return int
*
int main() {
int i;

denotes inital stock of various denomination.

stock[0] stores fives.

stock[1] stores ones.

stock[2] stores quater.

stock[3] stores dime.

stock[4] stores nickels.
int stock[5] = {0, 0, 25, 25, 25};

used for internal calculations.
int denominations[5] = {500, 100, 25, 10, 5};

denotes the change to be dispensed.
int changeToDispense[5] = {0, 0, 0, 0, 0};

price entered by the user.
double price, priceCpy;

extracting dollars and cents from the price.
int dollars, cents;

used to check if the price entered by the user is valid or invalid.
bool isValidPrice;

If user wishes to cancel the transaction, then return the money back.
bool isCancelled;

to store users choice if the want to pay for another item or not.(yes/no)
string choice;

stores the type of denomination.
char changeType;
cout
"\nWelcome to the vending machine change maker program\n";
cout
"Change maker initialized. \n";

Display the initial stock of coins.
cout
"Stock contains: \n";
for (i = 4; i >= 0; i--) {
cout
"\t";
if (i == 4) {
cout
stock[i]
" nickels\n";
} else if (i == 3) {
cout
stock[i]
" dimes\n";
} else if (i == 2) {
cout
stock[i]
" quaters\n";
} else if (i == 1) {
cout
stock[i]
" ones\n";
} else {
cout
stock[i]
" fives\n";
}
}

do while is an entry control loop.

hence the condition will not be checked for the first run.
do {
isValidPrice = false;

it is used for...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here