0

My greedy program which is nothing like what they suggested to do and I just did whatever with...only works with some numbers...why? Thank you:)

#include <stdio.h>

int main()
{
  float change;
  int loop_times;

    printf("How much change do you owe?\n");
    scanf("%f",&change);

    for(loop_times=0;loop_times<1000;loop_times++)
    {
      if(change>=0.25)
        change=change-0.25;
      else if(change<0.25 && change>=0.10)
        change=change-0.10;
      else if(change<0.10 && change>=0.05)
        change=change-0.05;
      else if(change<0.05 && change>=0.01)
        change=change-0.01;
      else
        break;
    }

    printf("The fewest number of coins that you need to pay off your debt is %i.\n",loop_times);

  return 0;
}
1
  • 1
    Why don't you use MOD(%) operation. It will be simpler :) Mar 23 '16 at 7:11
1

It's all about the floating point number and how it is stored in the computer, which is the purpose of the lesson.

Floats are stored imprecisely in a computer. For example, if you try to store 5.50 in a float, it might be stored as something like 5.4999999978 or maybe like 5.500000023. (These are just illustrations. The exact stored number will be dependant on your computer's architecture.) The error in the number, both in amount and direction +/- will depend on the number stored and the architecture.

Here's how that affects your code. The computer does exactly and precisely what it is told. So, when you say if(change<0.25 && change>=0.10), look what could happen. If the float is .24999987, it is less than .25, but the actual number should have been .25, so the code fails to handle it correctly. (Actually, it would have been mishandled on the prior test.) Or, what if the float is .099999999978? Same problem.

This also illustrates the need for testing special cases, or corner cases, as they're sometimes known.

Want to see an effective demonstration? Compile the following on your computer and feed numbers into it.

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

int main(void)
{ 
    float cents;

    do
    {
        printf("Enter 0 to quit or a floating point number: ");
        cents =  GetFloat() ;    
        printf("You entered: %.15f \n",cents );   
        if (cents == 0.0) break;
    }
    while ( true );
}

You can see exactly what I mean when you enter .30 and .35.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

0

As Cliff B said, it's because of the float imprecision. Unless you are calculating interest, you don't care about fractions of a cent. I made it a challenge for myself to complete these challenges without cs50.h so I'm not sure how you'd do this with the Get* functions but I'd look into storing a number like 1.5 not as a float or double (which is really going to be something like 1.500000000004254146141 so you'll often be off by a penny) but as two ints delimited by a .

I'm sure there are plenty of other ways to solve it, but that's what I did.

0

Why not just multiply the float number of dollars by 100, and cast it to an int number of cents? Then you can do precise arithmetic without worrying about floating point imprecision.

Also @ProgyanBhattacharya is right about finding a good way to use the modulo operator % -- which is what I thought was the real point of this exercise.

You must log in to answer this question.