math.h and ceil explanation
Code:
#define taxRate .06#include <math.h>
float legalInput( float minimum );
float roundUpToPenny( float amount );
void makeChange ( const float denoms[], int n, float tend, float cost );
int main ( void ) {
const float denoms[] = {20, 10, 5, 1, .25, .10, .05, .01};
const int nDenoms = 8;
float price; // Amount on price tag
float tendered; // Money from customer
float total; // price + tax
printf("Making Change Please enter price and amount tendered, when prompted.\n" );
price = legalInput( 0.01 );
total = roundUpToPenny( price * (1 + taxRate));
tendered = legalInput( total );
printf( "\nPrice = %.2f + tax = %.2f\n", price, total );
printf( "Amount tendered = %.2f\n", tendered );
makeChange ( denoms, nDenoms, total, tendered );
return 0;
}
// -----------------------------------------------------------------------------
// Round an amount up to the nearest penny.
float
roundUpToPenny( float amount ){
return ceil( amount*100 ) / 100;
}
// -----------------------------------------------------------------------------
// Prompt user to input a dollar amount greater than or equal to the minimum.
float
legalInput( float minimum ) {
float input;
// puts("legalInput called.");
for (;;) {
printf ("Enter value >= %.2f: ", minimum );
scanf ("%f", &input );
if (input >= minimum) break;
printf ("Try again; enter a number >%f: ", minimum );
}
return input;
}
// -----------------------------------------------------------------------------
// Given a price and an amount tendered,
// output a list of coins and bills needed to make change.
void
makeChange ( const float denoms[], int n, float cost, float tend )
{
//puts("\n makeChange called.");
float change; // Money returned to customer
float remaining; // Amount of change not yet printed
int k = 0;
remaining = change = tend - cost;
if (change < 0) {
printf( "Please give me $%.2f more.\n", -change );
}
else printf( "Total change = %.2f\n", change );
while (remaining > 0 && k<8) {
if (remaining >= denoms[k]-.001) {
//if (diff > .001) {
remaining -= denoms[k];
printf("\t%5.2f\n", denoms[k] );
}
else ++k;
}
}
if i input 5.00, why is the tax 31 cents and not 30 cents?
thanks in advance