#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
float quarter = 25, dime = 10, nickle = 5, penny = 1,dollar;
int coins = 0;
int cents;
// prompts the user for the value of change in valid parameters
do
{
printf("How much change is owed?\n");
dollar = GetFloat();
if (dollar == 0)
{
return 0;
}
}
while (dollar <= 0);
cents = (int) roundf(dollar * 100);
// takes as many quarters as possible from the value of change
while( quarter < cents )
{
cents = cents - quarter;
coins++;
}
// takes as many dimes as possible from the value of change
while( dime < cents )
{
cents = cents - dime;
coins++;
}
// takes as many nickles as possible from the value of change
while( nickle < change )
{
cents = cents - nickle;
coins++;
}
// takes as many pennies as possible from the value of change
while( penny < cents )
{
cents = cents - penny;
coins++;
}
// prints how many coins were used
printf("%d\n", coins);
return 0;
}
Hey, so this is my code for greedy. When I run it, the amount of necessary coins is slightly imprecise ( when I modify it to a float value, I see just a clean 3.00000 * 10 for example with a roundf for change). For example, when I input 0.41 it shows 3 or 5 to 22 coins or 10 to 42 coins, never actually getting the precise amount of coins needed even if the aforementioned modification not in this topic shows a clean floating value.
Please help !
Removed code, problem solved.