# cs50 Problem Set 1: Greedy. float 0.01 problem

sorry for the long title I'm currently doing cs50's problem set 1's Greedy and I've run into an issue. It seems that every cent works except for one's that need to use .01 cent. I've changed my code to display what the command gets when I give it 0.01 cents. Here's the code:

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

int main (void)
{
int changeamount = 0;
printf("Amount to be converted to change: ");
float change = GetFloat();
float roundf(float change);
for (changeamount = changeamount - 1 + 1; change >= .25; changeamount++)
{
change = change - .25;
}
for (changeamount = changeamount - 1 + 1; change >= .10; changeamount++)
{
change = change - .10;
}
for (changeamount = changeamount - 1 + 1; change >= .05; changeamount++)
{
change = change - .05;

}
for (changeamount = changeamount - 1 + 1; change >= .01; changeamount++)
{
change = change - .01;
}
change = change + changeamount;
printf("%.50f\n", change);

}
``````

It only doesn't work whenever I input an amount of money which would take the 1 cent to show (like: 1.01 or 0.41). Here's what it tells me when I input 0.01:

0.00999999977648258209228515625000000000000000000000

I've tried rounding it using:

``````float round(float change)
``````

I've also tried converting it to an int and then rounding it:

``````int changeint = change * 100 / 100
``````

Both of which didn't end up helping. So any tips/hints at what's wrong with the code would be great! (I don't want to get a direct answer as that could be considered cheating)

EDIT: Here's the updated code:

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

int main (void)
{
int changetwentyfive = 0;
int changeten = 0;
int changefive = 0;
int changeone = 0;
int changeamount = 0;
printf("Amount to be converted to change: ");
float changefloat = GetFloat();
if (changefloat < 0)
{
do
{
printf("Amount to be converted to change: ");
changefloat = GetFloat();
}
while(changefloat < 0);
}
int change = changefloat * 100;
for (changeamount = changeamount - 1 + 1; change >= 25; changeamount++)
{
change = change - 25;
changetwentyfive++;
}
for (changeamount = changeamount - 1 + 1; change >= 10; changeamount++)
{
change = change - 10;
changeten++;
}
for (changeamount = changeamount - 1 + 1; change >= 5; changeamount++)
{
change = change - 5;
changefive++;

}
for (changeamount = changeamount - 1 + 1; change >= 1; changeamount++)
{
change = change - 1;
changeone++;
}
printf("%d\n", changeamount);
printf("25: %d, 10: %d, 5: %d, 1: %d", changetwentyfive, changeten,     changefive, changeone);

}
``````

Hope you can help me.

Most of the problem is that no matter what you do, you keep putting the result back into `change`, a float. Floats have this nasty little problem that they don't store fractional parts of a number, the decimal part, correctly. In fact, your question demonstrates it well. If you store 0.01 in a float, it actually gets stored as `0.00999999977648258209228515625`. This happens because everything stored in a computer is stored in binary. Most fractional parts of a whole number simply cannot be completely accurately stored in a computer because of the conversion from decimal to binary. In fact, this is the whole point of this lesson!

That's why it is necessary to convert to something that is in whole numbers and can be stored as an integer. (That's a really big hint.) Also remember, when converting from a float to an int, the decimal portion gets truncated at the decimal point, not rounded. That means that if you have a float of 2.999 and store it in an int, the number that is stored is 2, not 3.

This should be enough to get you going. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• This fixes all my problems!... almost. the only single problem left is that it doesn't pass the check for 4.2. It says it uses 22 instead of 18 and I have no idea what's wrong. I did add some parts to the code so it tells me how much it uses of a specific coin and instead of using 16 of 25 and 2 of 10 it uses 16 of 25, 1 of 10, 1 of 5 and 4 of 1. Any hints at how to fix this? edit: updated code in the top post thingy Jul 3 '16 at 7:21
• When you add all those coins together, what's the total? Try printing out change immediately after `int change = changefloat * 100;` Jul 3 '16 at 7:28
• It prints out 419 which means that as the idea giving walkthrough thing said I need to somehow round it? I've tried doing roundf and round but I can't seem to make it work :/ Jul 3 '16 at 20:38
• I've taken another look at your comment and the tip giving greedy walkthrough and figured out the problem. I was not rounding it properly. All I needed to do when converting to an int is to also include the round in the same command. Jul 4 '16 at 7:27

When 4.2 is stored as float, it is stored as 4.199999....Doing changefloat=changefloat*100 causes the value stored in changefloat as 419.999....Now do changefloat=round(changefloat). This changes value of changefloat to 420.0000001...Storing this value in an integer variable by typecasting like: int change=(int)changefloat ; makes the value of change to be 420.

The test case having 4.2 as amount is stored as 4.19.Try this : changefloat = round(changefloat); int change = changefloat * 100; This will change the amount from 4.19 to 4.2.This change will surely help you pass the test case.If this answers your question, please click on the check mark to accept.

• Souradip, you should try that yourself. That would change it first to 4.0 and then to 400. I'll let you run it in a test program to figure out why. Also, the idea here is to help people figure out why so that they can learn how to get to the answer, not to just give the answer. Jul 3 '16 at 10:01
• Sorry,I have given the order wrong. When 4.2 is stored as float, it is stored as 4.199999....Doing changefloat=changefloat*100 causes the value stored in changefloat as 419.999....Now do changefloat=round(changefloat). This changes value of changefloat to 420.0000001...Storing this value in an integer variable by typecasting like: int change=(int)changefloat ; makes the value of change to be 420. Jul 7 '16 at 10:45