C change denomination program - please fix my code!?
-
Hi. I need to create a MODULAR C program that allows the user to enter the amount of change as dollars and cents as a double. Australian dollars/cents too, so cents need to be multiples of five. We need to explicitly convert a double to an integer to break the inputted double down to its component parts as integers and must use this code: float change; int dollar, cents; ... dollar = (int) change; cents = (int) (((change – dollar)*100) + 0.5); I have done most of it so far but I would really like somebody to help me fix the code. #include <stdio.h> int GetAmount() { double change; printf("Enter the amount of cents in the change: "); scanf("%lf%*c", &dollar, &change); return (change); } int ConvertAmount() { float change; int dollar, cents; dollar = (int) change; cents = (int) (((change - dollar)*100) + 0.5); return(); } void DetermineDollars(int *dollar, int *hundredd, int *fiftyd, int *twentyd, int *tend, int *fived, int *twod, int *oned) { int change = *dollar; *hundredd = change / 100; change %= 100; *fiftyd = change / 50; change %= 50; *twentyd = change / 20; change %= 20; *tend = change / 10; change %= 10; *fived = change / 5; change %= 5; *twod = change / 2; change %= 2; *oned = change / 1; change %= 1; return; } void DetermineCents(int *cents, int *fiftyc, int *twentyc, int *tenc, int *fivec) { int change = *cents; *fiftyc = change / 50; change %= 50; *twentyc = change / 20; change %= 20; *tenc = change / 10; change %= 10; *fivec = change / 5; change %= 5; return; } int main() { int change, cents, fiftyc, twentyc, tenc, fivec, dollar, hundredd, fiftyd, twentyd, tend, fived, twod, oned; dollar = GetAmount(); cents = GetAmount(); change = ConvertAmount(); if (dollar < 1) printf("You have not entered a valid amount.\n"); else if ((cents < 5) || (cents > 95) || (cents%5 > 0)) printf("Change must be divisible by 5, and be between 5 cents and 95 cents.\n"); else { DetermineCents(¢s, &fiftyc, &twentyc, &tenc, &fivec); DetermineDollars(&dollar, &hundredd, &fiftyd, &twentyd, &tend, &fived, &twod, &oned); if (fiftyd > 0) printf("# of fifty dollar notes: %d\n", fiftyd); if (twentyd > 0) printf("# of twenty dollar notes: %d\n", twentyd); if (tend > 0) printf("# of ten dollar notes: %d\n", tend); if (fived > 0) printf("# of five dollar notes: %d\n", fived); if (twod > 0) printf("# of two dollar coins: %d\n", twod); if (oned > 0) printf("# of one dollar coins: %d\n", oned); if (fiftyc > 0) printf("# of fifty cent pieces: %d\n", fiftyc); if (twentyc > 0) printf("# of twenty cent pieces: %d\n", twentyc); if (tenc > 0) printf("# of ten cent pieces: %d\n", tenc); if (fivec > 0) printf("# of five cent pieces: %d\n", fivec); } return(0); } Any assistance a.s.a.p. is very appreciated, I want to fix it soon.
-
Answer:
Hopefully, you will spend time studying the following and learn. Your call: #include <stdio.h> #include <stdlib.h> #include <math.h> #define MAXTXT (200) const struct { char * name; long int value; } coinage[]= { { "fifty dollar notes", 5000 }, { "twenty dollar notes", 2000 }, { "ten dollar notes", 1000 }, { "five dollar notes", 500 }, { "two dollar coins", 200 }, { "one dollar coins", 100 }, { "fifty cent pieces", 50 }, { "twenty cent pieces", 20 }, { "ten cent pieces", 10 }, { "five cent pieces", 5 }, { "", 0 } }; int coin( long int total, int idx ) { long int count; if ( total < 0 || (total > 0 && coinage[idx].value == 0) ) return 0; if ( total == 0 ) return 1; count= (total / coinage[idx].value); if ( coin( total - count * coinage[idx].value, idx+1 ) ) { if ( count > 0 ) printf( "%ld %s\n", count, coinage[idx].name ); return 1; } return 0; } int main( int argc, char *argv[] ) { char buf[MAXTXT]; long int amount; for ( ; ; ) { printf( "Enter amount: " ); if ( fgets( buf, sizeof(buf), stdin ) == NULL ) break; amount= (long int) floor(100.0 * strtod( buf, NULL ) + 0.5); if ( amount <= 0 ) break; if ( !coin( amount, 0 ) ) printf( "The full amount cannot be given in change.\n" ); } return 0; }
vshh at Yahoo! Answers Visit the source
Related Q & A:
- How to Convert Code from VB to C++?Best solution by Stack Overflow
- How can I convert Matlab code to c#?Best solution by Stack Overflow
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- I want to download full encyclopedia program, please help.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.