0

My original code for this problem is as follows:


int main(void)
{

float c = 0; //total coins

float $ = get_float("Change: ");


while($ <= 0.0099999 && $ >= 0.00000001)
{
    $ = get_float("Change: ");
}

while($ <= -0.00000001)
{
    $ = get_float("Change: ");
}

while($ == 0)
{
    printf("0");
}

float updated_$ = $;


while(updated_$ >= 0 || updated_$ >= -0.25)
{
    if ($ >= 0.25 && updated_$ >= 0.25) {{
        updated_$=updated_$ - 0.25;}
        c++;
    }
    else if($ >= 0.10 && updated_$ >= 0.10) {{
           updated_$=updated_$ - 0.10;}

           c++;
    }
     else if($ >= 0.05 && updated_$ >= 0.05) {{
             updated_$=updated_$ - 0.05;}
             c++;<p>
    }
     else if($ >= 0.01 && updated_$ >= 0.01) {{
             updated_$=updated_$ - 0.01;}
             c++;
    }
    else if(updated_$ >= 0.0098 && updated_$ <= 0.01) {{
        updated_$=updated_$ - 0.098;}
        c++;
    }
     else if(updated_$ < 0.0098) {
         printf("%f\n", c );
         return 0;
     }
     }
 }
}

This program is returning correct values for every number I tested so far except for 4.2, which it incorrectly computes as 23 coins instead of 18. When I run debug50, I can see that the input 4.2 is changed to something like 4.19999998 for variable $ while the program is running. In fact, for any number entered, $ is changed to some similar imprecise value, hence why I added certain parts to the code such as

else if(updated_$ >= 0.0098 && updated_$ <= 0.01) {{
updated_$=updated_$ - 0.098;}
c++;

I have searched around this site for various lines of code that would help me round the value to the nearest hundredth decimal place during the actual computation, and so far the codes I have found and tried to insert only round the result of the computation, giving me the same incorrect answer for 4.2 of 23, and when I run debug50,I can still see that the incorrect value of 4.19999..something is being used for the computation itself. I have tried to use roundf as follows:


int main(void)
{
{
float c = 0; //total coins

float $ = get_float("Change: ");

float $r = roundf($ * 100) / 100; 

while($ <= 0.0099999 && $ >= 0.00000001)
{
    $ = get_float("Change: ");
}

while($ <= -0.00000001)
{
    $ = get_float("Change: ");
}

while($ == 0)
{
    printf("0");
}

float updated_$ = $r;


while(updated_$ >= 0 || updated_$ >= -0.25)
{
    if ($r >= 0.25 && updated_$ >= 0.25) {{
        updated_$=updated_$ - 0.25;}

        c++;
    }
    else if($r >= 0.10 && updated_$ >= 0.10) {{
           updated_$=updated_$ - 0.10;}

           c++;
    }
     else if($r >= 0.05 && updated_$ >= 0.05) {{
             updated_$=updated_$ - 0.05;}
             c++;
    }
     else if($r >= 0.01 && updated_$ >= 0.01) {{
             updated_$=updated_$ - 0.01;}
             c++;
    }
    else if(updated_$ >= 0.0098 && updated_$ <= 0.01) {{
        updated_$=updated_$ - 0.098;}
        c++;
    }
     else if(updated_$ < 0.0098) {
         printf("%f\n", c );
         return 0;
     }

     }
}
}

$r is still being changed to 4.1999... during the computation instead of 4.2 as I had hoped, and then the printf result is rounded instead. I also tried inserting

for(double a=120;a<=130;a+=1.0);

into my code and again had the same problem of the $ value being converted to an imprecise value once inserted and only the printf final result being rounded. Is there a way to round the value of $ for the computation itself, so that I will see it as 4.2 instead of 4.1999...etc when I run debug50?(ie, all the computations will be run with 4.2 exactly as entered, instead of being changed to 4.1999...) Or maybe there is another way around this? Perhaps I inserted roundf into the program in such a way that it didn't work as desired?

1
  • It looks like the code was cut and pasted from an http formatted page, making it confusing, and it needs to be reformatted for this site. Please review the following link to learn how to post formatted code and make life easier for everyone! ;-) meta.stackexchange.com/questions/22186/…
    – Cliff B
    Jun 3 '18 at 21:14
2

The code seems far more complicated than necessary, but this seems to be because you're trying to handle the imprecise float storage problem unsuccessfully.

This can be resolved by simply converting dollars and cents in a float to cents only and storing it in an int. It's not that hard. First, remember that round functions round the input to the nearest whole number, not to a particular digit. With that in mind, the idea is to multiply dollars by 100 to get cents, round it to the nearest cent and store in in an int, not a float. If the result is stored in a float, the storage inaccuracy problem is reintroduced. Then, calculate the number of coins accordingly, using all integers. Floats won't be needed at that point.

Also, I don't understand the use of two get_float commands. The second one, if it runs, is an infinite loop. Getting the user input and validating it can be done with a single do/while loop.

Finally, while it works, using $ as a variable isn't a good practice. A descriptive word would be better. $ is used for other purposes in c, much as a keyword is used.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .