
September 26th, 2013, 06:35 PM
#1
Please Help! Convert money to denominations
Hello everyone I am in need of some help.. I thought that I did my assignment the way that my professor wanted but I just found out that he wants me to solve it differently.
Problem:
Given any amount of money expressed in dollars, and cents, this program computes the number of 100 , 50, 20, 10, 5 and 1 dollar bills and number of quarters, dimes, nickels, and pennies to be returned, returning how many of each denomination are included.
I figured out how to program it correctly for the most part except I cannot figure out how to handle large numbers, such as 14.49999999999999999999999999999999.
When converting that number to int it returns it as 14.50. Does anyone know how to handle converting doubles, similar to the one above, to an integer correctly?
This is the code that my teacher did not want, since I handled the input as a string:
Code:
#include <iostream>
#include <string>
#include<iomanip>
#include<windows.h>
using namespace std;
int main(){
const int hundredsConst=10000, fiftiesConst=5000, twentiesConst=2000, tensConst=1000;
const int fivesConst=500, onesConst=100, quartersConst=25, dimesConst=10, nickelsConst=5, tab=5;
int hundreds, fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies;
int dollarsBeforeDecimal, centsAfterDecimal, moneyInPennies, counter=1;
string name, hundredsEnd, fiftiesEnd, twentiesEnd, tensEnd, fivesEnd, onesEnd, quartersEnd, dimesEnd, nickelsEnd, penniesEnd;
char decimalPoint;
cin >> dollarsBeforeDecimal >> decimalPoint >> centsAfterDecimal;
moneyInPennies = (dollarsBeforeDecimal*100) + (centsAfterDecimal);
hundreds = moneyInPennies/hundredsConst;
moneyInPennies = hundreds*hundredsConst;
fifties = moneyInPennies/fiftiesConst;
moneyInPennies = fifties*fiftiesConst;
twenties = moneyInPennies/twentiesConst;
moneyInPennies = twenties*twentiesConst;
tens = moneyInPennies/tensConst;
moneyInPennies = tens*tensConst;
fives = moneyInPennies/fivesConst;
moneyInPennies = fives*fivesConst;
ones = moneyInPennies/onesConst;
moneyInPennies = ones*onesConst;
quarters = moneyInPennies/quartersConst;
moneyInPennies = quarters*quartersConst;
dimes = moneyInPennies/dimesConst;
moneyInPennies = dimes*dimesConst;
nickels = moneyInPennies/nickelsConst;
moneyInPennies = nickels*nickelsConst;
pennies = moneyInPennies;
return 0;
}

September 26th, 2013, 11:04 PM
#2
Re: Please Help! Convert money to denominations
Originally Posted by coolioschmoolio
Hello everyone I am in need of some help.. I thought that I did my assignment the way that my professor wanted but I just found out that he wants me to solve it differently.
Differently in what way? We are not your professor, and programs can be written in practically any number of ways to achieve the same results.
I figured out how to program it correctly for the most part except I cannot figure out how to handle large numbers, such as 14.49999999999999999999999999999999.
That is not a large number. The issue is that floating point fractional values cannot be represented exactly in binary unless they are a sum of negative powers of 2. Just like 1/3 cannot be represented as an exact decimal fraction (0.33333...), numbers such as 0.49 cannot be represented exactly in binary.
Please read this:
http://www.parashift.com/c++faq/floatingpterrs.html
When converting that number to int it returns it as 14.50. Does anyone know how to handle converting doubles, similar to the one above, to an integer correctly?
You cannot use doubles or floats to do this "exact" math. That is the bottom line.
Either use integers, or use a library that handles exact math. Looking at your original solution, it seems to use integer. Believe it or not, that is more or less the correct way to solve the problem. If your professor insists on using doubles and floats, and expects to get the same results, then again, it isn't going to happen due to floating point being inaccurate.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 26th, 2013 at 11:06 PM.

September 27th, 2013, 06:55 AM
#3
Re: Please Help! Convert money to denominations
Originally Posted by Paul McKenzie
then again, it isn't going to happen due to floating point being inaccurate.
to be more precise:
the problem domain can be restricted enough so that you CAN make it work with floating point by making sure the accumulated inacuracy/error in the double never bleeds into your results.
This isn't easy stuff however, it requires a lot of attention to details and some 'tricks' to do right.
If your plan is really to handle 14.49999999999999999999999999999999.
THen the bigger issue is that a double does not have enough precision to even approximate that number in decimal. (a float is considerably worse). Any decimal number with more than 15 digits cannot be stored in a double without inherently loosing precision.
if you really need to work with that kind of numbers, you'll need either a 'big decimal' or 'big' fixed point or 'big' floating point class/library.

September 27th, 2013, 11:01 AM
#4
Re: Please Help! Convert money to denominations
he wants me to solve it differently.
How does he expect you to solve it differently? In what way? As the smallest denomination that can be specified on output is a penny (1 cent of 1/100 of 1 dollar) the smallest value that can be returned by the program is 1 penny for an input of 0.01. Therefore the program needs to work to 2dp. If an input of say 14.4999999999999 is entered, what is the expected output for number of pennies? You get
1 10 dollar
4 1 dollar
1 quarter
2 dimes
4 pennies
which is 14.49 dollars. And ???? for the remaining 0.009999999999 of a dollar? You either need to round up or round down
(to 14.50 or 14.49) to a whole number of pennies. What does your 'professor' expect for these fractions of a penny?
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C, C++ Compiler: Microsoft VS2017.2
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
