The change calculating program I wrote seems to be working as intended except when the input is .53 it returns 2 quarters and 2 pennies instead of 2 quarters and 3 pennies. It appears to be working fine for similar input values like 3.53, 0.23 or 0.38.

What could be causing this error?

    float c;
    double round(double c);

    int r=c*100;

First, when you call a function, you don't include its return type and/or its parameter(s) datatype(s). For example, the prototype for round() is

double round(double x);

This means that round() receives a double and returns a double. When we call it though, we call it like that


Where x is a double variable. Watch the short on functions for more information!

Second, you should multiply the amount you receive from the user by 100 first, then round it using roundf() or round() because these functions, as their names suggest, round floating-point values to the nearest integer. And if you round the input first, then multiply it by 100 it'll always give you a multiple of 100. For example, if I entered 1.99, do you think your program should

  1. round 1.99 first which gives 2
  2. then multiply 2 by 100 which gives 200


  1. multiply 1.99 by 100 first which gives 199.0
  2. then round 199.0 which gives 199

Lastly, you're dividing by quarters, dimes, nickles and pennies anyway regardless of q being >, < or = the divisor which is surely wrong since this is integer division and if q is < the divisor, the result will be 0. Probably some conditions will be sufficient to solve this.

Also, no need to use 4 variables for each type of coins. You could have used just one variable adding the number of coins of each type to it like

int total = 0;

// quarters
total += amount / quarters;
amount %= quarters;

// dimes
total += amount / dimes;
amount %= dimes;

and so on.

EDIT: you should only output the total number of coins followed by a newline character.

  • So it was still chunking. Confusion is gone now that I know how to read prototype composition, when to round and how to optimize the rest of the code! Thanks sensei – MrValue Jun 22 '14 at 9:27
  • Could You rephrase why having division result for q = 0 is wrong tho? Program never uses it as divisor later on so it seems alright to me. Even providing r=0 program never devides by 0. – MrValue Jun 22 '14 at 10:14
  • Consider these two variables, a and b, where a and b are integers and a < b. When dividing a/b (i.e., integer division), the result won't be a fraction or a floating-point value (because you're dividing integers). Instead, it will be just 0 which could lead to some logical errors in your program. – Kareem Jun 22 '14 at 10:25
  • My intention was to cover this eventuality with next line a%b. So that even if a/b divison is 0, remaining fraction is carried over under modulo a%b. – MrValue Jun 22 '14 at 10:37
  • I see. You were still doing one extra unneeded step though! Probably you may make use of that in a future program because this really can lead to some issues :) – Kareem Jun 22 '14 at 10:40

You are running into float imprecision. When you multiply 0.53 * 100 and store it in an int, you will truncate any decimals. 0.53 is actually stored as something like 0.52999999 and multiplying that by 100 will result in an integer of 52.

You want to round the value before storing it as an integer.

int r = round(c*100);  

is the correct syntax. That line you have in your code doesn't actually do anything.

Here's a good video on floating point imprecision that I found helpful: http://www.youtube.com/watch?v=PZRI1IfStY0


You must log in to answer this question.

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