Tuesday, October 18, 2011

RPL Programming Tutorial - Part 12 - HP 49g+/50g: Subroutines and the START Loop

Subroutines

Welcome to Part 12 of the RPL Tutorial. In Part 12, we will cover subroutines. A subroutine is a named routine defined in the program, which is saved for later use. the subroutine is called whenever the need arises.

One way to approach subroutines is to type and store the subroutines at the beginning at the program. Unless the user desires to store the subroutine permanently, the subroutine must be purged at the end of the program. Here is one template for a program using a subroutine:

<< << subroutine >> 'SUB1' STO
main program
'SUB1' PURGE >>


Using two or more subroutines:

<< < subroutine1 >> 'SUB1' STO
<< subroutine2 >> 'SUB2' STO
...
main program
...
{'SUB1' 'SUB2' ... } PURGE >>


START-NEXT Structure

The START-NEXT structure is like the FOR-NEXT structure, except that the START-NEXT structure does not use a counter. A structure looks like this:

<< start stop START commands NEXT >>

Keystroke sequence: [LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F3] (START)

The program presented in Part 12 will illustrate using a subroutine and a START-NEXT structure.

Building a list of random numbers

In this program, we are going to pick d integers from low (l) to high (h) - allowing the choices to repeat.

The program uses the subroutine named SUB1:

<< H L - RAND x IP L + >>

SUB1 will be called up in the START-NEXT loop.

The other variables used in the program are:

L = low number,
H = high number, and
D = # of draws

To build the list, the stack is cleared, allowing the program to fill it with draws. Once the draws are complete, DEPTH →LIST will be used to create the list.

The Program SAMP

[RS] [ + ] ( << >> )
[RS] [ + ] ( << >> )
[ALPHA] [MODE] (H) [SPC] [ALPHA] [NXT] (L) [ - ]
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND) [ x ]
[LS] [SYMB] (MTH) [F5] (REAL) [NXT] [F5] (IP) [ALPHA] [NXT] (L) [ + ] [ ↓ ]
[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA] [ → ]
[STO>]

* Enters are stores the subroutine SUB1
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [NXT] (L) [ ' ] (O) [+/-] (W) [RS] 3 (?)
* [ALPHA] [RS] 3 sequence gives the question mark character, the ? is not marked on the keyboard
[ALPHA] [ → ] [LS] [EVAL] (PRG) [NXT] [F5] (IN) [NXT] [F1] (PROMP) [ ' ] [ALPHA] [NXT] (L) [ → ] [STO>]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [MODE] (H) [TOOL] (I) [APPS] (G) [MODE] (H) [RS] 3 (?) [ALPHA] [ → ] [F1] (PROMP) [ ' ] [ALPHA] [MODE] (H) [ → ] [STO>]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [LS] 3 (#) [SPC] [F4] (D) [ √ ] (R) [F1] (A) [+/-] (W) [SIN] (S) [RS] 3 (?) [ALPHA] [ → ] [F1] (PROMP) [ ' ] [ALPHA] [F4] (D) [ → ] [STO>] [RS] [backspace] (CLEAR)

* Enters the prompts for H, L, and D.
1 [SPC] [ALPHA] [F4] (D)
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F3] (START)

* Inserts the START-NEXT structure
[ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA]
* Calls the subroutine SUB1
[ ↓ ]
[LS] [EVAL] (PRG) [F1] (STACK) [NXT] [F6] (DEPTH)
[LS] [EVAL] (PRG) [F6] (LIST) [F4] ( →LIST )

* Turns the stack into a list with DEPTH →LIST
[LS] [ + ] ( { } )
[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA] [ → ]
[SPC] [ ' ] [ALPHA] [MODE] (H) [ → ]
[SPC] [ ' ] [ALPHA] [NXT] (L) [ → ]
[SPC] [ ' ] [ALPHA] [F4] (D) [ → ] [ → ] [ → ]
[LS] [EVAL] (PRG) [F2] (MEM) [F1] (PURGE) [ENTER]

* Clean up and entry termination

[ ' ] [ALPHA] [ALPHA] [SIN] (S) [F1] (A) [HIST] (M) [SYMB] (P) [ENTER] [STO>]

The complete program:

<<
<< H L - RAND x IP L + >> 'SUB1' STO
"LOW?" PROMPT 'L' STO
"HIGH?" PROMPT 'H' STO
"# DRAWS?" PROMPT 'D' STO
CLEAR
1 D START SUB1 NEXT
DEPTH →LIST { SUB1 H L D } PURGE >>


Instructions:

1. Run SAMP
2. Enter the low number at the "LOW?" prompt, press [LS] [ON] (CONT)
3. Enter the high number at the "HIGH?" prompt, press [LS] [ON] (CONT)
4. Enter the number of draws at the "# OF DRAWS?" prompt, press [LS] [ON] (CONT)
5. Obtain a list. Use [HIST] [F2] (VIEW) to see the list.

Example:

1. Draw 6 numbers from 100 to 999. A result may be:

{361, 779, 156, 295, 349, 382}

2. Draw 12 2 digit numbers (10 to 99). A result may be:

{52, 63, 83, 76, 52, 54, 71, 47, 70, 16, 23, 41}

This concludes Part 12 of our tutorial. Up next we will make a custom menu and use it to help make programming easier. As always, thank you visiting - Eddie


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

TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...