PDA

View Full Version : Comp Sci Question


NDiggity
Oct3-07, 09:57 PM
1. The problem statement, all variables and given/known data
(Programming) A customer brings purchases worth less than $20 to the till, and hands over a $20 bill. Write a program that prompts a user for the purchase total, then figures out what change the customer should receive -- i.e., how many $10 bills, how many $5 bills, how many toonies ($2), loonies ($1), quarters (0.25), dimes (0.10), nickels (0.05), and pennies (0.01).

3. The attempt at a solution
My only problem is with the pennies part. Here is what I have:

while (change > 0.00) {
change = change - .01;
numberOfPennies = numberOfPennies + 1.;
}

My problem is, lets say the purchases are 19.99. So the change is .01. So my program says how many pennies they should get back. The problem is it says 2. If the purchase is 19.98, it says 3 pennies. What am I doing wrong?

DefaultName
Oct3-07, 10:03 PM
Can you tell me the variable TYPES and INITIALIZATIONS (if any)?

Perhaps sample code, pasted on rafb.net/paste ?

NDiggity
Oct3-07, 10:08 PM
Ok, I did that website thing.
http://rafb.net/p/VkUFTJ72.html

Hurkyl
Oct3-07, 10:08 PM
What am I doing wrong?
You are using approximate floating-point arithmetic. Therefore, you are getting an approximate answer.

DefaultName
Oct3-07, 10:11 PM
Also, you can tidy up your code if you do something like http://rafb.net/p/0yRiIq66.nln.html

You'll save a ton of space (reduce # of lines), and it might be easier to read.

NDiggity
Oct3-07, 10:13 PM
You are using approximate floating-point arithmetic. Therefore, you are getting an approximate answer.
Our teachers said if you mix int with float, you will get bad answers. Is this only true with division and multiplication? Also, any other time it deals with pennies its fine, only when the change is strictly pennies does it do this.

One more question. If the input is invalid, I want an error message to pop up and the program to stop. What is the function to stop a program. Sorry, I have only been in this class a month and I have never programmed before. Thank you both so much for your help!

DefaultName
Oct3-07, 10:19 PM
If you divide using int/floats, the result value will be truncated.

NDiggity
Oct3-07, 10:23 PM
If you divide using int/floats, the result value will be truncated.
I changed the numberOfMoney variables all to int and it still says 2 pennies when i say the total cost is 19.99

DefaultName
Oct3-07, 10:28 PM
I'm assuming numberOfMoney refers to each of the variables that represent a 10, 5, 1 dollar bills.

If you change those to ints, how are you going to get your change (in cents) to work out? Or am I misinterpreting your post above?

DefaultName
Oct3-07, 10:40 PM
http://rafb.net/p/KiGOga41.html Try that - that should work.

Notice what I did - there was no need of that while loop when adding up the remaining pennies. Once the change is STRICTLY less than 5 cents, you just add the remainder change (whether it is 4, 3, 2, or 1 cent) to the # of pennies needed.

Also, I don't know if you know, but for shorthand, you can do:

change -= 1; // eq A
change = change - 1; // eq B

eq A and eq B are completely identical, they preform the same mathematical computation. It may seem easier/quicker to type eq A, especially if you are going to be doing things over and over.

x++;

is equivalent to

x = x + 1;

which is also the same as

x += 1;

Hurkyl
Oct3-07, 10:49 PM
Having 0.02, or even 0.0199999998 pennies isn't very useful.




As I was saying, the problem is that you are doing approximate arithmetic. If you want exact answers, you need to either:

(1) Find a way to detect and correct approximation error.
(2) Find a different method that uses only exact arithmetic.

NDiggity
Oct3-07, 11:02 PM
http://rafb.net/p/KiGOga41.html Try that - that should work.

Notice what I did - there was no need of that while loop when adding up the remaining pennies. Once the change is STRICTLY less than 5 cents, you just add the remainder change (whether it is 4, 3, 2, or 1 cent) to the # of pennies needed.

Also, I don't know if you know, but for shorthand, you can do:

change -= 1; // eq A
change = change - 1; // eq B

eq A and eq B are completely identical, they preform the same mathematical computation. It may seem easier/quicker to type eq A, especially if you are going to be doing things over and over.

x++;

is equivalent to

x = x + 1;

which is also the same as

x += 1;

Thank you so much for your help! Also, thank you for teaching me those useful shortcuts, we have not gone over any shortcuts so far in my comp sci classes. Again, thanks for your patience and help!

NDiggity
Oct3-07, 11:14 PM
My very last question. So for the
if (change <= .05)
numberOfPennies += change;
that gives me numberOfPennies: .01 or something so i just times it by one hundred and it works perfect. But what does that double do at the start instead of float? Without it, I got like 1.0002 pennies, but with it, I get 1 which is what I want.

Hurkyl
Oct3-07, 11:18 PM
It means to use more precision in your floating point numbers.


Incidentally, you don't get 1, even with the doubles; by default, floating point numbers are rounded to 6 decimal digits when printed. Try doing the following:

Add the line

#include <iomanip>
at the top, and then do your printing with


cout << "Number of Pennies: " << setprecision(12) << numberOfPennies << endl;

NDiggity
Oct3-07, 11:26 PM
It means to use more precision in your floating point numbers.


Incidentally, you don't get 1, even with the doubles; by default, floating point numbers are rounded to 6 decimal digits when printed. Try doing the following:

Add the line

#include <iomanip>
at the top, and then do your printing with


cout << "Number of Pennies: " << setprecision(12) << numberOfPennies << endl;

Thank you for that, but with that double, for some reason, it only gives me whole numbers for numberOfPennies, which is what i want. So if the change is .01, it says number of pennies: 1. I dunno know, but it works! I'll try your way tho, thank you for your help!

Hurkyl
Oct3-07, 11:32 PM
I just tried it out; a precision of 12 isn't enough to see the error, but 16 is.


I think you misunderstood me, so let me say it again.

You did not get an exact integral number; you only think you did, because when you displayed numberOfPennies, it gets rounded to 6 decimal digits. I wanted you to run it with setprecision so that you can see for yourself that there really is an error there.



Oh, by the way, try entering a purchace price of 19.95...

19.85 is another good one to try...

DefaultName
Oct4-07, 06:33 AM
Thank you so much for your help! Also, thank you for teaching me those useful shortcuts, we have not gone over any shortcuts so far in my comp sci classes. Again, thanks for your patience and help!

Sure, hopefully you'll teacher will notice and be like woah this kid is genius!!

NDiggity
Oct4-07, 09:45 AM
Haha. There is one other problem. If the purchase is like 15.90, instead of getting a dime back, it gives a nickel and 5 pennies...that's correct change, but were supposed to give the smallest amount of coins possible.

NDiggity
Oct4-07, 06:48 PM
I fixed all the problems! I multipiled all the numbers by 100. That way, a penny is 1 so there is no rounding. Yay! Also, all those useful tips you taught me, we learnt them in class today, so they won't think im some sort of genious :cry: :rofl: