Click here to Skip to main content
15,889,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
void change(float amount, int &d2, int &d1, int &c50, int &c20, int &c10, int &c5)
{
	int dollars = amount;

	d2 = dollars / 2;
	d1 = dollars % 2;

	int cents = (amount - dollars) * 100; // here's probably the prob

	c50 = cents / 50;
	c20 = cents % 50 / 20;
	c10 = cents % 50 % 20 / 10;
	c5 = cents % 50 % 20 % 10 / 5;
}

If the amount = 79.85, c5 will be finally equal to 0.

I reckon the problem is probably about the assignment of the variable cents.

Can anyone solve this problem?

Thanks in advance.
Posted
Updated 30-Apr-12 15:27pm
v2
Comments
[no name] 30-Apr-12 21:20pm    
The question is whacky - there is not one fp calculation in your code.
Where are the code comments - how do we read your intentions?
What is the algorithm you are implementing?
Have you stepped through this in the debugger - that will tell you where your algorithm is falling over?
Sergey Alexandrovich Kryukov 1-May-12 0:13am    
Agree. Generally, there are all signs of bad code.
OP really needs to explain the goal of it.
--SA
Peter_in_2780 30-Apr-12 21:42pm    
In general, it is a VERY bad idea to use floating point data types for currency. Use integer cents (e.g. 200 represents $2). That way the small change doesn't fall down the back of the couch.
Sergey Alexandrovich Kryukov 1-May-12 0:11am    
Not always. From this code sample, we don't know if this is currency of... money. Many money calculations are floating point by its very nature. A simple obvious example is conversion between different currencies.

What is really bad is mixing -- in intermediate calculations -- integer and floating point types, or, more generally types with different accuracy. Rounding error can accumulate, especially it this is done without thorough thinking...
--SA
Sergey Chepurin 1-May-12 8:43am    
>If the amount = 79.85, c5 will be finally equal to 0.
And what result do you expect? How is that related to floating point precision???

As the previous solution by Pablo has correctly shown, the problem lies in the line

int cents = (amount - dollars) * 100; // here's probably the prob


and you were correct in your comment. What happens is that (amount - dollars) is a floating point operation (contrary what one of the comments above said) and delivers a value of 0.849998 or something the like. This is due to the fact that the floating point representation cannot correctly store all rational numbers. Instead it uses the nearest approximation that can be represented by a mantissa to the base 2.

In the next step that value is multiplied by 100 (which is also forced up to floating point) and you get: 84.9998, which is then truncated to 84 by the cast to int.

So what went wrong? You should not have cast the value to int but rounded it to int. Here is a way to do that:

double centsDlb = (amount - dollars) * 100.0;
int cents = floor (centsDlb + 0.5);


Of course you can contract both lines of code to a single one.

The solution given by Pablo will also get you there, but imagine what happens if someone specifies 79.849 as input. That's why you want to use rounding.

Don't forget to include "math.h" when you use the floor function.

And you might want to check what happens if someone enters a negative amount to your function.

Hope that helps.
 
Share this answer
 
If amount is 79.85, dollars (which is an int) will be 79.
Cents may be 85, or 84 (floats are not accurate): did you test this?
If cents is 84, c5 will be 84 % 10 / 5 which is 4 / 5 which is (as an integer) zero.
If cents is 84, you may try:
C++
int cents = (amount + 0.00001 - dollars) * 100.0;


Hope this helps,
Pablo.
 
Share this answer
 
You've mixed up two problems in one function:

1. converting a float representation into a fixed point representation
2. determining the minimum amount of coins to represent an amount of money

I suggest separating the two. You may need that conversion in other places too. Apparently you're currently stuck with the conversion, but solution 1 should help there. See that it works before trying to solve 2.

(Even if this is just a programming assignment, it won't hurt to separate the two functionalities in different functions - that's what you should do in practice anyway)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900