Given an amount of currency, return a vector of this form:
[100 50 20 10 5 2 1 0.5 0.25 0.1 0.05 0.01]
Input a = 257.68
Output b is [2 1 0 0 1 1 0 1 0 1 1 3]
Always use bigger bills/coins if possible.
whoa..rounding error uncovered
Also, "round(X,N)" gives an error - almost like it couldn't have a 2nd parameter
Great problem, not too hard but required a bit of thinking
It took me a long time (before reading the comments !!!) to figure out that for instance the result for rem(135.01,100) is not 35.01 but is 35.009999999999991.
Can anyone help me with why the issue is there and how to solve it (not by adding 1e-3)!?
good job! i enjoyed solving this code
I cannot understand this problem. what means?
Why function roundn is undefined?
hmmm, for the problem to pass, you need to ad 0.001 to a
Because of the floating point arithmetic, the last elsement of vector b may be different from the right answer(usually 1 smaller than it). So I add 1e-4 at last to mend it.
I get the right answer for this using this code....
function b = makingChange(a)
change = [100 50 20 10 5 2 1 0.5 0.25 0.1 0.05 0.01];
for i = 1:(length(change)-1)
b(i) = floor(a/change(i));
a = a - b(i)*change(i);
b(12) = a/change(12);
But it tells me I'm getting them all wrong. Not sure why.
The real challenge here is an issue with numerical precesion when eg using round or floor. I do not think that this was intented. The "fix" here was taken from other comments to this solution (ie add 1e-6 to input a)
I had to add the 'round' function in because for some reason both my Matlab and this are getting 135.01-100=35.0099999999. Not sure why.
1e-3 is required since there is a MATLAB bug which makes the last "a" slightly (1e-15) less than 0.01
Not a MATLAB bug, an inherent problem with representing decimals on a binary system.
I struggled with floating point issues, so this was the best solution I could come up with.
I could use a bit of help on this solution, as i am just a freshman. By reading it now line 4 mind sound strange, but without it, it didn't work. I always had one penny less than expected. I thought, that this could be a numerical problem using mod() and fix() in the way, that the mod function decreases the value a litte bit and than fix doesn't work right. I am not quiet sure and could need some help by an experienced matlab user. Thanks in advance and a nice day :)
In test case 2, 135.01 is represented internally as 135.009999999999991, so the fractional part is slightly less than 0.01, which leads to the problem. You could try rounding the input (and the change vector) to an integer number of pennies so all the calculations would use integers.
for doesn't have to be as dull as i=1:N
Having numerical precision problems...hence the sqrt(eps).
For me, it is difficult to make good code with the rouding error at the end.
I got rid of the rounding error by adding the 1e-6 ...
You can also multiply the original amount and the denominations by 100. Cheers.
Make a Palindrome Number
Implement a ROT13 cipher
find the maximum element of the matrix
Remove all the words that end with "ain"
Back and Forth Rows
Determine if input is odd
Clean the List of Names
Find the alphabetic word product
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office