So, I understand the point of the exercise is to show how imprecisely the floating points numbers are stored on the computer, I believe I have changed the floats to integers, but when I compile and run the code this happens:
1. If I store an input of 1:
jharvard@appliance (~/Dropbox/CS50/pset1/common): ./greedy Type input: 1
You gave the customer 7 coins! How greedy of you!
Here the obvious result would be 4 quarters.
2. The CHECK50 results:
jharvard@appliance (~/Dropbox/CS50/pset1/common): check50
2015.fall.pset1.greedy greedy.c
:) greedy.c exists
:) greedy.c compiles
:( input of 0.41 yields output of 4 \ expected output, but not "\nYou gave the customer 4 coins! How gr..."
:( input of 0.01 yields output of 1 \ expected output, but not "\nYou gave the customer 4 coins! How gr..."
:( input of 0.15 yields output of 2 \ expected output, but not "\nYou gave the customer 4 coins! How gr..."
:( input of 1.6 yields output of 7 \ expected output, but not "\nYou gave the customer 9 coins! How gr..."
:( input of 23 yieldsoutput of 92 \ expected output, but not "\nYou gave the customer 95 coins! How g..."
:( input of 4.2 yields output of 18 \ expected output, but not "\nYou gave the customer 20 coins! How g..."
:)rejects a negative input like -.1 :) rejects a non-numeric input of "foo"
:) rejects a non-numeric input of ""
Here is the code...
#include <stdio.h>
#include <cs50.h>
#include <math.h>
/* Change input variable */
float change;
/* Value of each coin */
int quarter = 25, dime = 10, nickle = 5, pennie = 1;
/* Amount of coins for final printf */
int coins_amount = 0;
/* *** PROGRAM STARTS *** */
int main (void)
{
/* Asks for user input.*/
do
{
printf ("Type input: ");
change = GetFloat();
/* Rounding the float change value.*/
change = (int)round(change *100);
}while (change <= 0);
/* Beggining of the loop*/
while (change > 0)
{
/* If quarter is smaller than the change do this...*/
do
{
change = (change - quarter);
coins_amount = (coins_amount + 1);
}while (quarter <= change);
/* If dime is smaller than the change do this...*/
do
{
change = (change - dime);
coins_amount = (coins_amount + 1);
}while (dime <= change);
/* If nickle is smaller than the change do this...*/
do
{
change = (change - nickle);
coins_amount = (coins_amount + 1);
}while (nickle <= change);
/* If pennie is smaller than the change do this...*/
do
{
change = (change - pennie);
coins_amount = (coins_amount + 1);
}while (pennie <= change);
printf ("\nYou gave the customer %d coins! How greedy of you!\n", coins_amount);
}
}
I know is not elegant at all, but I've been breaking my head to do this. What is wrong with it? And where could I found more material to learn more about this floating errors?