Thread: Exercise 3 --- Correct Change

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    42

    Exercise 3 --- Correct Change

    When cashiers in a store give you change they try first try to "fit" dollars into the amount you get back, then try to "fit" quarters (25 cent coins) into what is left over, they try to "fit" dimes (10 cent coins) into what is now left over, then try to "fit" nickels (5 cent coins) into what is left, and finally are left with a few odd cents. For example, say that your change is 163 cents:
    One dollar fits into 163, leaving 63 cents.
    Two quarters fit into 63 cents, leaving 13 cents.
    One dime fits into 13 cents, leaving 3 cents.
    No nickels are needed.
    Three cents are left.Your change is : 1 dollar, two quarters, one dime, and three cents.

    Write a program that reads change due to a user (in cents) and writes out how many dollars, quarters, dimes, nickels, and pennies she is due. If you are stuck, it will help to do an example problem with paper and pencil.

    http://stackoverflow.com/questions/33250065/convert-cents-to-quarters-nickels-dimes-and-pennies-using-python

    I took the codes from here and modified it slightly, but the program crashes whenever I try to run it. What's wrong?

    Code:
    #include<stdio.h>
    
    int main() // Convert some money to an appropriate collection of cents
    {
        int cent = 1;      
        int nickel = 5;    // Each nickel consists of 5 cents.
        int dime = 10;     // Each dime consists of 10 cents.     
        int quarter = 25;     // Each quarter consists of 25 cents.
        int dollar = 100;    // Each dollar consists of 100 cents.
    
    
        int dollars = 0;
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;
        int cents = 0;
    
    
        printf("Please enter an amount of money you have in cents: ");
        scanf("%d",&cents); //Total cents input by the user 
        
        do{    
            if (cents >= 100)
            {
                dollars = cents / dollars;    // How many dollars we can get?
                cents = cents % dollars;
            }
            else if (cents >= 25)
            {
                quarters = cents / quarter;    // How many quarters we can get?
                cents = cents % quarter;
            }
            else if (cents >= 10)
            {
                dimes = cents /dime;    // How many dimes we can get?
                cents = cents % dime;
            }
            else if (cents >= 5)
            {
                nickels = cents /nickel;    // How many nickels we can get?
                cents = cents % nickel;
            }
            else
            {    
                cents = cent;    // How many cents we'd have left?
            }
        }while (cents != 0);
        
        printf("Your change is : %d dollar, %d quarters, %d dime, %d nickels, and %d cents.\n", dollars, quarters, dimes, nickels, cents);
     
      return 0;
    
    }


  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Stop taking code from other places and trying to modifying it. Come up with your own code. If you don't understand how to do it then it's pretty certain that you do not have the ability (yet) to take some random code and make it work how you want it to; i.e. if you understood what was going on enough to modify it correctly than you would not have to "google it" it the first place! Please. I am not being intentionally nasty, but until you learn to work through problems yourself you will not progress. You won't learn anything. You will be forever be googling stuff and copy/pasting/adapting code that you don't understand because you have not put in the effort to try and solve the problem yourself. Use references, sure!, but don't just take rubbish you don't understand, modify the syntax and make some random adjustments and expect it to work. Semantics is more important than syntax.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    dollars = cents / dollars;
    You use "dollars" as the variable to store the amount of dollars, whereas you use "dollar" to store the value of 100 (cents).

    You want to use "dollar" here instead, otherwise you are dividing by zero the first time around (with an input >= 100).

    This confusion was due to similarly named variables. Perhaps you should make a more distinctive naming scheme to differentiate between the variables and the "constant" values. On that last point, you should use named constants in lieu of variables for the values that are to remain constant.

    There is an additional logic problem, but I'll leave it to you to sort out.

    May I ask, if you're learning C, why you are trying to modify existing code? Figuring out logic like this on your own is essential for building the problem-solving skills required to make useful programs.

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    Noted Hodor. I will. Thanks. I understand how to convert the money into dollars and cents, but after that I have no idea how to break down the cents further into quarters, nickels, etc. That is my main problem.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You don't need a loop.
    Get rid of all the "else"'s.
    cents = cent; is senseless. Get rid of that.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nadeera View Post
    Noted Hodor. I will. Thanks. I understand how to convert the money into dollars and cents, but after that I have no idea how to break down the cents further into quarters, nickels, etc. That is my main problem.
    That is why you need to figure out how to do it yourself, rather than look for existing solutions. These exercises are intended to prepare you for solving unique problems on your own, when no specific solutions are available.

    Spending some time with a pencil and paper is very important in solving logic problems like this.

    Start with, for example, 98 cents. Figure out how many quarters can fit into this amount. Figure out how "taking out" these quarters will effect the total. Then do the same for dimes, nickels, and so on.

  7. #7
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    This is what I understand.

    1. Total cents divide by 100 to get dollars.
    2. The remainder is the cents.
    3. The cents should be divided by 25 to get quarters.
    4. After quarters is removed, the remainder is divided by 10 to get dimes.
    5. After dimes is removed, the remainder is then divided by 5 to get nickels.
    6. What is left is the few odd cents.

    98= (25*3)+(10*2)+(1*3)

    What I don't know is how to convert it into codes? When programmers say logic, what do they mean? Is it the if...else statement, for loop, etc? I have problems converting my thoughts into flowcharts and pseudocodes.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Okay, I am going to put a lot of time and effort into this post, so I hope you read it carefully and try to follow the process.

    You started to break the problem down, but you should break it down further. Remember, computers need explicit instructions. Therefore, you should try to describe each and every little step separately. This makes converting the steps into code much easier.



    Step 1: Break the problem down into a discrete list of steps.

    Let's assume we start with an amount of $2.83

    First, we convert this amount to cents: 283

    Does this amount contain dollars?
    If the amount is greater than or equal to a dollar, then yes.
    To get the dollars, divide the amount by 100 (dollars = 283 / 100 = 2)
    Now reduce the amount by the number of dollars we took out
    amount = amount - 100 * dollars
    amount = 283 - (100 * 2)
    amount = 283 - 200
    amount = 83


    That is the basic logic we need to follow, with each little step described. As you can see, even broken down this way, there is not a whole lot to it.

    Fortunately, the rest of the code follows the same pattern. Let's continue the process.

    Does this amount contain quarters?
    If the amount is greater than or equal to a quarter, then yes.
    To get the quarters, divide the amount by 25 (quarters = 83 / 25 = 3)
    Now reduce the amount by the number of quarters we took out
    amount = amount - 25 * quarters
    amount = 83 - (25 * 3)
    amount = 83 - 75
    amount = 8


    Does this amount contain dimes?
    If the amount is greater than or equal to a dime, then yes.
    8 is not >= 10, so the number of dimes = 0.


    Does this amount contain nickels?
    If the amount is greater than or equal to a nickel, then yes.
    To get the nickels, divide the amount by 5 (nickels = 8 / 5 = 1)
    Now reduce the amount by the number of nickels we took out
    amount = amount - 5 * nickels
    amount = 8 - (5 * 1)
    amount = 8 - 5
    amount = 3


    Whatever is left over must be the pennies.

    So we know that $2.83 contains: (2) dollars, (3) quarters, (0) dimes, (1) nickel, and (3) pennies.



    Step 2: Convert these steps into code.

    Now that we have the entire problem broken down in words, converting it to code is pretty straight-forward.

    Let's start with the "dollars" section.

    Does this amount contain dollars?
    If the amount is greater than or equal to a dollar, then yes.


    Code:
    if(amount >= 100)
    To get the dollars, divide the amount by 100 (dollars = 283 / 100 = 2)

    Code:
    dollars = amount / 100;
    Now reduce the amount by the number of dollars we took out
    amount = amount - 100 * dollars
    amount = 283 - (100 * 2)
    amount = 283 - 200
    amount = 83


    Code:
    amount = amount - (100 * dollars);
    Do you see how breaking the problem down into each little step helps us easily convert the algorithm into code?

    I will stop at dollars, since the rest is the same idea.

    Notice how I opted for subtraction and multiplication when re-adjusting the value of "amount", instead of using the modulus (%) as in the original example you posted. I did it this way because, as a beginner, it might be easier for you to see the logic expressed that way. It solves the problem clearly, so it is sufficient. When you get more experience, you will be able to find easier ways to do things - but for now, it would help to see things spelled out like that.

    Anyway, I hope this was helpful for you. Let me know if you have any questions on any of this.

  9. #9
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    Thank you so much for this post. I finally got my enlightenment! It was definitely very helpful because I got to see the process of writing codes. I have a question, though. I am assuming that we are going to be using the if...else statement. I always thought that at each block of the if... else statement, the amount is going to be back at 283 cents. So that's not true?

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nadeera View Post
    Thank you so much for this post. I finally got my enlightenment!
    I'm very glad it helped!

    Quote Originally Posted by nadeera View Post
    I have a question, though. I am assuming that we are going to be using the if...else statement.
    In this case, we don't want to use "else", just a chain of "if"s. This is because, even if a previous "if" was true, we still want to check the value again at the next "if" as well. We want to check the value each step of the way.

    Quote Originally Posted by nadeera View Post
    I always thought that at each block of the if... else statement, the amount is going to be back at 283 cents. So that's not true?
    No - notice how we explicitly subtracted out the amount we "took out" before proceeding to the next test (i.e. "...reduce the amount by the number of dollars we took out").

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    By the way this assignment can be done without any if statements at all, if you can print a zero quantity for any denomination.

    Jim

  12. #12
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    I tried writing the codes.

    Code:
    #include<stdio.h>
    
    main()
    {
        int dollars = 0;
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;
        int pennies = 0;
        int amount;
    
    
        printf("Enter the amount in cents:");
        scanf("%d", &amount);
    
    
        if(amount >= 100)
            dollars = amount / 100;
            amount = amount - (100 * dollars);
        if(amount >= 25)
            quarters = amount / 25;
            amount = amount - (25 * quarters);
        if(amount >= 10)
            dimes = amount / 10;
            amount = amount - (10 * dimes);
        if(amount >= 5)
            nickels = amount / 5;
            amount = amount - (5 * nickels);
        if(amount >= 1)
            pennies = amount / 1;
            amount = amount - (1 * pennies);
    
    
        printf("Your change is : %d dollars, %d quarters, %d dimes, %d nickels, and %d pennies.\n", dollars, quarters, dimes, nickels, pennies);
      
    
    
    }
    Would it be okay to write this part of the code this way as well?

    Code:
    if(amount >= 1)            
            pennies = amount;
            amount = amount - pennies;
    if you can print a zero quantity for any denomination.
    What do you mean, Jim?

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if(amount >= 100)
        dollars = amount / 100;
        amount = amount - (100 * dollars);
    Your indentation is misleading. If you have multiple statements under the "if()", they should be enclosed in braces.

    Code:
    if(amount >= 100)
    {
        dollars = amount / 100;
        amount = amount - (100 * dollars);
    }


    Code:
    if(amount >= 1)
        pennies = amount / 1;
        amount = amount - (1 * pennies);
    This part isn't strictly necessary. After you're done subtracting out any nickels, whatever is left is just "pennies".

    Code:
    pennies = amount;


    One suggestion for improvement - you should consider using named constants in lieu of magic numbers:

    Code:
    #define DOLLAR  100
    #define QUARTER  25
    #define DIME     10
    #define NICKEL    5
    
    // ...
    
    if(amount >= DOLLAR)
    {
        dollars = amount / DOLLAR;
        amount = amount - (100 * dollars);
    }
    Looks good!

  14. #14
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    Improvised version.

    Code:
    #include<stdio.h>
    #define DOLLAR  100
    #define QUARTER  25
    #define DIME     10
    #define NICKEL    5
     
    main()
    {
    	int dollars = 0;
    	int quarters = 0;
    	int dimes = 0;
    	int nickels = 0;
    	int pennies = 0;
    	int amount;
    
    
    	printf("Enter the amount in cents:");
    	scanf("%d", &amount);
    
    
    	if(amount >= 100)
    	{
    		dollars = amount / DOLLAR;
    		amount = amount - (100 * dollars);
    	}
    	if(amount >= 25)
    	{
    		quarters = amount / QUARTER;
    		amount = amount - (25 * quarters);
    	}
    	if(amount >= 10)
    	{
    		dimes = amount / DIME;
    		amount = amount - (10 * dimes);
    	}
    	if(amount >= 5)
    	{
    		nickels = amount / NICKEL;
    		amount = amount - (5 * nickels);
    	}
    	if(amount >= 1)
    	{
    		pennies = amount;
    	}
    
    
    	printf("Your change is : %d dollars, %d quarters, %d dimes, %d nickels, and %d pennies.\n", dollars, quarters, dimes, nickels, pennies);
    
    
    }
    Thanks, Matticus. Would you mind if I attempted a different exercise and posted the logic first before I write the code?

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That would be fine. You should start a new thread if it's for a different exercise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-28-2015, 10:53 AM
  2. Change Calculator with Breakdown of the Change
    By insertcoin in forum C Programming
    Replies: 2
    Last Post: 04-16-2014, 08:25 AM
  3. little help with a C exercise
    By Talon320 in forum C Programming
    Replies: 29
    Last Post: 09-26-2013, 11:01 PM
  4. Replies: 2
    Last Post: 02-16-2012, 03:03 PM
  5. Help exercise!
    By sanhthai in forum C++ Programming
    Replies: 10
    Last Post: 09-22-2009, 12:07 PM