Design a computer program that computes the minimum number of coins and bills ne
ID: 3666999 • Letter: D
Question
Design a computer program that computes the minimum number of coins and bills needed to make change for a particular purchase. You should input the cost of the item and the amount tendered. Your program should indicate how many coins and bills of each denomination are needed for change. Make use of these denominations. Coins: $0.01, $0.05, $0.10, $0.25, $1, $2 Bills: $5, $10, $20, $50, $100 Your program should be able to process one set of input data at a time. You should run your program for all the given data sets to see if it will give you the proper output. You must input from a file and output to a file. Use end of file function for the loop input. Make sure the output format is exactly the same as given in this assignment. You must do the data validation. An appropriate error message should be printed for any invalid input data, that is, if the cost is less than or equal to 0.0, or if the amount tendered is less than the cost. You should hand in a user documentation and a programmer documentation including storage layouts and a variable list (showing all variables and their meanings.) Use the following input data sets. The first column is for 'cost' and the second column is for 'money tendered'. Your output should look like the sample output. Hint: You have to use the 'divide' and 'modulus' function for integers. You need to convert everything to cents to avoid rounding errors.SAMPLE OUTPUT cost tendered change:
cost tendered change $10 $5 $2 $1 $0.25 $0.10 $0.05 $0.01 10.50 20.00 1 2 2 3.00 10.00 1 1 2.50 5.00 1 2 4.80 10.00 1 2 3.33 5.00 1.99 1.00 not enough money tendered 0.29 1.00 6.53 10.00 0.00 10.00 cost must be greter thean zero 5.00 0.25 not enough money tendered 9.99 10.00 10.89 11.00 2.04 5.04 4.18 20.00 1 3 1 2 0.80 1.00 2Explanation / Answer
main.cpp
//header files
#include <iostream>
#include <cmath>
#include "change.hpp"
int main()
{
// declare variables
float price;
float money_given;
// asking user to enter cost
std::cout << "Enter the price of the item: $";
std::cin >> price;
// asking user to enter money given
std::cout << "Enter the money tendered: $";
std::cin >> money_given;
std::cout << " ";
// call function
ReturnChange( price, money_given );
return 0;
}
change.cpp
// header files
#include <cmath>
#include <iomanip>
#include <iostream>
#include "change.hpp"
// Static functions:
namespace
{
// This static function prints the amount of a given denomination (bill or coin).
void PrintDenomination( unsigned int amount, double denomination );
// This static function prints the amount of a given denomination (bill or coin).
void PrintDenomination( unsigned int amount, double denomination )
{
if( amount == 0 )
{
return;
}
std::cout << amount ;
if( denomination == 100 )
{
std::cout << " $100 bill";
}
else if( denomination == 50 )
{
std::cout << " $50 bill";
}
else if( denomination == 20 )
{
std::cout << " $20 bill";
}
else if( denomination == 10 )
{
std::cout << " $10 bill";
}
else if( denomination == 5 )
{
std::cout << " $5 bill";
}
else if( denomination == 2 )
{
std::cout << " $2 bill";
}
else if( denomination == 1 )
{
std::cout << " $1 bill";
}
else if( denomination == 0.25 )
{
std::cout << " $0.25 bill";
}
else if( denomination == 0.10 )
{
std::cout << " $0.10 bill";
}
else if( denomination == 0.05 )
{
std::cout << " $0.05 bill";
}
else if( denomination == 0.01 )
{
if( amount == 1 )
{
std::cout << " $0.01 bill";
}
else
{
std::cout << " penies";
}
}
else
{
std::cout << "Error: PrintDenomination() was given an invalid "
<< "denomination ("
<< denomination
<< "). Bugfix required. ";
exit( 2 );
}
if( amount > 1 && denomination != 0.01 )
{
std::cout << "s";
}
std::cout << " ";
return;
}
} // Unnamed namespace
// This function displays the change necessary, if any, for a given purchase.
void ReturnChange( double price, double money_given )
{
if( price <= 0 )
{
std::cout << "Error: That is not a valid price. ";
exit( 1 );
}
else if( money_given < 0 )
{
std::cout << "That is not a valid amount for money given. ";
exit( 1 );
}
double change = money_given - price;
if( change == 0 )
{
std::cout << "Exact change. ";
}
else if( change < 0 )
{
change = 0 - change;
std::cout << "Not enough money was tendered; $";
std::cout << std::setprecision(2) // Prints only two decimal places
<< std::fixed
<< change;
std::cout << " more is required. ";
}
else
{
std::cout << "The change is $";
std::cout << std::setprecision(2) // Prints only two decimal places
<< std::fixed
<< change;
std::cout << ", which is: ";
double denominations[11] = { 100, 50, 20, 10, 5, 2, 1, 0.25, 0.10, 0.05, 0.01 };
unsigned int leftover;
unsigned int coins_bills;
for( int i = 0; i < 11; i++ )
{
// Sets coins_bills to be the necessary amount of a specific denomination,
// and sets change to the remaining change to be calculated.
leftover = std::fmod( change, denominations[i] );
coins_bills = ( change - leftover ) / denominations[i];
change -= coins_bills * denominations[i];
PrintDenomination( coins_bills, denominations[i] );
}
}
return;
}
change.hpp
#pragma once
// This function displays the change necessary, if any, for a given purchase.
void ReturnChange( double price, double money_given );
Sample Output
Enter the price of the item: $2.50
Enter the money tendered: $5.00
The change is $2.50, which is:
1 $2 bill
2 $0.25 bills
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.