Date post: | 01-Jun-2018 |
Category: |
Documents |
Upload: | braulio-gutierrez-pari |
View: | 237 times |
Download: | 1 times |
of 15
8/9/2019 HP Prime Programming Tutorial ABC
1/15
HP Prime Programming Tutorial #1: LOCAL, RETURNOver the next month, maye month an! a hal", $lan to $o%t $rogramming tutorial% "or the HP&He'lett Pa()ar!* Prime+
" you have $rogramme! 'ith the HP -., /g, or /gii, thi% language 'ill e %imilar to tho%e+
The $rogramming language "or the Prime i% name! the HP Prime Programming Language&HPPP*+
Throughout thi% tutorial, am going to u%e the late%t ver%ion o" the %o"t'are+
Ho' to %tart 'riting a $rogram:
1+ Pre%% 0hi"t 1 &Program*+2+ Pre%% Ne'+ t i% the %e(on! tou(h )ey++ Enter the name o" the $rogram+ Pre%%ing the ALPHA )ey t'i(e 'ill turn on UPPERCA0E 3LPHA4LOC5+ Pre%%ing ALPHA, 0hi"t, ALPHA 'ill turn on lo'er(a%e al$ha4lo()+ To exit anylo(), $re%% the ALPHA )ey one more time+ 6hen ha$$y 'ith the name, $re%% Enter+
8/9/2019 HP Prime Programming Tutorial ABC
2/15
Rule% "or Program Name%:
1+ Letter%, numer%, an! the un!er%(ore (hara(ter &7* only+
2+ The $rogram name mu%t %tart 'ith a letter+
0tru(ture o" a HP Prime Program
A HPPP $rogram i% en(a%e! o" an E8PORT 4 9E.N 4 EN %tru(ture+ The layout i% generallyli)e thi%:
EXPORT program_name(arguments)
BEGIN
commands and comments go here
END;
Ea(h line (ontaining a (omman! generally mu%t en! 'ith a %emi(olon &;*+ A %emi(olon (an yty$e y $re%%ing ALPHA then the Plu% )ey & *+
Comment% (an e ty$e!+ The are !e%ignate! y t'o "or'ar! %la%he%+ The %la%he% are ty$e! y$re%%ing the ivi!e )ey & < *+ Anything in the line "ollo'ing the t'o %la%he% i% ignore! in runningthe $rogram+
0=N
Our "ir%t $rogram i% 0=N, e(au%e >Hello 6orl!> $rogram% are %o 2???%+ 0=N ta)e% anumer, %@uare% it, then (al(ulate% the re(i$ro(al+ n %hort 'e are !e"ining a (u%tom "un(tion:
0=N&x* 1Bx2
Comman!%:
RETURN: return% a re%ult to the %ta() &home $age*+ Dou (an return numer%, li%t%, ve(tor%,
8/9/2019 HP Prime Programming Tutorial ABC
3/15
matri(e%, %tring%, or a (omination o" the%e time%+ A((e%%: Tm$lt, 1+ 9lo(), 2+ RETURN
All the $rogram (o!e in thi% tutorial %erie% 'ill e %ho'n in Courier "ont+
EXPORT SQIN(X)
BEGINRETURN 1/X^2;
END;
Ti$: Dou (an (he() the %yntax o" the $rogram u%t y $re%%ing the Che() %o"t )ey in the $rograme!itor+ HP Prime 'ill in"orm you i" there i% a %yntax error an! attem$t to $oint you to the error+ "there are no %yntax error%, the Prime %tate% >No error% in the $rogram>+ u%e the Che()(omman! all the time+
Ho' to run the $rogram%:
Home Mode - Textbook Entry,Home Mode - Algebraic Entry,CAS Mode:
Ty$e the $rogram name+ Follo' the name 'ith $arenthe%i% an! en(lo%e the re@uire!argument%+
Or u%e the Toolox &to$ ro' o" 'hite )ey%, 2n! )ey "rom the le"t, it loo)% li)e a tool ox*, %ele(tthe U%er tou(h )ey, %ele(t the $rogram, an! in$ut the re@uire! argument%+
Home Mode - RPN Entry:
Enter ea(h argument, %e$arate ea(h entry y $re%%ing the Enter )ey+ Ty$e the name, an! in the$arenthe%i% %tate the numer o" argument%+
For exam$le, i" the $rogram TE0T ha% "our argument%, the RPN %ta() 'oul! li)e thi%:
G: argument71: argument722: argument71: argument7GTE0T&G* to run the $rogram+
Exam$le% to try 'ith 0=N:
0=N&* return% +?G0=N&I* return% +???JJ1I?G/-
The next $rogram 'ill !emon%trate the (on(e$t o" lo(al variale%+
KOPKT
!CA: e(lare% any variale% to e lo(al to the $rogram+ n other 'or!%, the variale% are(reate!, u%e!, $o%%ily !i%$laye! !uring $rogram exe(ution, an! !elete! at $rogram
termination+ A((e%%: Tm$lt, G+ ariale, 1+ LOCAL
8/9/2019 HP Prime Programming Tutorial ABC
4/15
Ti$: Dou (an !e(lare lo(al variale% an! a%%ign an initial value at the %ame time+ For exam$le:LOCAL 5:1; %tore% 1 in 5 an! ma)e% 5 a lo(al variale+
KOPKT (al(ulate% the monthly $ayment o" a loan+ The argument% are: the loan amount &L*, the
intere%t rate &R*, an! the numer o" month% &K*+
EXPORT MOPMT(L,R,M)
BEGIN
LOCAL K:=R/1200;
K:=L*K/(1-(1+K)^-M);
RETURN "Payment ="+K;
END;
Ti$: U%e RETURN, TE8TOUT7P, an! PRNT to return (u%tom %tring%, 'hi(h (omine re%ult%,me%%age%, an! (al(ulation%+ Part% are (onne(te! 'ith a $lu% %ign+
Exam$le%:KOPKT&G???, /+, ?* return% 1?+1JGJIKOPKT&J????, +, I?* return% 1II1+GIG-
Try thi% an! next time in the %erie% 'ill highlight other thing% 'e (an !o 'ith HPPP+ Than)%M
E!!ie
Thi% log i% $ro$erty o" E!'ar! 0hore+ 2?1
8/9/2019 HP Prime Programming Tutorial ABC
5/15
HP Prime Programming Tutorial #2: K0.9O8, F4THEN4EL0E, PRNT, FOR
6el(ome to another $rogramming tutorial "or the HP Prime+ n thi% %e%%ion, 'e 'ill (overK0.9O8, F4THEN4EL0E, PRNT, an! the FOR loo$+
K0.9O8
MS"#!$: K0.O8 ta)e% a %tring a ma)e% a $o$4u$ me%%age ox+ Program exe(ution %to$%until you $re%% a )ey to a()no'le!ge the me%%age+ A((e%%: Cm!%, I+ BO, -+ K0.9O8
The $rogram COKLOC5: magine that you are in (harge o" %etting the (omination% "or thegoo!, ol!4%(hool (omination lo()%+ Thi% $rogram give% three !igit (omination% through theu%e o" K0.9O8+
EXPORT COMLOCK()
BEGIN
LOCAL L0;
L0:=RANDINT(3,0,39);**
MSGBOX("SECRET: "+L0(1)+","+L0(2)+","+L0(3));
END;
Than)% to Thoma% La)e "or $ointing out my ty$o+ A$ologie% "or any in(onvenien(e 4 E!!ie&B21B2?1G*
Other (omman!% that are "eature!:
RANNT&n, a, * generate% a li%t o" n integer% et'een a an! + Dou (an leave n out i" you!e%ire a %ingle ran!om integer+ Pi()% may e re$eate!+
The HP Prime% !e"ault li%t variale% are !e%ignate! L? through L/+
Here i% a %am$le out$ut "or COKLOC5:
8/9/2019 HP Prime Programming Tutorial ABC
6/15
F4THEN4EL0E
%&-THEN-ESE: Program %tru(ture:IF condition THEN
do if the condition is true;
ELSE
do if the condition is false;
END;
A((e%%: Tm$lt, 2+ 9ran(h, 2+ F THEN EL0E
Ti$: Dou (an leave out the EL0E $art i" you only 'ant to te%t to %ee i" a (on!ition i% true+ A((e%%the %im$le F4THEN %tru(ture y $re%%ing Tm$lt, 2+ 9ran(h, 1+ F THEN+
A((e%% , Q, , et(+ y $re%%ing 0hi"t, I+ Note that the !oule e@ual% i% nee!e! to (he()e@uality+
PRNT
PR%NT: The PRNT (omman! $rint% a %ting, re%ult, or a (omination o" oth onto the Prime%Terminal %(reen+ " PRNT i% u%e!, the $rogram 'ill en! on the terminal &text out$ut* %(reen+
Pre%% a utton to exit+
8/9/2019 HP Prime Programming Tutorial ABC
7/15
Dou (an a((e%% the terminal %(reen at any time y $re%%ing the ON utton, hol!ing it, an! then$re%%ing the ivi!e & < * utton+
A((e%%: Cm!%, I+ BO, /+ PRNT
Ti$: To (lear the terminal %(reen, ty$e PRNT&*+ Thi% i% a goo! 'ay to (lear the terminal %(reen
an! u%ually u%e thi% at the eginning o" any $rogram i" PRNT i% going to e u%e! later on+
The $rogram =ROOT0 &yet one more @ua!rati( %olver, %orry "or not eing original guy% an!gal%*, !emon%trate% the u%e o" F4THEN4EL0E an! PRNT+
Here %et the %etting variale HCom$lex to 1, 'hi(h allo'% "or (om$lex numer re%ult%+
EXPORT QROOTS(A,B,C)
BEGIN
LOCAL D;
PRINT();
HComplex:=1;
D:=B^2-4*A*C;
IF D?0 THEN
PRINT("Roots are real.");
ELSE
PRINT("Roots are complex.");
END;
PRINT((-B+?D)/(2*A));
PRINT((-B-?D)/(2*A));
END;
Exam$le%:
=ROOT0&1,,-* return%:
Root% are (om$lex+42+1+22-JIi42+41+22-JIi
=ROOT0&2,4G,4-* return%:
Root% are real++2I?IJ/JJ41+2I?IJ/JJ
FOR
Thi% %e(tion 'ill ex$lore the a%i( &!R %tru(ture:
FOR variable FROM start TO end DO
commands;
END;
All the (omman!% in the loo$ 'ill e exe(ute! a %et numer o" time%+ Ea(h time a loo$ "ini%he%,the variable in(rea%e% y one+ The loo$ terminate% 'hen variableend +
A((e%%: Tm$lt, + LOOP, 1+ FOR
8/9/2019 HP Prime Programming Tutorial ABC
8/15
The $rogram 0UK ta)e% any integer an! a!!% u$ the %um o" it% !ivi%or%+ For exam$le, the!ivi%or% o" 12 are 1, 12, 2, , G, an! I+ The %um i% 2-+
Feature! Comman!% in 0UK:
idi'i(: i!ivi%&integer * return% a %e@uen(e o" all o" the !ivi%or% i" integer + A((e%%: Toolox, CA0,+ nteger, 1+ ivi%or%
Any CA0 (omman! u%e! in $rogramming 'ill e $re(e!e! y >CA0+> Not all CA0 (omman!%(an e u%e! in HP Prime $rogramming at thi% time+
)%M: return% the !imen%ion% o" a %e@uen(e, %tring, or matrix+ K mu%t e u%e! in%tea! o" 0Eto $revent a 9a! Argument error+
For %e@uen(e% or ve(tor%, K return% the length in a li%t Slength+For %tring%, K return% length a% a numer+For matri(e%, K return% the li%t Snumer o" ro'%, numer o" (olumn%+
A((e%%: Cm!%, 1+ 0tring%, /+ K
The $rogram:
EXPORT SUMDIV(N)
BEGIN
LOCAL S:=0,K,mdiv,ldiv;
mdiv:=CAS.idivis(N);
ldiv:=DIM(mdiv);
FOR K FROM 1 TO ldiv(1) DO
S:=S+mdiv(K);
END;
RETURN S;
END;
Than)% to Thoma% La)e "or $ointing out that the variale >mat>, 'hi(h ha! in thi% $rogram'a% unne(e%%ary+ 4 E!!ie B21B2?1
Exam$le%:0UK&12* return% 2-+0UK&2G* return% I?+0UK&-* return% 1?-+
Thi% (on(lu!e% thi% $art o" the HP Prime Programming Tutorial+
8/9/2019 HP Prime Programming Tutorial ABC
9/15
HP Prime Programming Tutorial #: 6HLE, NPUT, 5LL,REPEAT, .ET5ED
Thi% tutorial i% going to (over a lot, ea(h 'ith %ome ne' $rogramming (omman!% in thi% %erie%+ ho$e you are rea!y "or the inten%ity+ :*
6HLE, NPUT, 5LL
HP Prime Program: TAR.ET+ TAR.ET i% a game 'here you $rovi!e a gue%% to get a !e%ire!numer+ " you mi%%, the (al(ulator 'ill tell you i" numer i% higher an! lo'er+ At the en! o" thegame, the (al(ulator give% you ho' may $i()% you nee!e! to get the target numer+
*H%E: Re$eat a numer o" (omman!% 'hile a %$e(i"i( (on!ition i% te%t+
WHILE condition is true DO
commands
END;
A((e%%: Tm$lt, + Loo$, + 6HLE
Caution: 6at(h your EN%M Ka)e %ure an EN i% 'ith ea(h loo$ an! the $rogram it%el"+ Pre%%the %o"t )ey Che() to (he() your 'or)+
%NPUT: Create% an in$ut %(reen "or variale%+ On the HP Prime, the in$ut (an a%)e! "or morethan one in$ut+ TAR.ET !emon%trate% NPUT 'ith one $rom$t+
One ariale:INPUT(variable, "title", "label", "help text")
Kulti4ariale:INPUT(list of variables, "title", list of "labels", list of "help
text")
Note: Pre%%ing Can(el 'ill %tore a ? in variale+ Dou may in(lu!e (o!e o" 'hat to !o i" the u%er$re%%e% Can(el, ut it i% not re@uire!+
A((e%%: Cm!%, I+ BO, + NPUT
+%: Terminate% $rogram exe(ution+ Nothing !ie%, $romi%e+
A((e%%: Tm$lt+ 1+ 9lo(), + 5LL
Program:EXPORT TARGET()
BEGIN
LOCAL C:=0, N:=RANDINT(1,20), G:=-1;
WHILE G?N DO
C:=C+1;
INPUT(G,"Guess?","GUESS:","1 - 20");
IF G==0 THEN
8/9/2019 HP Prime Programming Tutorial ABC
10/15
KILL;
END;
IF G < N THEN
MSGBOX("Higher");
END;
IF G > N THEN
MSGBOX("Lower");
END;
END;
MSGBOX("Correct! Score: "+C);
END;
Try it an! o" (our%e, you (an a!u%t the higher limit+ Here i% %ome thing "or you to try 'ithTAR.ET:
1+ A!! a limite! amount o" gue%%e%+2+ Can you !i%$lay the li%t o" gue%%e%
REPEAT
ULAK Algorithm: ta)e an integer n+ " n i% even, !ivi!e it y 2+ " n i% o!!, multi$ly it y an! a!!1+ ULAK (ount% ho' many %te$% it ta)e% to get n to 1+
REPEAT:
A((e%%: Tm$lt, + Loo$, I+ REPEAT
Feature!:C!NCATli(t, li(t./: Kel!% li%t1 an! li%t2 into one+
A((e%%: Toolox, Kath, I+ Li%t, G+ Con(atenate
EXPORT ULAM(N)
BEGIN
LOCAL C:=1, L0:={N};
REPEAT
IF FP(N/2)==0 THEN
N:=N/2;
ELSE
N:=3*N+1;
END;
C:=C+1;
L0:=CONCAT(L0,{N});
UNTIL N==1;
MSGBOX("NO. OF STEPS="+C);
RETURN L0;END;
8/9/2019 HP Prime Programming Tutorial ABC
11/15
Exam$le%:
ULAK&* return%:Ke%%age 9ox: >NO+ OF 0TEP0I>Li%t: S, 1I, -, G, 2, 1
ULAK&22* return%:Ke%%age 9ox: >NO+ OF 0TEP01I>Li%t: S22, 11, G, 1J, 2, 2I, 1, G?, 2?, 1?, , 1I, -, G, 2, 1
.ET5ED
The next %e(tion 'ill intro!u(e a %u$er4im$ortant (omman!, .ET5ED+ 6e 'ill e 'or)ing 'ith.ET5ED over the entire %erie%+
The Program 5EDNO: The $er%on $re%%e% )ey $re%%e%+ 6hi(h ea(h )ey $re%%, the (o!e
return% to the terminal %(reen+ The $rogram terminate% 'hen the Enter )ey i% $re%%e!+
"ET+E0: Return% the )ey (o!e o" la%t )ey $re%%e!+ The Prime% )ey ma$ i% elo'+ &Pi(ture i%"rom the HP Prime U%er% .ui!e*
A((e%%: Cm!%, I+ BO, G+ .ET5ED
EXPORT KEYNO()
BEGIN
LOCAL K;
PRINT();
PRINT("Press any key to get its code.");
PRINT("Press Enter to exit.");
REPEAT
K:=GETKEY;
IF K ? 0 THENPRINT(K);
END;
8/9/2019 HP Prime Programming Tutorial ABC
12/15
UNTIL K==30;
END;
Exam$le 5ey Co!e%:
: - )ey2: u$J: le"t-: right12: !o'n?: $lu%G: minu%
Thi% (on(lu!e% Part + Again, it (ant e %ai! enough, than)% "or all the (omment% an!(om$liment%+ An! until next time,
8/9/2019 HP Prime Programming Tutorial ABC
13/15
HP Prime Programming Tutorial #G: CHOO0E an! CA0E,Ti$ aout NPUT
6el(ome to Part G o" our $rogramming %erie% "or the Prime+ To!ay% %e%%ion 'ill (overCHOO0E an! CA0E+
Fir%t a ti$ "rom Han o" the KoHPC Forum, 'hi(h i% "oun! at htt$:BB'''+h$mu%eum+orgB(gi4%y%B(gi'ra$Bh$mu%eumB"orum+(gi#2?-G + Than) you Han "or allo'ing me to %hare thi%+
U%e the F THEN EL0E %tru(ture 'ith NPUT to exe(ute a %et o" !e"ault in%tru(tion% i" the u%er$re%%e% (an(el+ NPUT return% a value o" ? i" E0C or (an(el i% $re%%e!, an! 1 i" a value i%
entere!+
IF INPUT(...) THEN
commands if values are entered
ELSE
commands if Cancel is pressed
END;
e"ault value% (an e a%%igne! to value% a% an o$tional "i"th argument "or NPUT+
INPUT(var, "Title", "Prompt", "Help", default value)
The ty$e o" variale maye %et to other than real numer%+ Vu%t rememer to %tore %u(h ty$ee"ore the NPUT (omman!+ For exam$le, i" you 'ant var to e a %tring, %tore an em$ty %tring:
var:=" ";
Again, maor than)% to Han+
CHOO0E an! CA0E
CH!!SE: Create% a $o$ u$ (hoo%e ox, %imilar to 'hat you %ee 'hen you (li() on a %o"tmenu+ There are t'o %yntaxe% "or CHOO0E:
0im$le 0yntax &u$ to 1G o$tion%*:
CHOO0E&var, >title %tring>, >item 1>, >item 2>, +++ , >item n>*;
Li%t %yntax &in"inite amount o" item%*:CHOO0E&var, >title %tring>, S>item 1>, >item 2>*;
Choo%ing item 1 a%%ign% the value o" 1 to var, (hoo%ing item 2 a%%ign% the value o" 2 to var+
A((e%%: Cm!%, I+ BO, 1+ CHOO0E
CA0E: Allo'% "or !i""erent te%t (a%e% "or one variale+ Al%o in(lu!e% a !e"ault %(enario&o$tional*+
CASEIF test 1 THEN do if true END;
http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi#255084http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi#255084http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi#255084http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi#255084http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi#255084
8/9/2019 HP Prime Programming Tutorial ABC
14/15
IF test 2 THEN do if true END;
...
DEFAULT commands END;
A((e%%: Cm!%, 2+ 9ran(h, + CA0E
Let% loo) at t'o $rogram% to !emon%trate oth CHOO0E an! CA0E+
TERKEL 4 Terminal elo(ity o" an Oe(t
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;
Exam$le%:
0$here, 0 Unit%, K +? )g, A +??2- m2Terminal elo(ity: T 2G+IIG?GJ-J mB%
Cue, U0 Unit%, K 1+2 l, A +G? "t2Terminal elo(ity: T +1G/-212?/ "tB%
AREAC 4 Area o" Cir(le%, Ring%, an! 0e(tor%
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
8/9/2019 HP Prime Programming Tutorial ABC
15/15
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;
Exam$le%
R 2+, r 1+, W XBG ra!ian% or GY
Cir(le: 1/+IG/G?-G/Ring: 12+IIJ?I1GG0e(tor: 2+GGI/2I?I2
That i% ho', in general CHOO0E an! CA0E 'or)+ than) you a% al'ay%+ t i% %o goo! to "inallye ri! o" a (ol! an! "iring on all (ylin!er% again+