Output: Change Lab Construct a C program, change.c, which computes the minimu an
ID: 3748499 • Letter: O
Question
Output:
Change Lab Construct a C program, change.c, which computes the minimu and coins needed to make change for a particular purchase. The cerof bi is $21.17 and the amount tendered is $100.00. These values should h the item your program using assignment statements rather than input into the during program runtime. Your program should indicate how many ba of each denomination are needed for the change. You should make following denominations: be built into program of the Bills: twenty, ten, five, one Coins: quarter, dime, nickel, penny Your program should make use of integer division as well as the modulus operator ( % ) . Do not use subtraction in place of the modulus operator! Note that it will be easier to convert the cost and amount tendered into pennies so you can work with integers rather than doubles. Use the following guidelines to develop your program: Declare your variables with appropriate data types. Assign values to your variables. Perform your calculations. Generate appropriate output. Points to Remember: Make sure you are creating a C program and not a C++ program. The .c suffix to your source code will invoke the C compiler while the .cpp suffix will invoke the C++ compiler. As this is a class in C and not C++, please make sure your source code uses the .c suffix. You should not be using any global variables in your program. A global variable is a variable declared outside of main(). Focus on Fundamentals of Programming with C Page 98Explanation / Answer
Find the source code below -
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char** argv) {
double cost = 21.17;
double tendered = 100.00;
char scost[50];
char stendered[50];
sprintf(scost, "%.2lf", cost);
sprintf(stendered, "%.2lf", tendered);
FILE * f = fopen("csis.txt", "w");
printf ("%8s: %6s ", "Cost", scost);
fprintf (f, "%8s: %6s ", "Cost", scost);
printf ("%8s: %6s ", "Tendered", stendered);
fprintf (f, "%8s: %6s ", "Tendered", stendered);
printf ("---------------- ");
fprintf (f, "---------------- ");
int cost_in_pennies = cost * 100;
int tendered_in_pennies = tendered * 100;
int pennies_to_pay = tendered_in_pennies - cost_in_pennies;
int twenty, ten, five, one, quart, dime, nickel, penny;
twenty = ten = five = = dime = nickel = penny = 0;
twenty = pennies_to_pay / (20 * 100);
pennies_to_pay = pennies_to_pay % (20 * 100);
ten = pennies_to_pay / (10 * 100);
pennies_to_pay = pennies_to_pay % (10 * 100);
five = pennies_to_pay / (5 * 100);
pennies_to_pay = pennies_to_pay % (5 * 100);
one = pennies_to_pay / (1 * 100);
pennies_to_pay = pennies_to_pay % (1 * 100);
quart = pennies_to_pay / (25);
pennies_to_pay = pennies_to_pay % (25);
dime = pennies_to_pay / (10);
pennies_to_pay = pennies_to_pay % (10);
nickel = pennies_to_pay / (5);
pennies_to_pay = pennies_to_pay % (5);
penny = pennies_to_pay;
printf("%8s: %d ", "Twenty", twenty);
fprintf(f, "%8s: %d ", "Twenty", twenty);
printf("%8s: %d ", "Ten", ten);
fprintf(f, "%8s: %d ", "Ten", ten);
printf("%8s: %d ", "Five", five);
fprintf(f, "%8s: %d ", "Five", five);
printf("%8s: %d ", "One", one);
fprintf(f, "%8s: %d ", "One", one);
printf("%8s: %d ", "Quarter", quart);
fprintf(f, "%8s: %d ", "Quarter", quart);
printf("%8s: %d ", "Dime", dime);
fprintf(f, "%8s: %d ", "Dime", dime);
printf("%8s: %d ", "Nickel", nickel);
fprintf(f, "%8s: %d ", "Nickel", nickel);
printf("%8s: %d ", "Penny", penny);
fprintf(f, "%8s: %d ", "Penny", penny);
printf("Press any key to continue . . . ");
getchar();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.