# Cash

## Problem to Solve

Suppose you work at a store and a customer gives you $1.00 (100 cents) for candy that costs $0.50 (50 cents). Youâ€™ll need to pay them their â€śchange,â€ť the amount leftover after paying for the cost of the candy. When making change, odds are you want to minimize the number of coins youâ€™re dispensing for each customer, lest you run out (or annoy the customer!). In a file called `cash.c`

in a folder called `cash`

, implement a program in C that prints the minimum coins needed to make the given amount of change, in cents, as in the below:

```
Change owed: 25
1
```

But prompt the user for an `int`

greater than 0, so that the program works for any amount of change:

```
Change owed: 70
4
```

Re-prompt the user, again and again as needed, if their input is not greater than or equal to 0 (or if their input isnâ€™t an `int`

at all!).

## Demo

## Greedy Algorithms

Fortunately, computer science has given cashiers everywhere ways to minimize numbers of coins due: greedy algorithms.

According to the National Institute of Standards and Technology (NIST), a greedy algorithm is one â€śthat always takes the best immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems.â€ť

Whatâ€™s all that mean? Well, suppose that a cashier owes a customer some change and in that cashierâ€™s drawer are quarters (25Â˘), dimes (10Â˘), nickels (5Â˘), and pennies (1Â˘). The problem to be solved is to decide which coins and how many of each to hand to the customer. Think of a â€śgreedyâ€ť cashier as one who wants to take the biggest bite out of this problem as possible with each coin they take out of the drawer. For instance, if some customer is owed 41Â˘, the biggest first (i.e., best immediate, or local) bite that can be taken is 25Â˘. (That bite is â€śbestâ€ť inasmuch as it gets us closer to 0Â˘ faster than any other coin would.) Note that a bite of this size would whittle what was a 41Â˘ problem down to a 16Â˘ problem, since 41 - 25 = 16. That is, the remainder is a similar but smaller problem. Needless to say, another 25Â˘ bite would be too big (assuming the cashier prefers not to lose money), and so our greedy cashier would move on to a bite of size 10Â˘, leaving him or her with a 6Â˘ problem. At that point, greed calls for one 5Â˘ bite followed by one 1Â˘ bite, at which point the problem is solved. The customer receives one quarter, one dime, one nickel, and one penny: four coins in total.

It turns out that this greedy approach (i.e., algorithm) is not only locally optimal but also globally so for Americaâ€™s currency (and also the European Unionâ€™s). That is, so long as a cashier has enough of each coin, this largest-to-smallest approach will yield the fewest coins possible. How few? Well, you tell us!

## Advice

Click the below toggles to read some advice!

## Write some code that you know will compile

Even though this program wonâ€™t do anything, it should at least compile with `make`

!

```
#include <cs50.h>
#include <stdio.h>
int main(void)
{
}
```

Notice that youâ€™ve now included `cs50.h`

and `stdio.h`

, two â€śheader filesâ€ť that will give you access to functions that might help you solve this problem.

## Write some pseudocode before writing more code

If unsure how to solve the problem itself, break it down into smaller problems that you can probably solve first. For instance, this problem is really only a handful of problems:

- Prompt the user for change owed, in cents.
- Calculate how many
*quarters*you should give customer. Subtract the value of those quarters from cents. - Calculate how many
*dimes*you should give customer. Subtract the value of those dimes from remaining cents. - Calculate how many
*nickels*you should give customer. Subtract the value of those nickels from remaining cents. - Calculate how many
*pennies*you should give customer. Subtract the value of those pennies from remaining cents. - Sum the number of quarters, dimes, nickels, and pennies used.
- Print that sum.

This is the greedy algorithm you can use to solve this problem, so letâ€™s write some pseudcode as comments to remind you to do just that:

```
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt the user for change owed, in cents
// Calculate how many quarters you should give customer
// Subtract the value of those quarters from cents
// Calculate how many dimes you should give customer
// Subtract the value of those dimes from remaining cents
// Calculate how many nickels you should give customer
// Subtract the value of those nickels from remaining cents
// Calculate how many pennies you should give customer
// Subtract the value of those pennies from remaining cents
// Sum the number of quarters, dimes, nickels, and pennies used
// Print that sum
}
```

## Convert the pseudocode to code

First, consider how you might prompt the user for the cents they are owed. Recall that a `do while`

loop is helpful when you want to do something at least once, and possibly again and again, as in the below:

```
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt the user for change owed, in cents
int cents;
do
{
cents = get_int("Change owed: ");
}
while (cents < 0);
}
```

Itâ€™s wise to stop here and `make`

your program. Test to be sure your program compiles, and that it reprompts you if you enter less than 0 cents (or if you enter an input like â€ścatâ€ť).

Next, consider how to calculate how many quarters you should give the customer. Since weâ€™re using a greedy algorithm, this question becomes â€śwhatâ€™s the *greatest* number of quarters could you give them?â€ť. You *could* write a solution to this problem in your `main`

function. But, it might clear up your thinking to write a new function: one called `calculate_quarters`

. That way you can better focus on the logic to calculate quarters. Later, you can integrate this function into your larger solution.

```
int calculate_quarters(int cents)
{
// Calculate how many quarters you should give customer
}
```

Notice that this function is indeed named `calculate_quarters`

. Per `int cents`

in parentheses, it takes an `int`

called `cents`

as input. And, per the `int`

in front of its name, it should also â€śreturnâ€ť an `int`

. That is, the output of this function is an integer: the number of quarters that fit into cents. If curious about this idea, recall there are several sample programs in Week 1â€™s Source Code that illustrate how functions can return a value.

Now consider this way of implementing `calculate_quarters`

by adding to the number of quarters until weâ€™ve run out of cents to convert to quarters:

```
int calculate_quarters(int cents)
{
// Calculate how many quarters you should give customer
int quarters = 0;
while (cents >= 25)
{
quarters++;
cents = cents - 25;
}
return quarters;
}
```

Granted, there is at least one simpler way to solve this `calculate_quarters`

problem. But weâ€™ll leave that up to you to figure out!

With `calculate_quarters`

functioning as intended, you can integrate this function into your program. Take care to â€śdeclareâ€ť the functionâ€™s â€śsignatureâ€ť (i.e., `int calculate_quarters(int cents)`

) above your `main`

function, so you can indeed use `calculate_quarters`

there while defining it later, below `main`

.

```
#include <cs50.h>
#include <stdio.h>
int calculate_quarters(int cents);
int main(void)
{
// Prompt the user for change owed, in cents
int cents;
do
{
cents = get_int("Change owed: ");
}
while (cents < 0);
// Calculate how many quarters you should give customer
int quarters = calculate_quarters(cents);
// Subtract the value of those quarters from cents
cents = cents - (quarters * 25);
}
int calculate_quarters(int cents)
{
// Calculate how many quarters you should give customer
int quarters = 0;
while (cents >= 25)
{
quarters++;
cents = cents - 25;
}
return quarters;
}
```

A few problems down, and a few more to go! Notice a pattern you could re-use here?

## How to Test

For this program, try testing your code manually. Itâ€™s good practice:

- If you input
`-1`

, does your program prompt you again? - If you input
`0`

, does your program output`0`

? - If you input
`1`

, does your program output`1`

(i.e., one penny)? - If you input
`4`

, does your program output`4`

(i.e., four pennies)? - If you input
`5`

, does your program output`1`

(i.e., one nickel)? - If you input
`24`

, does your program output`6`

(i.e., two dimes and four pennies)? - If you input
`25`

, does your program output`1`

(i.e., one quarter)? - If you input
`26`

, does your program output`2`

(i.e., one quarter and one penny)? - If you input
`99`

, does your program output`9`

(i.e., three quarters, two dimes, and four pennies)?

### Correctness

In your terminal, execute the below to check your workâ€™s correctness.

```
check50 cs50/problems/2024/x/cash
```

### Style

Execute the below to evaluate the style of your code using `style50`

.

```
style50 cash.c
```

## How to Submit

In your terminal, execute the below to submit your work.

```
submit50 cs50/problems/2024/x/cash
```