Thread: Coin Problem Help

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    11

    Coin Problem Help

    Hello, I'm experiencing difficulties with the last portion of my program where the user is to enter an amount to be paid and the program is then required to display number of loonies required/balance owing, and then subsequently the number of quarters required for that balance as well as displaying the new balance.

    Example:
    Please enter the amount to be paid: $10.80
    Loonies required: 10, balance owing $0.80
    Quarters required: 3, balance owing $0.05

    I'm having trouble figuring out the logic for the quarters.

    My program (Please excuse the ugly layout of the source code.Will clean it up later):

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        int money;
        int loonies;
        int count = 0;
    
    
        double amountToBePaid = 0.0;
        printf("Please enter the amount to be paid: $");
        scanf("%lf", &amountToBePaid);
        money = (amountToBePaid/1);
    
    
        for (loonies = 0;loonies < money;loonies += 1)
        {
            count++;
        }
        double balanceOwed = (amountToBePaid-count);
        printf("Loonies required: %d, balance owing $%.2lf\n", count, balanceOwed);
    
    
        double quarters;
        int count2 = 0;
        //int money2 = (balanceOwed*100);
        for (quarters = 0.0;quarters < balanceOwed;quarters += 0.25)
        {
            if(quarters<balanceOwed)
            {
              count2++;
            }
            else if(quarters>balanceOwed)
            {
            (quarters-0.25);
            break;
            }
        }
    
    
        double balanceOwed2 = (balanceOwed-quarters);
        printf("Quarters required: %d, balance owing $%.2lf\n",count2,balanceOwed2);
        
        return 0;
    }
    If I was to input $10.80, my program would give me the following output:

    Loonies required: 10, balance owing $0.80
    Quarters required: 4, balance owing $-0.20

    It should instead display 3 quarters required, with a balance owing of $0.05

    How do I change the syntax of the quarter portion of my loop to display this?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    By "loonies" you apparently mean "dollars"! (I know. Canadian. )

    Your overall approach seems to be flawed.
    It's best to deal with the number of cents.
    So you might want to do:
    Code:
    int cents = amountToBePaid * 100 + 0.5;  // add 0.5 to round up
    Then note that you can determine the number of dollars by dividing by 100. Remember that integer division ignores any remainder. Also recall that the modulus operator (%) gives you just the remainder. So
    Code:
    int dollars = cents / 100;
    cents = cents % 100;
    will give you the number of dollars and will update cents to be the remaining amount to be paid.

    Then do the quarters (25), dimes (10), and nickels (5). After that, cents should contain the remaining number of cents (0 to 4).

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by algorism View Post
    By "loonies" you apparently mean "dollars"! (I know. Canadian. )

    Your overall approach seems to be flawed.
    It's best to deal with the number of cents.
    So you might want to do:
    Code:
    int cents = amountToBePaid * 100 + 0.5;  // add 0.5 to round up
    Then note that you can determine the number of dollars by dividing by 100. Remember that integer division ignores any remainder. Also recall that the modulus operator (%) gives you just the remainder. So
    Code:
    int dollars = cents / 100;
    cents = cents % 100;
    will give you the number of dollars and will update cents to be the remaining amount to be paid.

    Then do the quarters (25), dimes (10), and nickels (5). After that, cents should contain the remaining number of cents (0 to 4).
    Aha, completely forgot to mention that loonies are equivalent to a dollar USD

    Thanks for your help but just a quick question about the cents variable. If I use the modulus to get the remainder I will end up with an integer (50 cents). I then tried to declare a double variable and initialized it to "cents/100". double balanceOwed=(cents/100). I thought it would give me "0.50" but instead it gave me "0.00" for some reason

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The idea is to NOT use floating point variables at all except for the initial input which you immediately convert to an int by multiplying it by 100 (and adding .5 for rounding).

    And remember that integer division (i.e., cents / 100) always discards the remainder (or fractional part), even if you then assign it to a floating-point variable. To do what you were trying to do you would need to use floating-point division, which you get by making one of the operands to the division operator a floating-point value, e.g., cents / 100.0

    But I wouldn't use any floating point variables after the initial input. Just work with ints.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Thanks, that makes sense. Using ints is simpler but the reason I wanted to know how to convert the remaining cents into a floating-point value is because my professor wants the balance in that format.

    Updated code
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int loonies, money, cents;
        double amountToBePaid = 0.0; 
        float balanceOwed;
    
    
        printf("Please enter amount to be paid: $");
        scanf("%lf",&amountToBePaid);
        cents = amountToBePaid*100 + 0.5;
        loonies = cents/100;
        cents = cents%100;
        balanceOwed = cents/100;
    
    
        printf("Loonies Required: %d, balance Owing %f",loonies,balanceOwed);
        
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    That doesn't look right to me, but if you're happy with it....

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    11
    Yeah, I forgot to put the decimal point after the divisor in the arithmetic for the cents variable

    Everything works now. Thanks, you saved me 2%

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int loonies, quarters, dimes, nickels, pennies, cents;
        double amountToBePaid = 0.0; 
        float balanceOwed, balanceOwed2,balanceOwed3, balanceOwed4, balanceOwed5, GST, priceWithTax;
    
    
        printf("Please enter the amount to be paid: $");
        scanf("%lf",&amountToBePaid);
        GST = (amountToBePaid*0.13+.005);
        priceWithTax = (amountToBePaid+GST);
        printf("GST: %.2f\n",GST);
        printf("Balance owing: $%.2f\n",priceWithTax);
        cents = priceWithTax*100 + 0.5;
        loonies = cents/100;
        cents = cents%100;
        balanceOwed = cents/100.0;
        quarters = cents/25;
        cents = cents%25;
        balanceOwed2 = cents/100.0;
        dimes = cents/10;
        cents = cents%10;
        balanceOwed3 = cents/100.0;
        nickels = cents/5;
        cents = cents%5;
        balanceOwed4 = cents/100.0;
        pennies = cents/1;
        cents = cents%1;
        balanceOwed5 = cents/100.0;
    
    
    
    
         
    
    
        printf("Loonies required: %d, balance owing $%.2f\n",loonies,balanceOwed);
        printf("Quarters required: %d, balance owing $%.2f\n",quarters, balanceOwed2);
        printf("Dimes required: %d, balance owing $%.2f\n",dimes, balanceOwed3);
        printf("Nickels required: %d, balance owing $%.2f\n",nickels, balanceOwed4);
        printf("Pennies required: %d, balance owing $%.2f\n",pennies, balanceOwed5);
    
    
        return 0;
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually, I think since the numbers 1, 0.75, 0.5 and 0.25 are all numbers that can be represented exactly in IEEE-754 doubles, and because Canada abandoned the penny, you could probably get away with this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        double balance = 10.80;
        int loonies, quarters, dimes, nickels;
        
        printf("Breakdown $%.02f:\n", balance);
    
        loonies = (int)balance;
        if (loonies > 0)
            balance -= loonies;
        printf("Loonies owed: %d, balance owed: $%.02f\n", loonies, balance);
        
        quarters = (int) (balance / .25);
        if (quarters > 0)
            balance -= (quarters * .25);
        printf("Quarters owed: %d, balance owed: $%.02f\n", quarters, balance);
        
        dimes = (int) (balance / .10);
        if (dimes > 0)
            balance -= (dimes * .10);
        printf("Dimes owed: %d, balance owed: $%.02f\n", dimes, balance);
    
        nickels = (int) (balance / .05);
        if (nickels > 0)
            balance -= (nickels * .05);
        printf("Nickels owed: %d, balance owed: $%.02f\n", nickels, balance);
    
        return 0;
    }
    
    
    Breakdown $10.80:
    Loonies owed: 10, balance owed: $0.80
    Quarters owed: 3, balance owed: $0.05
    Dimes owed: 0, balance owed: $0.05
    Nickels owed: 1, balance owed: $0.00
    However, that is only without ANY penny adjustments. Rounding errors make doing math with floating points really scary. Only a handful of the numbers between 0/100 and 100/100 are exactly represented in a float format... your teacher is a bit evil,(?) stupid(? take your pick) to require it.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You could do it with quite a few less variables.
    Code:
    #include <stdio.h>
     
    int main(void) {
        int cents, num;
        double amount, GST;
    
        printf("Please enter the amount to be paid: $");
        scanf("%lf", &amount);
    
        GST = amount * 0.13 + .005;
        amount += GST;
    
        printf("GST: %.2f\n", GST);
        printf("Balance owing: $%.2f\n", amount);
    
        cents = amount * 100 + 0.5;
    
        num = cents / 100;
        cents = cents % 100;
        printf("Loonies required: %d, balance owing $%.2f\n",
               num, cents / 100.0);
    
        num = cents / 25;
        cents = cents % 25;
        printf("Quarters required: %d, balance owing $%.2f\n",
               num, cents / 100.0);
    
        num = cents / 10;
        cents = cents % 10;
        printf("Dimes required: %d, balance owing $%.2f\n",
               num, cents / 100.0);
    
        num = cents / 5;
        cents = cents % 5;
        printf("Nickels required: %d, balance owing $%.2f\n",
               num, cents / 100.0);
    
        printf("Pennies required: %d, balance owing $0.00\n",
               cents);
    
        return 0;
    }
    Noticing how repetitive it is leads to another possibility:
    Code:
    #include <stdio.h>
    
    struct {
        const char *name;
        int value;
    } coins[] = {
        { "Loonies", 100 },
        { "Quarters", 25 },
        { "Dimes",    10 },
        { "Nickels",   5 },
        { "Pennies",   1 },
        { NULL,        0 }  // indicates end of list
    };
    
    int main(void) {
        printf("Please enter the amount to be paid: $");
        double amount;
        scanf("%lf", &amount);
    
        double GST = amount * 0.13 + .005;
        printf("GST: %.2f\n", GST);
    
        amount += GST;
        printf("Balance owing: $%.2f\n", amount);
    
        int cents = amount * 100 + 0.5;
    
        for (size_t i = 0; coins[i].name; i++) {
            int num = cents / coins[i].value;
            cents = cents % coins[i].value;
            printf("%s required: %d, balance owing $%.2f\n",
                   coins[i].name, num, cents / 100.0);
        }
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP! Coin Counter problem in C
    By Fawei Zhang in forum C Programming
    Replies: 3
    Last Post: 05-02-2015, 10:26 PM
  2. Help coin program.
    By Kinto in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2009, 08:00 PM
  3. coin toss
    By ejd81882 in forum C Programming
    Replies: 3
    Last Post: 10-14-2008, 12:53 PM
  4. coin problem
    By et1wilson in forum C Programming
    Replies: 5
    Last Post: 07-14-2008, 05:19 AM
  5. Coin counting (logic problem)
    By xlokix in forum C++ Programming
    Replies: 47
    Last Post: 01-20-2005, 03:22 PM

Tags for this Thread