# Why my greedy program only work on int number, not on float number?

I am trying to catch what is the problem and getting tired! If you have any suggestion, that will be very great and helpful!!!

my program layout is here :

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

int dollar = 0 ;
float quarter = 0 ;
float dime = 0 ;
float nickel = 0 ;
float penny = 0 ;
int count_coins = 0 ;

int rem_quat = dollar % 25 ;
int rem_die = rem_quat % 10 ;
int rem_nik = rem_die % 5 ;
int rem_pny = rem_nik % 1 ;

do
{
printf("How much own to customer? ");

dollar  =  GetFloat();

if ( rem_quat >= 0 )

{
quarter = dollar * 100 / 25 ;
}
else if ( rem_die <= rem_quat )
{
dime = dollar * 100 / 10 ;
}
else if ( rem_nik <= rem_die )
{
nickel = dollar * 100 / 5 ;
}
else if ( rem_pny <= rem_nik )
{
penny = dollar * 100 / 1 ;
}
else
{
printf("You have invaild number\n");
}

count_coins = quarter + dime + nickel + penny + rem_quat + rem_die + rem_nik + rem_pny;

} while ( dollar < 0);

printf("%d coins\n",count_coins);
}
``````
• obviously you have logical errors in your code, but you should probably be more specific and maybe give examples on how you think your code works for `int`s only!
– kzidane
Jun 28, 2015 at 1:13

If you're still having problem, consider this pseudocode :)

``````Repeat
Until input is valid

Convert float dollars to int cents

Calc how many 25¢ needed
Calc how much left after giving away 25¢s

(Repeat above two for 10¢, 5¢ and 1¢)

Total coins to be given = number of 25¢ + 10¢ +5¢ + 1¢

Print output
``````

Of course you don't have to follow this to achieve the same effect. Personally I further recognised this part

``````Calc how many 25¢ needed
Calc how much left after giving away 25¢s

(Repeat above two for 10¢, 5¢ and 1¢)
``````

is pretty much copy and paste, a hint that a better design is available. So I stored 25, 10, 5, 1 as array, and use a for loop to handle that part. To do that, I use

``````For each x cents in array
Calc how many x needed
Calc how much left
nCoins += how many x needed
``````

In summary, instead of finding number of 25¢ (and others) used and adding together, I opt to do addition straightaway while calculating. Saves me lots of variables space :)

But ultimately, your design is up to you.

– Lwin
Jul 4, 2015 at 16:52
``````....
int rem_quat = dollar % 25 ;
int rem_die = rem_quat % 10 ;
int rem_nik = rem_die % 5 ;
int rem_pny = rem_nik % 1 ;

do
{
printf("How much own to customer? ");

dollar  =  GetFloat();

if ( rem_quat >= 0 )
....
``````

Logic errors. Since your modulus came without knowing `dollar`, I assume all `rem_` variables equal zero. Remember, compilers go from top to bottom, left to right.

Fix: You need to ask for float first, then you do the modulus stuff.

``````do
{
printf("How much own to customer? ");

dollar  =  GetFloat();

if ( rem_quat >= 0 )

{
quarter = dollar * 100 / 25 ;
}
else if ( rem_die <= rem_quat )
{
dime = dollar * 100 / 10 ;
}
else if ( rem_nik <= rem_die )
{
nickel = dollar * 100 / 5 ;
}
else if ( rem_pny <= rem_nik )
{
penny = dollar * 100 / 1 ;
}
else
{
printf("You have invaild number\n");
}

count_coins = quarter + dime + nickel + penny + rem_quat + rem_die + rem_nik + rem_pny;

} while ( dollar < 0);
``````

Notice your `if` statements are inside the `do-while` loop! This means that if the user doesn't cooperate to produce a positive number, you try to perform the operations anyway.

Fix: The `if`s shouldn't be in the loop.

``````int dollar = 0 ;
...

do
{
printf("How much own to customer? ");

dollar  =  GetFloat();
...
``````

Lack of understanding of datatypes. Compilers are quite sensitive with types. You initialised `dollar` as `int` yet you try to use `GetFloat()`! Also you have got your `Float` and `int` mixed up.

Fix: Get `dollar` as `Float`. Since decimal points are imprecise, convert this `dollar` (1.99) to `int` (199) and work in cents. You do this by multiplying 100 and also rounding off. This should happen way before you attempt to calculate the number of coins by division. Thus, only `dollar` should be `float`, maybe one more var `int cents`. Your other variables `rem_` should all be in `int` because these are all cents (whole number).

Also, you don't actually need `if` to find number of coins. Eg:`20 % 25 = 20` so you don't need to check if your remaining cents is more than 25 cents.

• Thanks so much! Now, I see what is it messing me up!!!
– Lwin
Jul 1, 2015 at 11:56
• So, I did as you suggested! I have a one problem that occours
– Lwin
Jul 2, 2015 at 3:59