We will continue working through the CS50 Problem Set from last week where we were learning to code in the C programming language. In this post, I will go over my solution to the cash problem in Problem Set 1. The problem presented here, is that we want to know how many coins a person will receive from the cashier given an amount. We also need to account for the fact that the cashier will want to produce the least amount of coins possible. For instance, if the cashier is given $4.00 on a bill of $3.42, the cashier will want to give the customer back 2 quarters, 1 nickel , and 3 pennies. So the output from our code should be 6.

The first thing that I wanted to do in this problem was create some variables for all of the coins that our cashier will be giving back to our customer. Secondly, I created a variable changeOutput that will act as the counter for the number of coins. Like the last solutions, we want to prompt the user while the input is invalid. This way, we will make sure the user inputs what we want. In this case, a positive floating point number.

The next thing I want to do is change the cents from a float to an integer to make the arithmetic a lot easier. We have to be careful here because when we convert from a float to an integer we risk loosing some cents because of a rounding issue. Therefore I multiply the input by 100 before I round and convert to an integer. This technique allows us to avoid any rounding issues.

Next, I created for loops for each of the coin variables I made earlier to count how many of each I will need. Notice that the order here is important. If, for instance, I put dimes before quarters I would get more dimes than I need and no quarters. Each of these for loops operates in a similar fashion. While the cents variable is greater than or equal to the coin in question, it will subtract that value from the cents variable and add to our counter. Notice, you could also use modular arithmetic to solve this problem as well, though it may be a little more tricky.

Finally, after we have counted all of the coins out, our final step is to print the output to the user. I hope this helps you find your own solution to the problem. Keep plugging away at the course work and keep learning. If you have any questions about the material I have discussed, or you just want to connect, click on the Contact tab. If the content helped you, please return the favor by posting on your favorite social media site by simply clicking one of the buttons below. As always…Happy Programming.

Hi, I´m new on programming and I would´ve never found out that:

1. i needed to include a new math.h library

2. Needed to use those -= or += (I don´t really know what those mean)

Can you give me a hint on how to solve that problem without already knowing how to program? (after all it is the scope of the activity isn´t it?)

LikeLike

So the C programming languages doesn’t include a lot of functionality out of the box like most modern day languages such as Python or Java. The math library is just adding math functions for us to use. The += and -= is shorthand to increment or decrement a variable. For instance you could say x = x + 1, which would increment x by 1. So, x = x + 1 and x += 1 does the same thing. The best way, I think, to tackle these problems is to first write out pseudo code. Think of the things you are going to need your code to accomplish. In doing so you can break it down into more manageable sections. Then if you need to do one section, say increment a variable, and you can’t figure it out look it up on google and then move on to the next. Learning to program is more about learning how to solve problems, the code can always be looked up.

LikeLike

You can also do this exercise without including this library! Although it may not be all right since I had an error in the exercise and still do not know what happened. When I enter 0.42 it gives me that they are 22 currencies and in fact it would have to be 18. If anyone knows something I thank you

I leave my exercise code:

(I’m also new to programming and maybe there are ways to make code more efficient)

#include

#include

int main (void)

{

int changeInCents;

int coins = 0;

float changeInDollars;

do

{

changeInDollars = get_float (“Change owed: “);

}

while (changeInDollars = 25)

{

changeInCents = (changeInCents – 25);

coins += 1;

}

while (changeInCents >= 10)

{

changeInCents = (changeInCents – 10);

coins += 1;

}

while (changeInCents >= 5)

{

changeInCents = (changeInCents – 5);

coins += 1;

}

while (changeInCents >= 1)

{

changeInCents = (changeInCents – 1);

coins += 1;

}

printf(“%i\n”, coins);

}

LikeLike

Just wondering whats the purpose of the ints quarters dimes nickels pennies, I’m also fairly new to code.

LikeLike

These are just variables to keep track of the quantity of each coin I will need to give back to the customer.

LikeLike

Thanks Jason, I was hardly trying to avoid seeking help in Google but I had to because I’ve got stuck with this problem. I will try to use Modulo approach as well.

LikeLike

Good luck! Let us know how you go.

LikeLike

I have 1 question regarding “int changeOutput = 0;”…is it important to assign zero for this variable? I tried not to asign and nothing changed.

LikeLike

You don’t have to explicitly set it equal to zero. I did so just for clarity, but if you don’t initialize a value it is set to zero. Thanks

LikeLike

Hi Jason, where’d you get the value of “%i\n” from? I see nowhere in the code lines the variable for them? does the % here represents modulo? Thanks a lot!

LikeLike

Yes, the % represents the use of modulo arithmetic.

LikeLike

I like your solution. Going though CS50 now and even though I figured it out I like to look at other ways of doing it.

I stuck with some simple reduction math as I still get a bit mixed up running multiple for loops in a row.

#include

#include

#include

int main(void)

{

float change;

int quarters, dimes, nickles, pennies, coins;

// Prompt user for change due

do

{

change = get_float(“Change owed: “);

}

while (change <= 0);

// Convert change to rounded int

int cents = round(change * 100);

// Calculate number of coins reducing change left each time

quarters = (cents / 25);

cents = (cents – (quarters * 25));

dimes = (cents / 10);

cents = (cents – (dimes * 10));

nickles = (cents / 5);

cents = (cents – (nickles * 5));

pennies = (cents);

// Output number of coins

printf("Coins: %i\n", quarters + dimes + nickles + pennies);

return 0;

}

LikeLike

Nice. There is always many ways to solve these problems. Good job

LikeLike

not sure why the libraries didn’t paste in but…

#include

#include

#include

LikeLike