0

This is a problem in cs50 which states

Implement a program that calculates the minimum number of coins required to give a user change.

The output of the problem is the number of coins to be given to the user.I made the program and it is working almost perfectly but for 4.2 dollars the output is 22 but it should have been 18. Every other value the ouput is correct. Can anyone tell what is wrong with this code?

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

int main(void)
{
    float change_owed, change_owed_in_hundreds;
    int number_of_coins_to_be_given = 0;
    do
    {
        printf("enter the positve ammount of change owed in dollars");
        change_owed = get_float();
    }while(c < 0);

    change_owed_in_hundreds = change_owed * 100;

    while(change_owed_in_hundreds >= 25){
        change_owed_in_hundreds = change_owed_in_hundreds - 25;
        number_of_coins_to_be_given++;
    }
    while(change_owed_in_hundreds >= 10) {
        change_owed_in_hundreds = change_owed_in_hundreds - 10;
        number_of_coins_to_be_given++;
    }
    while(change_owed_in_hundreds >= 5) {
        change_owed_in_hundreds = change_owed_in_hundreds - 5;
        number_of_coins_to_be_given++;
    }
    while(change_owed_in_hundreds >= 1) {
        change_owed_in_hundreds = change_owed_in_hundreds - 1;
        number_of_coins_to_be_given++;
    }

    printf("%d\n", number_of_coins_to_be_given);
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ironman19_
  • 11
  • 2
  • Are you sure that isn't one of the cases where greedy algorithm fails? Also care to tell the number of coins of each denomination to be given to the user when the change is $4.2? https://stackoverflow.com/questions/13557979/why-does-the-greedy-coin-change-algorithm-not-work-for-some-coin-sets – Rocket Pingu May 16 '18 at 06:29
  • Try using the debugger, and you can figure it out yourself. – akshayk07 May 17 '18 at 19:28
  • i used debugger and turn out that the input value that i gave to the program that is 4.2 is considered as 4.19999902 and the value of c_! becomes 419. – ironman19_ May 18 '18 at 05:20

0 Answers0