Showing posts with label CHOOSE. Show all posts
Showing posts with label CHOOSE. Show all posts

Saturday, February 13, 2021

HP Prime CHOOSE for Repeated Use - Present Value

 HP Prime CHOOSE for Repeated Use - Present Value


Introduction:  Using the CHOOSE Command to Allow for Repeated Calculations


The use of REPEAT and CHOOSE will allow the programmer to set up a choose from a set of calculations and allows for additional calculations until the user exits the program.  


Here is a sample syntax, with comments followed by double slashes (//):


// Set up local variables:  a choice variable, a list of options, the size of the list

LOCAL ch, lst, s;


// Use a list of options.  Be sure to have the last option as "Exit", "Quit", etc.

lst:={"opiton 1", "option 2", ... , "Exit"};

s:=SIZE(lst);


// Set a choice variable to a starting value. Use a "dummy" value to begin with, like -1.  The choice variable will take on a positive integer value after the CHOOSE command has been executed.

ch:=-1;


// The REPEAT loop

REPEAT

CHOOSE(ch,"title",lst);


IF ch==1 THEN

[choice 1 - calculation]

END;


IF ch==2 THEN

[choice 2 - calculation]

END;


...


IF ch==n-1 THEN

[choice n-1 - calculation]

END;


UNTIL ch==s;   // This is the Exit option


Let's look at an example program.



HP Prime Program: PVACT


*  This is an example program that uses CHOOSE to allow for repeated calculations.   The application is for several types of present value for several types of annuities.


*  As a bonus, you can save some settings, such as format and number of digits, to variables before switching settings that the program requires.  This allows for the programmer to restore settings at the end of the program.  


EXPORT PVACT()

BEGIN

// EWS 2021-01-10

// CHOOSE demonstration

// loop until user exits


// Grab the settings

LOCAL h1,h2;

h1:=HFormat;

h2:=HDigits;


// ch choice varaible

LOCAL ch,n,p,r,m,w,v;

LOCAL lst,s,g;


// Set a "dummy" initial choice

// for ch

ch:=−1;


// Set a list of choices

lst:={"Constant Annuity",

"Growing Annuity",

"Perpetual Annuity",

"Exit"};

s:=SIZE(lst);


// Set decimal fix to 2 places

HFormat:=1;

HDigits:=2;


// Use a Repeat loop to allow

// more than one calculation

REPEAT


CHOOSE(ch,"Present Value",lst);

 

// Constant Annuity - Choice 1

IF ch==1 THEN

INPUT({n,r,m},"Constant Annuity",

{"n?  ","i%? ","PMT? "},

{"number of payments",

"periodic interest rate",

"payment"});

v:=Finance.TvmPV(n,r,−m,0,1);

PRINT();

PRINT("n = "+n);

PRINT("i% = "+r);

PRINT("PMT = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// Growing Annuity - choice 2

IF ch==2 THEN

INPUT({n,r,g,m},"Growing Annuity",

{"n?  ","i%? ","g%? ","PMT? "},

{"number of payments",

"periodic interest rate",

"payment growth rate",

"base payment"});

w:=(1+0.01*g)/(1+0.01*r);

v:=m/(1+0.01*r)*(1-w^n)/(1-w);

PRINT();

PRINT("n = "+n);

PRINT("i% = "+r);

PRINT("g% = "+g);

PRINT("Base Payment = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// Perpetual Annuity - choice 3

IF ch==3 THEN

INPUT({r,m},"Perpetual Annuity",

{"i%? ","PMT? "},

{"interest rate",

"payment"});

v:=m/(0.01*r);

PRINT();

PRINT("r% = "+r);

PRINT("PMT = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// if Exit is selected

UNTIL ch==s;


// Restore the setting

HFormat:=h1;

HDigits:=h2;

END;



Eddie


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Wednesday, April 4, 2018

HP Prime Programming Tutorial is now available


HP Prime Programming Tutorial is now available

The HP Prime Programming tutorial is a set of twelve tutorials designed to get up and running on HP’s PPL programming language.  Knowledge of the programming language will help you take your HP Prime further.  The topics covered are:

1.  Help Key, Local Variables, RETURN command
2.  Message box, IF-THEN-ELSE structure, FOR loop
3.  WHILE loop, REPEAT loop, Detecting key presses
4.  CHOOSE command (providing a pop-up menu), CASE structure
5.  Colors, Switching Apps
6.  Subroutines
7.  Drawing Graphics, Cartesian vs Pixel Coordinates
8.  Drawing Lines, Polygons, Arcs, Circles
9. Numerical Calculus
10. Using App Functions in Programs
11. Drawing and using a soft menu
12. Creating custom apps

Firmware Version 13441



A little history

If you want to look at my original tutorial series that posted during October through December 2013, the first of the series is here: https://edspi31415.blogspot.com/2013/10/hp-prime-programming-tutorial-1-local.html 

However, the HP Prime’s firmware had a lot of updates since then.  And the PDF file has more topics and is up to date. 

On the date of this entry, this blog is one week away from its 7th Anniversary.  This blog’s “birthday” is April 11.  I want to thank you all for your support and compliments, this is labor of love for. 

Enjoy!

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Monday, June 23, 2014

HP Prime and HP 50g: Moments of Inertia

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

Tuesday, April 1, 2014

HP Prime Video Tutorial: Data Input Loop


Link to video: Link: http://youtu.be/phiY8WMEYF0

I don't know how often I am going to post videos, but I aim to post as many videos as I can.


Eddie

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

Trigonometry Reduction Formula and Solving Simple Arcsine and Arccosine Equations

Trigonometry Reduction Formula and Solving Simple Arcsine and Arccosine Equations Some Background and Periodic Reduction Formulas ...