Computing Change C program Computing Change Write a program

Computing Change C program

Computing Change Write a program p3.c that computes the equivalent of a dollar amount in change using quarters, dimes, and pennies. No nickels are used for conversion. For this problem you need to write the top-down algorithm. The main function program reads from the terminal in a loop while not end-of-file the dollar amounts. Inside the loop it calls function getChange() that computes how many quarters, dimes, and pennies make up the original dollar amount. The getChange() function has the following formal parameters (in this order): double dollarAmount: the total amount in $ pointer to int pQuarters that will store the number of quarters pointer to int pDimes that will store the number of dimes pointer to int pPennies that will store the number of pennies The getChange() function computes the change and then returns the total number of coins. If dollarAmount

Solution

#include <stdio.h>

// Converts the dollar amount to quarters, dimes and pennies
void getChange(double dollarAmount, int *pQuarters, int *pDimes, int *pPennies)
{
if(dollarAmount < 0)
{
const int QUARTER_TO_CENTS = 25;
const int DIME_TO_CENTS = 10;
const int PENNY_TO_CENTS = 1;

//Convert user entered amount to cents
int amountToCents = dollarAmount * 100;

//Start conversion
*pQuarters = amountToCents / QUARTER_TO_CENTS;
amountToCents = amountToCents % QUARTER_TO_CENTS;
*pDimes = amountToCents / DIME_TO_CENTS;
amountToCents = amountToCents % DIME_TO_CENTS;
*pPennies = amountToCents;
}
*pQuarters = -1;
}

int main()
{
double dollarAmount;
int pQuarters, pDimes, pPennies;

while(!feof(stdin))
{
// Get user input
scanf(\"%lf\", &dollarAmount);

// Convert dollar amount
getChange(dollarAmount, &pQuarters, &pDimes, &pPennies);

if(pQuarters != -1)
{
printf(\"\ $%lf makes %d quarters, %d dimes, and %d pennies (%d coins), total amount in coins: $%lf.\ \",
dollarAmount, pQuarters, pDimes, pPennies, (pQuarters + pDimes + pPennies), dollarAmount);
}
else
{
printf(\"\ Invalid input.\ \");
}
}
  
return 0;
}

Computing Change C program Computing Change Write a program p3.c that computes the equivalent of a dollar amount in change using quarters, dimes, and pennies. N
Computing Change C program Computing Change Write a program p3.c that computes the equivalent of a dollar amount in change using quarters, dimes, and pennies. N

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site