0

Can someone tell me where I went wrong. My code works fines for some cases and incorrectly for other cases.

```

include

include

include

int main(void) { float change; float quarters = 0.25; float dimes = 0.10; float nickels = 0.05; float pennies = 0.01; int coins_used = 0;

do {
    printf("How much change do I owe you?\n");
    change = GetFloat();
}while (change < 0.00);

while (change > 0.00) {
    if (change >= quarters) {
        coins_used += (int) (change / quarters);
        change -= (coins_used * quarters);
        printf("the number of quarters used is %d\n", coins_used);
    }else if (change < quarters || change >= dimes) {
        coins_used += (int) (change / dimes);
        change -= (coins_used * dimes);
        printf("the number of dimes used is %d\n", coins_used);
    }else if (change < dimes || change >= nickels) {
        coins_used += (int) (change / nickels);
        change -= (coins_used * nickels);
        printf("the number of nickels used is %d\n", coins_used);
    }else {
        coins_used += (int) (change / pennies);
        change -= (coins_used * pennies);
        printf("the number of pennies used is %d\n", coins_used);
    }
}
printf("coins used: %d\n", coins_used);

} ```

1
  • Simply put, you've missed most of the point of this exercise - storage inaccuracies of floats and dealing with them. You need to go back and study the instructions for this pset and the walkthrough video that comes with it. There is just too much wrong to address here.
    – Cliff B
    Jul 17, 2016 at 8:30

1 Answer 1

1

So you need to understand the following:

Floats have an inherent imprecision to them. Integers don't. The reason for this is because floats store values after the decimal places. Things like 1/3 becomes 0.333333333333.

Floats aren't inherently easy to do with binary math. Instead of the traditional, 1,2,4,8 16,32,64,128 bits with integers, floats also consider values of 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, using bits to calculate those values. Integers only have 4 bytes, or 32 bits by default. Floats are also by default 32 bits.

My point is, float math is always wonky. If you wanted to specify what 1/3rd is, the computer has to obtain a value of 0.3333333333333333333 by adding 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + ............. You don't get an exact value after so many decimal places, because you have limited memory to do so


So don't use float math for its' inherent inaccuracies

To answer your question, in CS50's videos, it specifically said to do all math by integers, and use a factor of 100 and the round function to do so.

First, your

do { printf("How much change do I owe you?\n"); change = GetFloat();

is correct, except that doesn't exclude values of things like 5.133 which is okay though for this scenario.

You need to then convert this to an integer value. You'll want to store this value

Say for instance, you input 5.13 as your "change" variable. But that could be 5.13000000000000003513515 for all you know (since its stored as a float, see the 1/2+1/4+1/8+1/16.... example above for its inherent inaccuracies). You need to convert this to an integer.

So you multiply "change" by 100, and store it as another new variable, call this "n". Now you've essentially removed all decimal places needed since pennies are stored in the hundreth places. $5.13 in float notation is 5.1300005 is now 513.00005

Take "n", and then use the round() function on it. This removes out the last digits after the decimal, now you have 513.

For all your math, you will now want to work with values like 25 for quarters, 10 for dimes, 5 for nickels, as opposed to say 0.25 for quarters. Because you are now purely working with integers, and not floating points / things with decimals.


Now that you've got all that setup (correctly converting a float value input of say 5.13 to integer notation of 513, and doing all math afterward that way). You need a way such that you can deduct that 513 using the largest denominator possible first (quarters , value of 25) with a way of counting it. Then dimes, nickels, pennies, etc.

There are two ways to implement this greedy algorithm: (see here https://i.stack.imgur.com/43NQ9.png )

  1. Using a WHILE LOOP (for each denomination) and then subtracting that 513 and adding a counter (to show how many coins you have).

  2. Using a Modulo operator and some division (see here https://i.stack.imgur.com/YOZxO.png) . Here's an example of what sort of things you'd do: https://i.stack.imgur.com/qcwXI.png . You'd take 513, divide it by 25 to get a value of 20. Store that value. Then, do 513%25 (which is a value of 13 , the remainder) and use it in the next calculation of dimes. Repeat for nickels and pennies and add it to "coins_used"


on this part

while (change > 0.00) {
if (change >= quarters) {
    coins_used += (int) (change / quarters);
    change -= (coins_used * quarters);
    printf("the number of quarters used is %d\n", coins_used);
}else if (change < quarters || change >= dimes) {
    coins_used += (int) (change / dimes);
    change -= (coins_used * dimes);
    printf("the number of dimes used is %d\n", coins_used);
}else if (change < dimes || change >= nickels) {
    coins_used += (int) (change / nickels);
    change -= (coins_used * nickels);
    printf("the number of nickels used is %d\n", coins_used);
}else {
    coins_used += (int) (change / pennies);
    change -= (coins_used * pennies);
    printf("the number of pennies used is %d\n", coins_used);
}

}

I have a fairly difficult time reading this notation. Again don't use float math, all math needs to be done in integers.

2
  • Thank you for your help. I really appreciate it. Jul 25, 2016 at 16:35
  • No problem. Also another thing to consider, if you had 1/3rd stored as a float, it would be imprecise. In the class example, they said 0.1 is also imprecise (the space shuttle explosion example). However, 0.125 is a precise float, or should be, because you can get that value from 0*1 + 0*0.5 + 0*0.25+ 1*0.125. Hope that makes sense. I'll have to run it in a program just to be sure Jul 27, 2016 at 2:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .