Tuesday, October 18, 2011

RPL Programming Tutorial - Part 10 - HP 49g+/50g: The DO Loop

Do It Until It's Done!

Welcome to the third week of my RPL tutorials for the HP 49g+ and 50g Hewlett Packard graphing calculators. We will start this batch of tutorials with Part 10: the Do Loop.

The DO-UNTIL-END loop structure works like the WHILE-REPEAT-END loop structure except instead of repeating a bunch of designated commands while a certain condition is met, this time we are repeating a bunch of designated commands until a condition is met.

A DO-UNTIL-END structure looks like this:

DO commands
UNTIL this condition is met
END


Key sequence from the PRG-BRCH menu:

[LS] [F5] (DO)

Let's illustrate a use of a DO-UNTIL-END structure with a simple program: have the calculator generate a bunch of random numbers until the sum of 1 is reached.

It's a new week - so here is a reminder of how I label shift keys:

[RS] is the right shift key, 2nd key up from the ON button on the left side of the keyboard. [RS] is red on the 49g+ and orange on the 50g.

[LS] is the left shift key, 3rd key up from the ON button on the left side of the keyboard. [LS] is green on the 49g+ and white on the 50g.

[ALPHA] is the 4th key up from the ON button on the left side of the keyboard. On both the 49g+ and the 50g, the key is yellow. Pressing [ALPHA] twice puts the calculator in ALPHA-LOCK mode.

[big X] is the "X" key on the 4th row from the bottom, 3rd from the left. This is so I distinguish X from the times key ([ x ]).

Finally, the soft keys labeled F1 - F6 are on the top row.

Add Random Numbers until I get at least 1

This program will keep adding random numbers until the sum exceeds one.

The Program RAND1

Comments are italicized beginning with an asterisk. Here we go!

[RS] [ + ]
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND)

* Inserts the first random number. RAND always enters numbers between 0 and 1.
[LS] [EVAL] (PRG) [F3] (BRCH)
[LS] [F5] (DO)

* Inserts the DO-UNTIL-END structure
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND)
[ + ] [ ↓ ]

* Enter the commands that are to be repeated (RAND + )
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
1 [LS] [1/X] ≥

* Enters the condition (sum≥1)
[ENTER]
* Terminates program entry

[ ' ] [ALPHA] [ALPHA] [ √ ] (R) [F1] (A) [EVAL] (N) [F4] (D) [ENTER] [STO>]

The completed program:

<< RAND
DO RAND +
UNTIL DUP 1 ≥
END >>


Instructions:

Just run RAND1.

Examples of some results you might get: 1.46211570675, 1.12775622211, 1.50610262525

That wraps up Part 10 of the RPL Tutorial Series. Next time, we'll look at the CASE structure. See you then!

Eddie

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

6 comments:

  1. Eddie,
    I'm scratching my way through remedial RPL. I am using "get" and "put" to store and extract coordinates and angular values from a matrix. I am using the matrix as a data table rather than an arithmetric creature. The net result being that the operator will enter any given number of control coordinate pairs and the measured angles between them in order to compute the "free station" or resection position which can be further refined by through a least squares estimate. Relevant to this post is the loop strucure for input. I'm looking for any suggestions with regard to setting up the input loop (any any other suggestions are always welcome!. The number of times will be determined by the user and will be equal to the number of rows in the data matrix. Thoughts??? this is what I've got thus far:
    << 'DATFL' PURGE "NO. OF CONTROL POINTS" "" INPUT OBJECT> 4. 2. >LIST 0. CON 'DATFL STO 'DATFL'{1. 2.} "CP NORTHING" "" INPUT OBJ> PUT 'DATFL' {1. 3.} "CP EASTING" "" INPUT OBJ> PUT {2. 2.}>>
    NOTE THAT > IS REALLY THE ARROW ATTACHED TO "OBJ" COMMAND. ANY THOUGHTS ON HOW TO UPDATE THE {MATRIX ELEMENTS} I.D. AS THE LOOP INCREASES? THE FIRST POINT DATA IS IN ROW 1, THE SECOND WILL BET PUT IN ROW 2 AND SO ON... COLUMN 1 IS POINT I.D., COL 2 IS NORTHING, COL3 IS EASTING, COL 4 IS MEASURED ANGLE IN SEQUENCE.

    ReplyDelete
  2. Jason,

    Thanks for writing. I tend to favor the FOR loop because it is versatile. Here I would use it for the input structure:

    << 'DATAFL' PURGE
    "NO. OF CONTROL POINTS" INPUT OBJECT> 4 2 LIST> 0 CON
    'DATAFL'STO

    DATAFL
    1 DATAFL SIZE OBJ> DROP2 FOR K
    {K 1} K PUT
    {K 2} "CP NORTHING" "" INPUT OBJ> PUT
    {K 3} "CP EASTING" " " INPUT OBJ> PUT
    {K 4} "MEASURED ANGLE" " " INPUT OBJ> PUT
    NEXT
    'DATAFL' STO
    ...
    (rest of your program)
    >>

    Notes:
    * K acts a counter (Row 1, Row 2, etc)
    * Each row is updated a row at a time.
    * The commands DATAFL SIZE OBJ> DROP2 gives the number of rows, which is the required upper limit for the FOR loop

    Some alternatives:
    * You can store the number of rows in a variable and later purge it
    * Add an additional string to the stack prompt to tell the user which row the user is working with. Strings are combined by the + (plus) operator. Example: "CP NORTHING-ROW " K >STR + ("CP NORTHING-ROW 1" for whatever k is at the time)

    Hope this helps and looking forward to the finished product.

    Eddie

    ReplyDelete
  3. Thanks Eddie. That's the ticket. I have a rough finished product. It will accept infinite input and produce a radius, radius point coordinates, the point of curvature in and point of tangency out coordinates. I need to tidy up the program routines and variables to be efficient and transparent. I'll forward the packet. Thanks again!

    ReplyDelete
  4. Hi Eddie,

    Do you know if there is a way to insert 2 conditions into a DO-UNTIL-END structure?
    e.g. until <40 or >60?

    Cheers!

    ReplyDelete
    Replies
    1. Use an OR statement.

      DO
      ...
      UNTIL X 40 < OR X 60 >
      END


      Hope that helps,

      Eddie

      Delete


  5. Thanks for give me this information really this product is very effective.


    hp printer support

    ReplyDelete

Adventures in Python: String Manipulation and Function/Derivative Table (Subroutines)

Adventures in Python:  String Manipulation and Function/Derivative Table (Subroutines) String Manipulation This script demonstrate...