-1

This is the output of my tests:

:) greedy exists

:) greedy compiles

:( input of 0.41 yields output of 4
    expected "4\n", not "3\n"

:( input of 0.01 yields output of 1
    expected "1\n", not "0\n"

:) input of 0.15 yields output of 2

:) input of 1.6 yields output of 7

:) input of 23 yields output of 92

:) input of 4.2 yields output of 18

:) rejects a negative input like -.1

:) rejects a non-numeric input of "foo"

:) rejects a non-numeric input of ""

This is the code:

#include <stdio.h>
#include <cs50.h>

void count_coins();

int coin = 0;
float change = 0;

int main(void)
{
    do
    {
    change = get_float("How much change is owed? ");
    }
    while (change < 0);

    count_coins();

    printf("%i\n", coin);
}

void count_coins()
{
    while (change > 0.24)
    {
        coin++;
        change -= 0.25;
        // printf("%.2f\n", change);
    }

    while (change > 0.09)
    {
        coin++;
        change -= 0.10;
        // printf("%.2f\n", change);
    }

    while(change > 0.04)
    {
         coin++;
         change -= 0.05;
         // printf("%.2f\n", change);
    }

    while (change >= 0.01)
    {
        coin++;
        change -= 0.01;
        // printf("%.2f\n", change);
    }
}
lilezek
  • 6,976
  • 1
  • 27
  • 45
  • 1
    What's supposed to happen? What debugging steps have you taken? – Dave Newton Sep 25 '17 at 18:35
  • 3
    Maybe you are assuming `float` can hold the exact numbers you try to store in it, it cannot. Numbers may get approximated here and there to fit the specification of how `float` works. The kind of comparisons you are doing are most likely going to cause weird behavior. You should work with integer numbers there, use `100` to represent `1.00` etc. – Havenard Sep 25 '17 at 18:37
  • @DaveNewton The program is suppose to count the minimum amount of coins that need to be given as change. I noticed the problem is when we reach while (change > 0.01) – Kaue Pacheco Sep 25 '17 at 18:38
  • @Havenard What are other comparisons I can do to work effectively on floats? – Kaue Pacheco Sep 25 '17 at 18:39
  • 3
    I don't think you can rely on floats for this kind of operation at all, just use integers instead. – Havenard Sep 25 '17 at 18:40
  • 3
    Answer: don't work with `float`s. Please see [Is floating point math broken](http://stackoverflow.com/questions/588004/is-floating-point-math-broken)? There are many "change" questions here, and they usually recommend not to use floating point but integer variables. The *exact* decimal value `0.01` cannot be exactly represented by a `float`. FWIW the fraction `1/7` cannot be exactly represented by either system. – Weather Vane Sep 25 '17 at 18:40
  • 2
    `long cents = round(change * 100);` this should give you a solid value to work with. Also, you don't need loops to count the coins you can just calculate the whole thing... – Havenard Sep 25 '17 at 18:42
  • @Havenard can you throw a pseudo-code for me please? I really cant see this without using loop. – Kaue Pacheco Sep 25 '17 at 18:46
  • 1
    `coin = 0; coin += cents / 25; cents %= 25; coin += cents / 10; cents %= 10; coin += cents / 5 + cents % 5;` – Havenard Sep 25 '17 at 18:48
  • 1
    `/` between integers returns an integer value, and `%` returns the remainder of the division. so `51 / 25 = 2`, and `51 % 25 = 1`. Can calculate the number of quarters pretty straightforwardly. – Havenard Sep 25 '17 at 18:51
  • 1
    @Havenard Aside: A useful alternative to `double round(double)` is `long rlint(double)` as it not only rounds, it also converts to an integer type. – chux - Reinstate Monica Sep 25 '17 at 18:58
  • 1
    @chux That's good to know, I was looking for that. – Havenard Sep 25 '17 at 19:00
  • 1
    @chux is `rlint` standard? – Weather Vane Sep 25 '17 at 19:04
  • 1
    @WeatherVane C11dr 7.12.9.5 The lrint and llrint functions. May be C99 too – chux - Reinstate Monica Sep 25 '17 at 19:08
  • 2
    @chux sorry - your typo. The function is `lrint` not `rlint` you mentioned before. Thx though, new one for me. – Weather Vane Sep 25 '17 at 19:12

1 Answers1

0

As Havenard already wrote, the problem is that change was stored as float. For such a programm change must be stored as an integer value.

This your code with int instead of float:

#include <stdio.h>
#include <cs50.h>

void count_coins (void);

int coin = 0;
int change = 0;

int main (void) {
    do {
        change = 41; // insert a get integer function here
    } while (change < 0);

    count_coins();

    printf("%i\n", coin);
}

void count_coins (void) {
    while (change >= 25) {
        coin++;
        change -= 25;
    }

    while (change >= 10) {
        coin++;
        change -= 10;
    }

    while(change >= 5) {
        coin++;
        change -= 5;
    }

    while (change >= 1) {
        coin++;
        change -= 1;
    }
}
Sister Fister
  • 368
  • 1
  • 10
  • 1
    This is like dividing by repeatedly subtracting, and might take a very long time to execute for a large sum. Do you know the `%` modulus aka remainder operator? – Weather Vane Sep 25 '17 at 18:58
  • @WeatherVane Yes I know this. My intention here was to just change the datatype while keeping the remaining logic exactely the same, so that the OP can see where his programm failed. – Sister Fister Sep 25 '17 at 19:01