Post Reply 
(12C) Tip Calculation
04-21-2018, 08:53 AM (This post was last modified: 04-21-2018 09:28 AM by Dieter.)
Post: #7
RE: (12C) Tip Calculation
(04-20-2018 10:52 AM)Gamo Wrote:  Dieter Thank You
Now updated the instruction.
Your program is much better don't have to use any register.

It's not magic, that's something you can do as well. There are essentially two things to consider:

1. Think about the problem you want to solve. Think hard. Don't take the first solution you find, there may be better ones. In this case you have written a program that does exactly what you would do if you had to calculate the tip manually: calculate the tip, add it to the bill amount and divide the result by the number of persons. Then calculate the tip again (although you have already done so before) and divide only the tip by the number of persons. Essentially you are doing the whole calculation twice. Does not sound like a good idea.

But there is a better way: you can just as well first divide the bill by the number of persons and then (!) add the tip percentage. Instead of adding 15% to 123,45 and then divide this by 3 (while the "3" is required another time in the next step) you can simply divide 123,45 by 3 (=41,15) and then add 15% of that (=6,17).

2. Think about an effective implementation. If you first divide the bill amout by the number of persons, each of the three input data is only used once. If you have 41,15 in Y and 15 in X, the %-function keeps the 41,15 in Y and returns 6,17 in X. So you only have to add both (=47,32). This again leaves 6,17 in LastX from where it can be recalled afterwards.

This also means that you do not have to store the input in data registers. During the calculation each value is required only once, so you can be sure that it can all be done on the stack:

Code:
T   ?         ?         15        15        15        15        15        15
Z   123,45    123,45    ?         15        ?         ?         15        ?
Y   15        3         123,45    ?         41,15     41,15     ?         47,32
X   3         15        3         41,15     15        6,17      47,32     6,17

    input     X<>Y      R↓        /         R↑        %         +         LastX

Since the 12C has no R↑ you just have to replace it with 3x R↓.

The same idea – first divide the bill amount by the number of persons, then add the tip percentage – can be implemented even shorter on calculators with stack arithmetics, for instance the HP41:

Code:
01 ST/ Z
02 R↓
03 %
04 +
05 LastX

Stack diagram:

Code:
T   ?         ?         3         3         3         3 
Z   123,45    41,15     ?         ?         3         ?
Y   15        15        41,15     41,15     ?         47,32
X   3         3         15        6,17      47,32     6,17

    input     ST/ Z     R↓        %         +         LastX

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(12C) Tip Calculation - Gamo - 10-08-2017, 12:02 PM
RE: (10C) (12C) Tip Calculation - Dieter - 10-08-2017, 06:44 PM
RE: (10C) (12C) Tip Calculation - Gamo - 10-09-2017, 01:05 AM
RE: (10C) (12C) Tip Calculation - Gamo - 04-19-2018, 01:12 PM
RE: (12C) Tip Calculation - Dieter - 04-20-2018, 07:28 AM
RE: (12C) Tip Calculation - Gamo - 04-20-2018, 10:52 AM
RE: (12C) Tip Calculation - Dieter - 04-21-2018 08:53 AM



User(s) browsing this thread: 1 Guest(s)