-1

I'm very new to CS and programming and am trying to write a code for the cs cash problem set 1.I don't think this is the intended solution and I know this isn't a clean and condensed code, but at this point I'm just trying to make it run.I was wondering if the code I wrote was complete gibberish or if it could actually work because it honestly made sense to me theoretically.

  #include<stdio.h>
#include<cs50.h>
#include<math.h>
int main(void)
{
    float change;
    do
    {
     change= get_float("Enter change here\n");
    }
     while(change<0);
    int quarter=floor(change/0.25);
    int dime=floor((change-(quarter*0.25))/0.10);
    int nickel=floor((change-(quarter*0.25)-(dime*0.10))/0.05);
     int penny=round((change-(quarter*0.25)-(dime*0.10)-(nickel*0.05))/0.01);
    int sum =quarter+nickel+dime+penny;
    printf("%i",sum);
   
     
}

        
  • @paxdiablo uh...I lost my mind, thanks for pointint that out. – Louis Go Aug 19 '20 at 03:25
  • 1
    FYI to any reviewer; the question looks like code review at first glance, but the real question is how to compile and run it. There's a header file and another c file missing, and there's no platform nor compiler tag. All fixable issues. – Joshua Aug 19 '20 at 04:01
  • 2
    It is still missing a clear question. The CS50 has **lots** of instructions for how to run the code, what have you tried and where? – Antti Haapala -- Слава Україні Aug 19 '20 at 04:41
  • 1
    @Joshua "fixable" yes, but before asker fixes would still be called "unsalvageable"... – Antti Haapala -- Слава Україні Aug 19 '20 at 04:42
  • And the program seems to work... somewhat, so what would be the question? – Antti Haapala -- Слава Україні Aug 19 '20 at 04:45
  • hahaha sorry about that, this is the first time I use Stackoverflow. Basically my code runs but it does not run correctly if that makes sense.For example, if the user inputs 4.2 as the change, the answer should be 18, because you need 16 quarters and and 2 dimes right? However, when I run my code with the change input as 4.2 I got 22 coins needed.This code is supposed to return the amount of coins(using quarters, dimes,nickels,and pennies) needed for a certain amount of change. I don't know if that helped but I hope it did. – user14128559 Aug 19 '20 at 04:58
  • Why do all these seem to start off with 'float change;' :(( – Martin James Aug 19 '20 at 09:03
  • @MartinJames: It's not a bad idea. It was my first real exercise in algorithms too. His base algorithm form is not quite correct but it really just needs debugging. – Joshua Aug 19 '20 at 15:33

1 Answers1

1

This is a common issue that many beginners struggle with, which is the imprecision on floating point numbers. When doing operations with floats and doubles, it's possible for the resulting value to be off by a small decimal. You can learn more about it on this SO post: Why are floating point numbers inaccurate?.

Anyways, your program involves float division and integer truncation, which usually don't work well together. In the example of $4.20 as input, after quarters has been set to 16, when calculating dimes, (4.2 - 0.25 x 16) / 0.10 is actually saved as 1.999998, instead of 2. This is then truncated (since dime is an int) and saved as 1, which explains the inaccurate outputs that your program is giving you. A solution would be to multiply everything by 100, using integer division to avoid imprecision. So the above example would become (420 - 25 x 16) / 10, which will correctly be saved as 2 in your dime int.

  • hey, I think I kinda get what you are saying.My only question is why is dime stored as 1.999998 instead of 2. Because (4.2-(16*0.25))/0.10=exactly 2, why does the code or variable dime store it as a decimal.I'm sorry if I'm making you repeat anything, but I'm very new to this so I'm a little slow lol. – user14128559 Aug 19 '20 at 23:15
  • I posted this link in my answer if you didn't see: https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate . A brief summary is that since computers use binary, or base 2 to do calculations, when performing calculations with floating point numbers, sometimes the resulting answer is impossible to store in a base 2 fraction. Even in our base 10 system, we can't always show the exact amount of a fraction with a finite amount of digits, an example being 1/3, which is 0.33333 recurring. – Sankeeth Ganeswaran Aug 20 '20 at 03:26