This problem is adapted from Programming Project 1 in chapter 6 of the text. Wri
ID: 3634354 • Letter: T
Question
This problem is adapted from Programming Project 1 in chapter 6 of the text.
Write a program for a machine that dispenses money. The machine can dispense both paper money and coins. The denominations dispensed are $20, $5, $1, 25¢, 10¢, 5¢, and 1¢.
Your program should consist of the function:
void dispense(double amount, int* twenties, int* fives, int* ones, int* quarters, int* dimes, int* nickels, int* penneys);
and a main function that prompts for the amount and prints the individual amounts dispensed. If a denomination is not dispensed, then it should not be listed. The main function should loop until 0 is entered for an amount.
The function dispense should not do any input or output. All input and output should be done by main.
So far, I have learned upto "functions with simple output parameters" including array, if, and basic things, so please don't use any other "things" please :D
Explanation / Answer
#include #include void dispense(double amount, int* twenties, int* fives, int* ones, int* quarters, int* dimes, int* nickels, int* penneys); int main() { // Declare variables and pointers. double amt = 0; int *numTwenties, *numFives, *numOnes, *numQuarts, *numDimes, *numNickels, *numPenneys; // Initialize space in memory numTwenties = (int*) malloc(sizeof(int)); numFives = (int*) malloc(sizeof(int)); numOnes = (int*) malloc(sizeof(int)); numQuarts = (int*) malloc(sizeof(int)); numDimes = (int*) malloc(sizeof(int)); numNickels = (int*) malloc(sizeof(int)); numPenneys = (int*) malloc(sizeof(int)); // Loop runs until user enters 0. while (1) { // Get user input. printf("Enter the amount: "); scanf("%lf", &amt); // Stop when 0 is entered. if (amt == 0) break; // Call the dispense function and pass in amt, and all // out pointers. dispense(amt, numTwenties, numFives, numOnes, numQuarts, numDimes, numNickels, numPenneys); // Display output. printf("Here are the individual amounts: "); printf("BILL QTY "); printf("Twenty %d ", *numTwenties); printf("Five %d ", *numFives); printf("One %d ", *numOnes); printf("Quarter %d ", *numQuarts); printf("Dime %d ", *numDimes); printf("Nickel %d ", *numNickels); printf("Penny %d ", *numPenneys); } // End of program. return 0; } // Function to determine the number of denominations a change machine // would spit out when given amount void dispense(double amount, int* twenties, int* fives, int* ones, int* quarters, int* dimes, int* nickels, int* penneys) { // Multiply amount by 100 so we don't have to worry about // decimals (i.e. $4.99 would be represented as 499) int intAmt = (int) (amount * 100); // Number of twenties equals intAmt / (20 * 100) // 20 is the value of a 20, and 100 is to keep things in // line with our multiply of 100 to amount above. Afterward, // we need to subtract the amount we just took care of. // (i.e. number of twenties * 2000) *twenties = (int) (intAmt / 2000); intAmt -= (double) (*twenties * 2000); // Number of fives equals intAmt / (5 * 100) // For explanation, see above. *fives = (int) (intAmt / 500); intAmt -= (double) (*fives * 500); // And so on... *ones = (int) (intAmt / 100); intAmt -= (double) (*ones * 100); *quarters = (int) (intAmt / 25); intAmt -= (double) (*quarters * 25); *dimes = (int) (intAmt / 10); intAmt -= (double) (*dimes * 10); *nickels = (int) (intAmt / 5); intAmt -= (double) (*nickels * 5); // NOTE: the divide by 1, and the multiply by 1 are unnecessary // I just kept them in there to continue the pattern as seen // above ^_^ *penneys = (int) (intAmt / 1); intAmt -= (double) (*penneys * 1); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.