Programming in C General Description The ACME supermarket wants to install sever
ID: 641759 • Letter: P
Question
Programming in C
General Description
The ACME supermarket wants to install several automated checkout machines. These machines allow users to scan groceries and compute the total amount of a sale as well as insert currency to pay for the groceries. Given the total amount of the sale and the amount paid by a user, the checkout machines also compute and return the change back to the user. Your task for this assignment is to implement a program that (repeatedly) performs each of the above features. Specifically, your program should input a set of sale amounts and compute a grand total. Next, your program should input a value that represents the total amount paid by the user. Finally, your program should display the amount of change returned to the user in the form of numbers of quarters, dimes, nickels, and pennies.
Your program must operate using a large outer loop. Inside this loop, you should first input a number of grocery items, n. Afterwards, you should input n dollar amounts (one for each grocery item). After all of the n amounts are entered into your program, you should compute their sum as the total sale amount. Next, your program should input a single value representing the total amount paid by the user. Finally, your program should print the numbers of quarters, dimes, nickels, and quarters that would be returned as a result of this transaction. Note that your program should return the coins with the highest denomination that it can first. So, if the total sale amount is $1.50, and the user pays $2.00, your program should return two quarters (if it can) instead of five dimes or ten nickels.
While returning change to the user, your program needs to keep track of how many coins it contains in its bank. Initially, your bank contains 10 quarters, dimes, nickels, and pennies. So, if your program returns 2 quarters, you only have 8 quarters left in your bank. Thus, it is possible that your program returns $0.50 as five dimes instead of as two quarters. This would occur if/when your bank has no more quarters. Your program should repeat all of the above steps until a user enters a value of zero for the number of grocery items, n. When a user enters zero, your program should terminate.
Assumptions
? No negative values will be entered into your program. This includes the value of n, the value of each grocery amount, and the amount paid by the user.
? The amount of change generated after each sale will never be negative.
Explanation / Answer
Program Code:
#include <stdio.h>
#include <stdlib.h>
/*printf and scanf option*/
#include <math.h>
int qrem=100,drem=50,nrem=200,prem=25;
void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies);
void changeLeft(int *quarters, int *dimes, int *nickels, int *pennies); /*function protype*/
int main(void)
{
int dollars = 0, quarters = 0, dimes = 0, nickels = 0, pennies = 0, nitems=0;
double amt_paid = 0, amt_due = 0, amt_change = 0, coin_change = 0;
double total=0;
do{
printf(" Welcome to ACME Super Market ");
printf(" Enter total items purchased> ");
scanf("%d", &nitems);
if(nitems==0)
return 0;
double items[nitems];
int i;
for(i=0;i<nitems;i++)
{
printf(" Cost of item %d: ",(i+1) );
scanf("%lf", &items[i]);
}
printf(" Item purchased are: ");
for(i=0;i<nitems;i++)
{
printf(" Cost of item %d: %0.2lf",(i+1), items[i] );
total+=items[i];
}
printf(" -------------------------------");
printf(" Total amount: %0.2lf", total);
printf(" -------------------------------");
printf(" Enter the amount paid> "); /*Prompt user to enter amount paid*/
scanf("%lf", &amt_paid);
amt_change = amt_paid - total; /*Formula for amount of change to be given*/
dollars = (amt_change);
coin_change = (int)((amt_change - (amt_change)) * 100 + 0.5);
coin_change = (100 * amt_change);
printf(" Change amount in cents: %0.2f ", coin_change);
change(coin_change, &quarters, &dimes, &nickels, &pennies);
printf(" Change returned is: quarters: %d dimes: %d nickels: %d pennies: %d", quarters, dimes, nickels, pennies);
changeLeft(&quarters, &dimes, &nickels, &pennies);
}while(1);
getch();
return(0);
}
void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies)
{
int q = 1, d = 1, n = 1, p = 1;
do {
if(coin_change >= 25 && qrem > 0 )
{
*quarters += q;
qrem-=1;
coin_change-=25;
}
else if (qrem < 0 && coin_change >= 10 && drem > 0 )
{
*dimes += d;
drem-=1;
coin_change -= 10;
}
else if (drem < 0 && coin_change >= 5 && nrem > 0)
{
*nickels += n;
nrem-=1;
coin_change -= 5;
}
else if (coin_change >= 1 && prem > 0)
{
*pennies += p;
prem-=1;
coin_change -= 1;
}
else if(prem ==0 && nrem==0 && drem==0 && qrem==0)
{
printf(" Sorry! No change.");
}
} while (coin_change >= 1);
}
void changeLeft(int *quarters, int *dimes, int *nickels, int *pennies)
{
printf(" Amount left in the bank is: ");
printf(" Quarters: %d", (qrem));
printf(" Dimes: %d", (drem));
printf(" Nickels: %d", (nrem));
printf(" Pennies: %d", (prem));
}
Sample output:
Welcome to ACME Super Market
Enter total items purchased> 2
Cost of item 1: 3
Cost of item 2: 6
Item purchased are:
Cost of item 1: 3.00
Cost of item 2: 6.00
-------------------------------
Total amount: 9.00
-------------------------------
Enter the amount paid> 10
Change amount in cents: 100.00
Change returned is:
quarters: 4
dimes: 0
nickels: 0
pennies: 0
Amount left in the bank is:
Quarters: 96
Dimes: 50
Nickels: 200
Pennies: 25
Welcome to ACME Super Market
Enter total items purchased> 4
Cost of item 1: 1.5
Cost of item 2: 3.4
Cost of item 3: 2.8
Cost of item 4: 10
Item purchased are:
Cost of item 1: 1.50
Cost of item 2: 3.40
Cost of item 3: 2.80
Cost of item 4: 10.00
-------------------------------
Total amount: 26.70
-------------------------------
Enter the amount paid> 30
Change amount in cents: 330.00
Change returned is:
quarters: 17
dimes: 0
nickels: 0
pennies: 5
Amount left in the bank is:
Quarters: 83
Dimes: 50
Nickels: 200
Pennies: 20
Welcome to ACME Super Market
Enter total items purchased> 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.