## Saturday, October 26, 2013

### HP Prime Programming Tutorial #2: MSGBOX, IF-THEN-ELSE, PRINT, FOR

Welcome to another programming tutorial for the HP Prime. In this session, we will cover MSGBOX, IF-THEN-ELSE, PRINT, and the FOR loop.

MSGBOX

MSGBOX: MSGOX takes a string a makes a pop-up message box. Program execution stops until you press a key to acknowledge the message.
Access: Cmds, 6. I/O, 8. MSGBOX

The program COMLOCK: Imagine that you are in charge of setting the combinations for the good, old-school combination locks. This program gives three digit combinations through the use of MSGBOX.

EXPORT COMLOCK()
BEGIN
LOCAL L0;
L0:=RANDINT(3,0,39);**
MSGBOX("SECRET: "+L0(1)+","+L0(2)+","+L0(3));
END;

** Thanks to Thomas Lake for pointing out my typo.  Apologies for any inconvenience - Eddie (3/21/2014)

Other commands that are featured:

RANDINT(n, a, b) generates a list of n integers between a and b. You can leave n out if you desire a single random integer. Picks may be repeated.

The HP Prime's default list variables are designated L0 through L9.

Here is a sample output for COMLOCK:

IF-THEN-ELSE

IF-THEN-ELSE: Program structure:
IF condition THEN
do if the condition is true;
ELSE
do if the condition is false;
END;

Access: Tmplt, 2. Branch, 2. IF THEN ELSE

Tip: You can leave out the ELSE part if you only want to test to see if a condition is true. Access the simple IF-THEN structure by pressing Tmplt, 2. Branch, 1. IF THEN.

Access <, ≤, ==, etc. by pressing Shift, 6. Note that the double equals is needed to check equality.

PRINT

PRINT: The PRINT command prints a sting, result, or a combination of both onto the Prime's Terminal screen. If PRINT is used, the program will end on the terminal (text output) screen. Press a button to exit.

You can access the terminal screen at any time by pressing the ON button, holding it, and then pressing the Divide ( ÷ ) button.

Access: Cmds, 6. I/O, 9. PRINT

Tip: To clear the terminal screen, type PRINT(). This is a good way to clear the terminal screen and I usually use this at the beginning of any program if PRINT is going to be used later on.

The program QROOTS (yet one more quadratic solver, sorry for not being original guys and gals), demonstrates the use of IF-THEN-ELSE and PRINT.

Here I set the setting variable HComplex to 1, which allows for complex number results.

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;

Examples:

QROOTS(1,5,8) returns:

Roots are complex.
-2.5+1.32287565553*i
-2.5-1.32287565553*i

QROOTS(2,-4,-8) returns:

Roots are real.
3.2360679775
-1.2360679775

FOR

This section will explore the basic FOR structure:

FOR variable FROM start TO end DO
commands;
END;

All the commands in the loop will be executed a set number of times. Each time a loop finishes, the variable increases by one. The loop terminates when variable=end.

Access: Tmplt, 3. LOOP, 1. FOR

The program SUMDIV takes any integer and adds up the sum of its divisors. For example, the divisors of 12 are 1, 12, 2, 3, 4, and 6. The sum is 28.

Featured Commands in SUMDIV:

idivis: idivis(integer) returns a sequence of all of the divisors if integer. Access: Toolbox, CAS, 5. Integer, 1. Divisors

Any CAS command used in programming will be preceded by "CAS." Not all CAS commands can be used in HP Prime programming at this time.

DIM: returns the dimensions of a sequence, string, or matrix. DIM must be used instead of SIZE to prevent a Bad Argument error.

For sequences or vectors, DIM returns the length in a list {length}.
For strings, DIM returns length as a number.
For matrices, DIM returns the list {number of rows, number of columns}.

Access: Cmds, 1. Strings, 9. DIM

The program:

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;
** Thanks to Thomas Lake for pointing out that the variable "mat", which I had in this program was unnecessary.  - Eddie 3/21/2013

Examples:
SUMDIV(12) returns 28.
SUMDIV(24) returns 60.
SUMDIV(85) returns 108.

This concludes this part of the HP Prime Programming Tutorial.

HAPPY HALLOWEEN!

Eddie

This blog is property of Edward Shore. 2013

1. Very nice. Thanks. This machine is really growing on me. Very fun.

1. Thanks Alan. Appreciate the kind words.

2. Alright, another fine entry from Ed, good stuff.

Prime vs. 50g, they both have pro and cons, I like the fact that the 50g uses disposable batteries, so I'm never dry. Color? Well, I can live without. Zeta function, hah, if you knew how many times I've used it in my entire life...

It's good to see that HP is building advanced calculators again, I wasn't really sure the 50g wouldn't be the last one. As far as the prime, well, I've done all my studies with a 48gx and that was my generation's calculator. The Prime is probably targeted at people much younger than I.

I use the 50g almost on a daily basis, and when I need to do some heavy lifting, I still rely on Mathematica, I'm not sure the Prime could replace either one, at least for me. I've never used anything but reverse polish on a calculator, I'm not sure I have the time or devotion to learn a new language; but if my son gets one, I'm sure I'll pick up one as well and start investigating.

That being said, I'm going to follow your series on the Prime very closely, but I'll stick to my 50g for the time being. It works for me.

Cheers!

1. No problem, I am a big fan of the 50g myself.

3. Thank you! This was very useful. I was just wondering how to use CAS commands inside HP Prime programs, and your tutorial clarified for me.

1. CAS commands are tricky. Some work well, like the Integer and other commands that produce primarily numerical results, and others not so well (like solve). I plan on going into this in more detail.

Eddie

4. I like your tutorials very much. I tried some CAS commands in a program to do some calculus operations, i. e. diff() and zeros() on a given function F1. It works well in the CAS view, but it doesn't in a program, not even with qualified variables or commands. What's wrong?

Regards
Wolfgang (Germany)

1. Wolfgang,

In a program, all CAS commands need to have the "CAS." prefix. For example, CAS.diff and CAS.zeros.

If you are working with algebraic or symbolic objects (such as functions or polynomials), you may have to enclose them with single quotes (Shift + Parenthesis key).

Example in Home Mode:
CAS.diff('X^3-2') returns 3*X^2
CAS.zeros('X^3+4') returns [-1.58740105197]

I recommend using zeros instead of solve in Home and programming mode.

Eddie

5. In the program SUMDIV, a local variable called mat is declared. Is this used in the idivis function? SUMDIV doesn't use it anywhere.

1. Thomas,

No it does not and I'll go ahead and edit the unnecessary variable "mat" in SUMDIV. I promise I am not always this bad in proofreading before posting.

Again, many thanks and I will make the correction, it is very much appreciated.

Eddie

6. In the program COMLOCK, the following line has an error:

It should be
L0:=RANDINT(3,0,39);

1. Thank you for pointing this out, Thomas. I am going to correct this error.

Eddie

7. Hi,
I tried to write your QROOTS program and it gives me a syntax error on line 1 right after the B variable in the parentheses. Do you what could be the problem?

Thanks,
Tristan

8. Good evening! I would like to know how to use the input command and enter a complex value in HP prime. Every time I try, invalid data appears. Thanks in advance.

1. Another exemple with 2 complex numbers:

EXPORT Specifying_Types_complex()
BEGIN