Monday, October 24, 2011

RRL Programming Tutorial - Part 15 - HP 49g+/50g: Strings and Message Boxes

Say it in Style

Welcome to Part 15 of the RPL Programming Tutorial for the HP 49g+ and 50g calculators. Today, we will be working with strings and the MSGBOX command.

MSGBOX

MSGBOX takes a string and displays it as a message box. You can turn in a result into a string (as we will do in the MOPMT program later) and display it in a message box.

The keystroke sequence for MSBOX (49g+/50g) is:
[LS] [EVAL] (PRG) [NXT] [F6] (OUT) [F6] (MSGBO)

Note: The MSGBOX command is not available on the HP 48S or 28C.

String Commands

While entering strings, you can type both upper and lower case letters, insert additional lines, and join multiple strings together.

Upper and Lower Case Letters

Press [ALPHA] once to type in one upper case letter or any of the numbers on the numeric keypad.

Press [LS] [ALPHA] once to type in one lower case letter. The numeric keypad gives some special characters not marked on the keyboard.

Press [RS] [ALPHA] to get access to some Greek characters. The numeric keypad gives some special characters not marked on the keyboard.

Pressing [ALPHA] [ALPHA] turns ALPHA-LOCK on. While on ALPHA-LOCK, pressing [LS] [ALPHA] will switch between upper-case character lock and lower-case character lock. It is a good idea to reset ALPHA-LOCK to upper-case before exiting ALPHA-LOCK.

Carriage Return

Press [RS] [ . ] ←| (it looks something like this) starts a new line in the string.

Joining Strings

Join two strings together by "adding" them. ( [ + ] ).

Level 2: "left string "
Level 1: "right string "

Pressing [ + ] yields:

Level 1: "left string right string "

Some Characters on the HP 49g+/50g

[ALPHA]...
[RS] [F1] α
[RS] [F2] β
[RS] [F3] ∆
[RS] [SIN] σ
[RS] [COS] θ
[LS] [ 0 ] ∞
[RS] [y^x] ^
[RS] [ √ ] √
[RS] [ ÷ ] /
[LS] [ 4 ] $
[LS] [ 5 ] £
[LS] [ 1 ] %
[LS] [ 2 ] ;
[RS] [ 2 ] !
[LS] [ 3 ] #
[RS] [ 3 ] ?
[RS] [ 6 ] angle symbol
[RS] [ 5 ] \
[LS] [ . ] :

Some Characters on the HP 48S/48G

[ALPHA]...
[RS] [1st soft key] α
[RS] [2nd soft key] β
[RS] [3rd soft key] ∆
[RS] [SIN] σ
[RS] [6th soft key] θ
[RS] [CST] ∞
[LS] [ 4 ] $
[LS] [ 5 ] £
[RS] [TAN] %
[RS] [ ÷ ] #
[RS] [ 6 ] degree symbol
[LS] [ 1 ] ==
[RS] [ 1 ] ≠
[LS] [ 2 ] <
[RS] [ 2 ] >
[LS] [ 3 ] ≤
[RS] [ 3 ] ≥
[RS] [SPC] angle symbol


Part 15 will feature two programs: MYMSG and MOPMT.

MYMSG: Types a simple message.

MOPMT: Finds a monthly payment given the number of years, loan amount, and the annual interest rate.

The Program MYMSG

Comments will be italicized, starting with an asterisk *.

[RS] [ x ] (<< >>)
[RS] [ + ] ( " " )
[ALPHA] [ALPHA] [TOOL] (I)

* Starts the string
[LS] [ALPHA]
* Switch to the lower-case alpha lock
[SPC] [F1] (a) [HIST] (m) [SPC] [F1] (a) [SPC] [SIN] (s) [COS] (t) [F1] (a) [ √ ] (r) [RS]
[ . ] (←|)

* [RS] [ . ] is the carriage return
[LS] [ALPHA]
* Switch back to upper-case alpha lock
[ √ ] (R) [SYMB] (P) [NXT] (L) [SPC] [LS] [ALPHA]
* After RPL, switch back to lower-case alpha lock
[SYMB] (p) [ √ ] (r) [ ' ] (o) [APPS] (g) [ √ ] (r) [F1] (a) [HIST] (m) [HIST] (m) [F5] (e) [ √ ] (r) [RS] [ 2 ] (!)
* Finish the string
[LS] [ALPHA] [ALPHA]
* Set the alpha-lock back to upper-case and exit alpha-lock
[ → ] [LS] [EVAL] (PRG) [NXT] [F6] (OUT) [F6] (MSGBO) [ENTER]
* Inserts the MSGBOX command and terminates program entry

[ ' ] [ALPHA] [ALPHA] [HIST] (M) [1/X] (Y) [HIST] (M) [SIN] (S) [APPS] (G) [ENTER] [STO>]

Here is the completed program:

<< " I am a star ←|
RPL programmer!" MSGBOX >>

The Program MOPMT

This program asks for the loan amount, the length of the term in years, and the annual interest rate and returns the payment in a message box.

Formula:

PMT = (A * R) / (1 - (1 + R)^T) where

A = loan amount
T = term in years x 12
R = annual interest rate ÷ 1200

Keystrokes:

[RS] [ + ] ( << >> )
[RS] [ x ] ( " " )
[ALPHA] [ALPHA] [F1] (A) [LS] [ALPHA]

* switch to lower case alpha-lock
[HIST] (m) [ ' ] (o) [TAN] (u) [EVAL] (n) [COS] (t) [LS] [ . ] ( : )
* finish the first string
[LS] [ALPHA] [ALPHA] [ → ]
[LS] [EVAL] (PRG) [NXT] [F5] (IN) [NXT] [F1] (PROMP)

* switch back to upper case alpha-lock and finish the first prompt
[RS] [ x ] ( " " )
[ALPHA] [ALPHA] [COS] (T)
[LS] [ALPHA] [F5] (e) [ √ ] (r) [HIST] (m) [LS] [ . ] ( : )
[LS] [ALPHA] [ALPHA] [ → ] [F1] (PROMP) 12 [ x ]

* Enter the second prompt and multiplied term by 12
[RS] [ x ] ( " " )
[ALPHA] [ALPHA] [ √ ] (R)
[LS] [ALPHA] [F1] (a) [COS] (t) [F5] (e) [LS] [ . ] ( : )
[LS] [ALPHA] [ALPHA] [ →] [F1] (PROMP) 1200 [ ÷ ]

* Enter the third prompt and divided rate by 1200
[RS] [ 0 ] ( → )
[ALPHA] [ALPHA] [F1] (A) [SPC] [COS] (T) [SPC] [ √ ] (R) [ALPHA]

* Store amount, 12*term, rate/1200 in local variables A, T, and R respectively
[RS] [ + ] ( << >> )
[ALPHA] [F1] (A) [SPC] [ALPHA] [ √ ] (R) [ x ]
1 [SPC] 1 [SPC] [ALPHA] [ √ ] (R) [ + ]
[ALPHA] [COS] (T) [y^x] [1/X] [ - ] [ ÷ ]

* Enters the calculation for monthly payment
2 [LS] [SYMB] (MTH) [F5] (REAL) [NXT] [NXT] [F1] (RND)
* Rounds the result to 2 decimal places
[RS] [ x ]
[ALPHA] [ALPHA] [SYMB] (P)
[LS] [ALPHA] [F1] (a) [1/X] (y) [HIST] (m) [F5] (e) [EVAL] (n) [COS] (t) [SPC]
[LS] [ 4 ] ($) [LS] [ALPHA] [ALPHA]

* Build the "Payment $" string. This is the left string, so we need to swap levels 1 and 2
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP)
[LS] [EVAL] (PRG) [F5] (TYPE) [F4] (→STR)

* →STR converts numbers and expressions to strings
[ + ]
* Joins the two strings.
[LS] [EVAL] (PRG) [NXT] [F6] (OUT) [F6] (MSGBO) [ENTER]
* Enters the MSGBOX command and terminate the program entry

[ ' ] [ALPHA] [ALPHA] [HIST] (M) [ ' ] (O) [SYMB] (P) [HIST] (M) [COS] (T) [ENTER] [STO>]

The completed program:

<< "Amount:" PROMPT
"Term:" PROMPT 12 *
"Rate:" PROMPT 1200 /
→ A T R
<< A R * 1 1 R + T ^ INV - / 2 RND
"Payment $" SWAP →STR + MSGBOX >> >>


Instructions:

1. Run MOPMT
2. Enter the loan amount and press [LS] [ 0 ] (CONT)
3. Enter the term (in years) and press [LS] [ 0 ] (CONT)
4. Enter the annual rate and press [LS] [ 0 ] (CONT)
5. A message box displays the monthly payment.

Example:

Find the monthly payment on a $256,000, 36-year loan with a given interest rate of 3.7%.

Answer:

Payment
$1073.18

Next time we will work with handy conversions and using choose boxes.

Happy programming,
Eddie.


This tutorial is property of Edward Shore. Mass reproduction and distribution requires express permission of the author.

Spotlight: Sharp EL-5200

  Spotlight: Sharp EL-5200 As we come on the 13 th (April 16) anniversary of this blog, I want to thank you. Blogging about mathematic...