C Language Change Making Program

Discussion in 'Mac Programming' started by fily, May 16, 2008.

  1. fily macrumors regular

    fily

    Joined:
    May 12, 2008
    #1
    Thought I'd ask you fine gentlemen and women another question, since my last one was answered to perfectly.

    I basically just need to make a program with C that asks the USER to input a cost, and subtract it from one hundred.

    It's also suppose to have 5 dollars, nickels and quarters. It's easy enough I know, but I, for the life of me, can't get passed 2 dollars. I can have it tell me how many Twenties, Tens and Fives they need to give back, but after that, I'm just incredibly stumped.

    This is what I have so far, this is my 3rd attempt, writing the code more clearly from my other attempts.

    Code:
    #include <stdio.h>
                                         
    int main(void)
    {
    	
    	int num;
    	int change;
    	int rea20;							/*Remaining after Twenty*/
    	int rea10;							/*Reamining after Ten   */
    	int rea5;							/*Reamining after five  */
    	
    	printf("Enter the price of the purchase in dollars ($100 or less): \n");
    		scanf("%d", &num);
    		
    		
    	printf("The change from $100 is: %d\n", 100 - num);
    		change = 100 - num;
    	
    	printf("The number of $20 bills is: %d\n", change / 20);
    		rea20 = change % 20;
    		
    	printf("The number of $10 bills is: %d\n", rea20 / 10);
    		rea10 = rea20 % 10;
    		
    	printf("The number of $5 bills is: %d\n", rea10 / 5);
    	
    
    If someone could show me how to just get the toonie, but nothing else. I just need a push in the proper direction, so I can than figure out the rest on my own.

    Thanks in advance.

    EDIT: Actually if I could get help for the cents instead, as I just figured out the toonie and loonie.. go figure.
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Just do all your working out in cents not dollars. It will be much easier that way.

    Edit : Just work it out on a piece of paper. You are not having problems programming, you are having problems with the maths. Just do it on paper and then just transfer the sums to your program and voila!

    Edit 2 : Initialise your variables as well :).
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    As Cromulent said, working in cents is much easier. if you must take input as a floating point value, do so (use the %lf for a double or %f for a float, rather than %d), then multiply times 100 to get the input in cents. Subtract that value from 10000, and you know how many cents you need to give as change. From there you just have to divide and mod away.

    20s: Divide by 2000, mod by 2000 and reassign.
    10s: Divide by 1000, mod by 1000 and reassign.
    ...
    Quarters: Divide by 25, mod by 25 and reassign.
    ...

    -Lee

    P.S. This assumes no input validation at all. Input validation does muck things up, but you should only read into very large character arrays using a "safe" read of only the length of the buffer. After doing so, you have to make sure if you want a number, you got one, and if you have an upper and lower bound (0.00 and 100.00) that the number is in that range, etc.
     
  4. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #4
    If you insist on using "whole dollar arithmetic" instead of cents, you probably ran into the problem that you tried to use the modulo operator on floating point values, by trying

    amount % 0.25

    for the quarters, and got an error saying % only works for integers.

    In that case, look up the "fmod()" function.

    Good luck,
    Sander
     
  5. enkidu macrumors newbie

    Joined:
    Apr 20, 2008
    #5
    Or alternatively, multiply the amounts by 100 to do all calculations in cents. Same div for number of each currency, mod for remainder algorithm should work.
     
  6. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #6
    Rather than using rea20, rea10, rea5, and so on, I'd keep one variable "change" for the amount of change left to be given, because that's how you'd think of the process in your head.

    To make change for $53.22, you'd think how many $20s? Two. Print that fact. Subtract $20 times 2.

    Now you have $13.22. How many $10s? One. Print that fact. Subtract $10.

    Now you have $3.22. How many $5s? Zero. Nothing to print. Subtract $5.

    You still have $3.22. How many $1s? And so on.
     
  7. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #7
    Obviously just a typo, but just in case someone types this in, bases their multi-million multinational business on it, and turns around to sue you. ;-)
     
  8. fily thread starter macrumors regular

    fily

    Joined:
    May 12, 2008
    #8
    Yeah, I ended up figuring that out the other day on my own, I just kept reassigning my remainder into Change, which worked out much, much better. It got much too confusing using all these different Integers. But yeah, my problem now is the problem I get with trying to mod floating points, but I noticed the post above, mentioning the fmod(), definitely need to look that up, since this stupid lab is due tomorrow.

    As of right now, this is what my code looks like.

    #include<stdio.h>

    int main(void)
    {

    int num = 0; /*Number to be inputed by user*/
    int change = 0; /*The change the user will receive when subtraced from 100*/

    printf("Enter the price of the purchase in dollars ($100 or less): \n");
    scanf("%d", &num);

    printf("The change from $100 is: %d\n", change = 100 - num);

    printf("The number of $20 bills is: %d\n", change / 20);
    change = change % 20; /*Change now equals the remainder of change divided by 20*/

    printf("The number of $10 bills is: %d\n", change / 10);
    change = change % 10; /*Change now equals the remainder of change divided by 10*/

    printf("The number of $5 bills is: %d\n", change / 5);
    change = change % 5; /*Change now equals the remainder of change divided by 5*/

    printf("The number of $2 coins is: %d\n", change / 2);
    change = change % 2; /*Change now equals the remainder of change divided by 2*/

    printf("The number of $1 coins is: %d\n\n", change);

    }
     
  9. fily thread starter macrumors regular

    fily

    Joined:
    May 12, 2008
    #9
    Yeah, I can't figure out this fmod() thing, and my teacher is no help, says he's never used fmod before.. some teacher he is..

    Could anyone please use fmod in an example, possibly using some of my code?
     
  10. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #10
    You can Google any function name and it normally gives you a good reference page for that specific function.

    fmod()
     
  11. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #11
    fmod(a,b) returns the remainder of a/b. So, if a is 0.25, and b is 1.37, fmod(a,b) would return 0.12.

    So if you have quarters, dimes, nickles, you do each in turn.
     
  12. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
    Multiplying by 100 is so much easier! Money is not floating point in the vast majority of uses (gas stations that quote prices as $3.49 9/10 are just *******s, and otherwise only banks that deal with interest and the fractions of a cent it produces care about floating point math when it comes to money)!

    The smallest unit of currency in the EU, US, etc. is one cent or eurocent or whatever you want to call it. Everything else is multiples of that. So a cent is 1 unit of currency, a nickel is 5, a dollar is 100, 20 dollars is 2000, etc.

    Floating point math on a computer, especially using a 4-byte IEEE-734 number is inherently inexact. There's no reason to introduce losses of precision when you are dealing with fixed point math.

    As I stated above, if you must read the value as $xxx.yy, that's fine. But that's the only operation you need to use a floating point value for. After that, multiply by 100, then compare to (int)10000 to get the change, and work with an int from then on. Then you can use the plain, built-in / and % operators to do your calculations.

    It's great to know fmod is out there and how to use it, but in this case it's totally beyond the scope of the lab/project. Once you start dealing with floats you have to worry about rounding, and lose the ability to use == to test equivalence due to machine delta, etc. Just stick with ints, and save yourself the headache.

    -Lee
     
  13. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    Even better, you can just type:
    man fmod

    at the terminal and get the handy man page.

    And I don't think not knowing every function in the standard libraries makes you a bad teacher. Not knowing how to find the information out, might, though.

    -Lee
     
  14. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #14
    Blaming the teacher...some student you are...:rolleyes:
     
  15. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #15
    I thought we were the teacher and that guy was just our on-site assistant!
     

Share This Page