How to write a simple but robust loop for the following example?
Afficher commentaires plus anciens
Suppose, I have a $100. Every time when I go to the coffee shop I spend 8.3 % of my money. So, at first when I spend 8.3% I will have (100- 0.083*100) = $91.7 remaining. The next day, I want to spend 8.3% of $91.7, after that I will have (91.7 - 0.083*91.7) = $84.0889 remaining, and so on.
So, for how many days I can use my money?
8 commentaires
Stephen23
le 3 Déc 2018
"So, for how many days I can use my money?"
You don't seem to have a lower limit to how much money you need to have/spend, so surely the answer is "forever" ?
Att
le 3 Déc 2018
madhan ravi
le 3 Déc 2018
Did you see my answer?
@madhan: I do not see your answer.
@A T: This sounds like a homework and you can write a small code, which calculates the answer. You just need a while loop and for keeping teh standards fractinal cents are rounded, e.g. by round(x, 2). Please try to write this code. Post it here if there are problems and ask a specific question.
The lower limit is zero? Of cause you can do this an infinite number of times, if you do not consider the rounding to cents. I guess the minimal value is 1 cent.
Att
le 3 Déc 2018
OCDER
le 3 Déc 2018
Oh, the joy of doing homeworks that ask to solve unrealistic problems... Or is this a trick question?
When you have $1 left, you cannot buy anything in most coffee shops. You may get lucky with $2.00 drip coffee. Below that, you'll get a bunch of baristas with puzzled looks, something like this face: o_O?
You may be able to get sample coffee beans or literally, one drip of coffee. Also, prices of coffee do not scale continuously from $8.30 to 0.0001 cent. You're be better off doing the following:
Days of visit = floor($100 / (minimum price of an item in the store))
You should give the homework answer and the real-life answer.
madhan ravi
le 4 Déc 2018
@Jan yes I deleted my answer since it was not the one OP was looking for.
Stephen23
le 4 Déc 2018
"lower limit is zero. Any help?"
Then the answer is trivially easy: you can do this for infinite days, because you will never reach zero using the formula that you have given us.
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 4 Déc 2018
To answer, you need to know the price of the least expensive item in the coffee shop. I'm certain that the coffee shop does not sell anything for $0.01. In fact you might have trouble finding anything less than $0.50.
Anyway, like Jan said, 8.3% of 6 cents is less than half a cent, so that rounds to no money being spent, so you have to quit once you get to 6 cents because the requirement of spending 8.3% can no longer be met.
Here's a hint for a line of code you can put into the loop to print out your balance at each iteration:
fprintf('After %d purchases, you have %f left.\n', iteration, balance);
Unlike Jan, I get to 6 cents at 85 iterations, not 84. Maybe it's because he used floor() while I used round(). I think cash registers actually do use round() in the US. When I traveled in Europe it seemed everything is rounded to the nearest 5 euro cents, though I could be wrong.
5 commentaires
Jan
le 4 Déc 2018
You are right: With floor to cent values the limit of 1 cent is reached after 84 days.
Some European countries decided to avoid the 1 and 2 cent coins and round to the nearest 5c value. The idea is, that this saves time and energy for counting and transporting a large amount of almost non-value heavy coins.
Image Analyst
le 4 Déc 2018
Honestly, I wish the US would get rid of the cent. They cost more to make than they're worth. No one even wants them - people won't even pick one up off the ground if they see one. And in the long run, the money paid will average out to the same amount anyway.
Jan
le 4 Déc 2018
Feel free to send all your cents to me, or to a project, which needs the money more urgently than I do.
OCDER
le 4 Déc 2018
PH = 0.75; % penny diameter
PT = 0.06; % penny thickness
Box = [23+11/16, 11.75, 3];
Grid = floor(Box1./[PH PH PT]);
PennyPerBox = prod(Grid);
PennyValue = PennyPerBox/100;
ShippingCost = 18.90; %FlatRate USPS
MoneySent = PennyValue - Cost;
MassOfBox = PennyPerBox * 2.5 * 0.002204;
%You can send around 23250 pennies for $18.90 via USPS flat rate box, for a
%total of $213.60 monetary value left over. The mailman will not like
%carrying the 128-lb box though...
Jan
le 5 Déc 2018
@OCDER: Thank you. I wil give the postman 2136 cents as a tip.
Catégories
En savoir plus sur Functional Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!