Source:
Browne, Michael E. Ph. D "Schaum's Outlines: Physics for Engineering and Science" 2nd. Ed McGraw Hill: New York. 2010.
HP Prime: INERTIA
This program demonstrates the use of CHOOSE and CASE
EXPORT INERTIA( )
BEGIN
// EWS 2014-06-24
LOCAL c,a,b,m,r;
CHOOSE(c, "Moment of Inertia",
{"Square", "Rectangle", "Cylinder", "Sphere", "Hoop"});
CASE
IF c==1 THEN INPUT({m,a});
RETURN m*a^2/6; END;
IF c==2 THEN INPUT({m,a,b});
RETURN m*(a^2+b^2)/12; END;
IF c==3 THEN INPUT({m,r});
RETURN m*r^2/2; END;
IF c==4 THEN INPUT({m,r});
RETURN 2*m*r^2/5; END;
IF c==5 THEN INPUT({m,r});
RETURN m*r^2; END;
DEFAULT KILL; END;
END;
HP 50g: INERTIA
6/23/2014
<< 1 1 1 1 1
→ c a b m r
<< "Moment of Inertia"
{ { "Square" 1 }
{ "Rectangle" 2 }
{ "Cylinder" 3 }
{ "Sphere" 4 }
{ "Hoop" 5 } }
1 CHOOSE
IF 0 == THEN KILL END
'c' STO
CASE c 1 == THEN
"m" PROMPT "a" PROMPT
SQ * 6 / END
c 2 == THEN
"m" PROMPT "a" PROMPT SQ
"b" PROMPT SQ + * 12 / END
c 3 == THEN
"m" PROMPT "r" PROMPT SQ *
2 / END
c 4 == THEN
"m" PROMPT "r" PROMPT SQ * 2 *
5 / END
c 5 == THEN
"m" PROMPT "r" PROMPT SQ *
END
END >> >>
Notes:
* I just stored a value in the variables c, a, b, m, and r, and used the right arrow ( → ) to make the variables local.
* The IF 0 == THEN KILL END sequence handles the case the user chooses CANCEL over its options.
* The consecutive PROMPT commands saved space instead of using INFORM.
* INERTIA demonstrates the CASE structure
Eddie
This blog is property of Edward Shore. 2014
Monday, June 23, 2014
HP Prime and HP 50g: Moments of Inertia
Wednesday, November 13, 2013
HP Prime Programming Tutorial #4: CHOOSE and CASE, Tip about INPUT
Welcome to Part 4 of our programming series for the Prime. Today's session will cover CHOOSE and CASE.
First a tip from Han of the MoHPC Forum, which is found at http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi#255084. Thank you Han for allowing me to share this.
Use the IF THEN ELSE structure with INPUT to execute a set of default instructions if the user presses cancel. INPUT returns a value of 0 if ESC or cancel is pressed, and 1 if a value is entered.
IF INPUT(...) THEN
commands if values are entered
ELSE
commands if Cancel is pressed
END;
Default values can be assigned to values as an optional fifth argument for INPUT.
INPUT(var, "Title", "Prompt", "Help", default value)
The type of variable maybe set to other than real numbers. Just remember to store such type before the INPUT command. For example, if you want var to be a string, store an empty string:
var:=" ";
Again, major thanks to Han.
CHOOSE and CASE
CHOOSE: Creates a pop up choose box, similar to what you see when you click on a soft menu. There are two syntaxes for CHOOSE:
Simple Syntax (up to 14 options):
CHOOSE(var, "title string", "item 1", "item 2", ... , "item n");
List syntax (infinite amount of items):
CHOOSE(var, "title string", {"item 1", "item 2"});
Choosing item 1 assigns the value of 1 to var, choosing item 2 assigns the value of 2 to var.
Access: Cmds, 6. I/O, 1. CHOOSE
CASE: Allows for different test cases for one variable. Also includes a default scenario (optional).
CASE
IF test 1 THEN do if true END;
IF test 2 THEN do if true END;
...
DEFAULT commands END;
Access: Cmds, 2. Branch, 3. CASE
Let's look at two programs to demonstrate both CHOOSE and CASE.
TERMVEL - Terminal Velocity of an Object
EXPORT TERMVEL()
BEGIN
LOCAL L0:={9.80665,32.174},
L1:={1.225,.0765},
L2:={.47,1.05,1.15,.04},C,K,M,A,T;
CHOOSE(C,"Units","SI","English");
CHOOSE(K,"Type of Object","Sphere","Cube",
"Cylinder","Tear-Shaped");
INPUT({M,A},"Object",
{"M=","A="},{"Mass","Surface Area"});
T:=√((2*M*L0(C))/(L1(C)*A*L2(K)));
MSGBOX("Terminal Velocity="+T);
RETURN T;
END;
Examples:
Sphere, SI Units, M = .05 kg, A = .0028 m^2
Terminal Velocity: T = 24.6640475387 m/s
Cube, US Units, M = 1.2 lb, A = .3403 ft^2
Terminal Velocity: T = 53.149821209 ft/s
AREAC - Area of Circles, Rings, and Sectors
EXPORT AREAC()
BEGIN
LOCAL C,R,S,θ,A;
CHOOSE(C,"Areas","1. Circle","2. Ring","3. Sector");
INPUT(R, "Input Radius", "R =");
CASE
IF C==1 THEN A:=π*R^2; END;
IF C==2 THEN
INPUT(S,"Small Radius","r=");
A:=π*(R^2-S^2);
END;
IF C==3
INPUT(θ, "Angle", "θ=");
\\ Assume you are in the correct angle mode
IF HAngle==1 THEN
\\ Test Angle Mode
θ:=θ*π/180;
END;
A:=θ*R^2/2;
END;
END;
MSGBOX("Area is "+A);
RETURN A;
END;
Examples
R = 2.5, r = 1.5, θ = π/4 radians or 45°
Circle: 19.6349540849
Ring: 12.5663706144
Sector: 2.45436926062
That is how, in general CHOOSE and CASE work. I thank you as always. It is so good to finally be rid of a cold and firing on all cylinders again.
Eddie
This blog is property of Edward Shore. 2013
Tuesday, October 18, 2011
RPL Programming Tutorial - Part 11 - HP 49g+/50g: The CASE Structure and Labeling Results
In this case, do this; in that case, do that...
Part 11 of our tutorial will cover the CASE structure, which is really the IF-THEN-ELSE-END structure repeated more than one time.
The CASE structure looks like this:
CASE test_1 THEN commands if test_1 is true END
test_2 THEN commands if test_2 is true END
test_3 THEN commands if test_3 is true END
...
commands if none of the tests are true (default, optional) END
The default commands are optional. If there is no default commands, the structure ends without any action. Note that CASE is only needed once, and there must always be a final END to this structure. CASE is found in the PRG-BRCH menu.
Key [LS] [F2] (CASE) to get:
CASE
THEN
END
END
Use this key sequence as a starter.
Key [RS] [F2] (CASE) to get:
THEN
END
Use this key sequence to add additional cases. Remember only one CASE is needed per structure.
Labeling Results
Sometimes we want to label results. One way to label results is the relatively easy command →TAG. This command can be accessed by the keystroke sequence:
[LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)
→TAG takes the result from Level 2 and a string (your label) from Level 1. An example would be:
2: 3.14159265359
1: "PI"
Executing →TAG returns:
1: PI:3.14159265359
→TAG automatically adds a colon ( : ) to the label. A tagged object (number) can be used in any calculation like normal. However once a calculation is executed, the tag is lost.
What's my tax?
To illustrate the CASE structure, let's analyze and calculate how much income tax liability taxpayers have in a given state. The state takes the taxable income of a taxpayer and uses this graduated schedule to determine payment:
$0 - $14,999: 5% of taxable income
$15,000 - $99,999: 7% of taxable income exceeding $14,999 plus $1,050
$100,000 and above: 9% of taxable income exceeding $99,999 plus $7,000
Here the tax preparer wants to know how much is the tax liability. In addition, the preparer wants to label both the income and tax liability.
The CASE structure is set up like this:
Case 1: If income < 15,000, then calculate income * 5%
Case 2: If 15,000 ≤ income < 100,000, then calculate income above 14,999 * 7% + 1050, We'll break up the condition for Case 2 this way: 15,000 ≤ income AND income < 100,000.
Otherwise: Calculate income above 99,999 * 9% + 7000
The Program INTAX
[RS] [ + ] (<< >>)
[LS] [SYMB] (MTH) [F5] (REAL) 0 [NXT] [NXT] [F1] (RND)
* Rounds the entry to the nearest integer (number 0 RND)
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F2] (CASE)
* The primary CASE-THEN-END-END structure
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
15000 [RS] [big X] ( < )
* First condition: income < 15,000
[ ↓ ] [F1] (DUP) .05 [ x ]
* First calculation
[ ↓ ]
[F1] (DUP) [F1] (DUP) 100000 [RS] [big X] ( < ) [F2] (SWAP) 15000 [LS] [1/X] ( ≥ )
[LS] [EVAL] (PRG) [F4] (TEST) [NXT] [F1] (AND)
[LS] [EVAL] (PRG) [F3] (BRCH) [RS] [F2] (CASE)
* Second condition: 15,000 ≤ income < 100,000 - connect two required conditions with AND
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP) 14999 [ - ] .07 [ x ] 1050 [ + ]
* Second calculation
[F1] (DUP) 99999 [ - ] .09 [ x ] 7000 [ + ]
* Default calculation
[ ↓ ]
* Exits the CASE structure
0 [LS] [SYMB] (MTH) [F5] (REAL) [NXT] [NXT] [F1] (RND)
* Rounds the final result to the nearest integer
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [COS] (T) [F1] (A) [big X] [ALPHA] [ → ]
[LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)
* Apply the tag on the income tax.
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP)
[RS] [ x ] [ALPHA] [ALPHA] [TOOL] (I) [EVAL] (N) [F3] (C) [ ' ] (O) [HIST] (M) [F5] (E) [ALPHA] [ → ] [LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)
* Apply the tag on the income.
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP) [ENTER]
* Finish the program
[ ' ] [ALPHA] [ALPHA] [TOOL] (I) [EVAL] (N) [COS] (T) [F1] (A) [big X] [ENTER] [STO>]
The completed program:
<< 0 RND
CASE DUP 15000 < THEN DUP .05 * END
DUP DUP 100000 < SWAP 15000 ≥ AND THEN
DUP 14999 - .07 * 1050 + END
DUP 99999 - .09 * 7000 + END
0 RND "TAX" →TAG SWAP "INCOME" →TAG SWAP >>
Instructions:
1. Enter the taxable income.
2. Run INTAX
Examples:
1. Income: $13,000; Tax: $650
2. Income: $15,000; Tax: $1,050
3. Income: $50,000; Tax: $3,500
4. Income: $100,000; Tax: $7,000
5. Income: $150,000; Tax: $11,500
This wraps it up for Part 11 - on to Part 12 where we work with a subroutine. Until next time, Eddie.
This tutorial is property of Edward Shore. Mass distribution and reproduction requires express permission of the author.
Python in Numworks: Duplicating and Grayscale
Python in Numworks: Duplicating and Grayscale All three scripts presented today use the math, random, and the Numworks specific ...