Wednesday, November 16, 2011

HP 15C Programming Tutorial - Part 6: Loops and Counters

Loops

The HP 15C offers two programming instructions for loop control:

1. ISG - Increment and Skip if Greater than
2. DSE - Decrement and Skip if Less than or Equal to

Both instructions use a counter variable for loop control. Let's take a look at ISG and DSE
one at a time.

ISG

ISG uses a counter variable and performs a test:

If the current value is greater than the final value, then the next instruction is skipped.

Loops are created with both the use of ISG and GTO.

The counter variable for ISG is in the form of SSSSS.EEEII where:

SSSSS is the starting value. When ISG is encountered, the starting value is increased by II.

EEE is the ending value. The ending value can be anywhere from 000 to 999. Leading zeros MUST be included. For example, if you want the ending value to be 25, EEE must be 025.

II is the increment value. The increment can be anywhere from 00 to 99, and like the ending value, leading zeros MUST be included.

Hint: If an increment value is not included, the HP 15C assumes an increment value of 1. For example, a counter value of 1.015 increments the counter variable from 1 to 15 in steps of 1.

The counter variable is stored before-hand in a memory register. You can either do this in the program or you can do this outside the program.

Key stroke sequence:
[ f ] [ 6 ] (ISG) register number

Let's illustrate ISG in a program.

The Future Value of Monies Deposited Today

A family buys a $1,000 bond that pays back 5% annual interest. The bond matures (terminates) in five years. What is the value of the bond as each year progresses?

The value of the bond can be found by the formula:

FV = PV ( 1 + I/100 ) ^ N where

PV (Present Value) = Value of the bond today
I (Periodic Interest Rate) = Interest Rate
N (Number of Periods) = Number of Years
FV (Future Value) = Value of the bond N periods from now

Store the following values in these memory registers:
R0 = PV = 1000
R1 = 1 + I/100 = 1.05
R2 = Counter Variable = 1.005

1.005 means, start at 1 and end when 5 is reached. Since the increment is not specified, the HP 15C assumes the increment is 1.

Program Listing:


Key Code Keys
001 42 21 11 LBL A
002 42 7 2 FIX 2 * - Display Setting
003 45 1 RCL 1
004 45 2 RCL 2 * - Counter Variable
005 43 44 INT * - integer of R2
006 14 y^x
007 45 20 0 RCL × 0
008 31 R/S
009 42 6 2 ISG 2 * - increase int(2) by 1
010 22 11 GTO A * - if int(R2) ≤ end value
011 43 32 RTN * - skip if int(R2) > end value


Running the program ([ f ] [ √ ] (A)) and pressing [R/S] four times yields:

Bond Value:
After 1 year, $1,050.00
After 2 years, $1,102.50
After 3 years, $1,157.63
After 4 years, $1,215.51
After 5 years, $1,276.28

The family should expected to be paid $1,276.28 at the maturity of the bond.

DSE

DSE uses a counter variable and performs a test:

If the current value is less than or equal to the final value, then the next instruction is skipped.

Loops are created with both the use of DSE and GTO.

The counter variable for DSE is in the form of SSSSS.EEEII where:

SSSSS is the starting value. When DSE is encountered, the starting value is decreased by II.

EEE is the ending value. The ending value can be anywhere from 000 to 999. Leading zeros MUST be included. For example, if you want the ending value to be 25, EEE must be 025.

II is the decrement value. The decrement can be anywhere from 00 to 99, and like the ending value, leading zeros MUST be included.

Hint: If an decrement value is not included, the HP 15C assumes an decrement value of 1. For example, a counter value of 15.001 decrements the counter variable from 15 to 2 in steps of 1. Remember DSE tests whether the counter value is less than or equal to the final value.

The counter variable is stored before-hand in a memory register. You can either do this in the program or you can do this outside the program.

Key stroke sequence:
[ f ] [ 5 ] (DSE) register number

Let's illustrate DSE in a program.

Countdown

Make a program that has the calculator count down from n to 1.

Let R0 be the counter. We want to the loop to stop at 1, so set the final value to be 0.
We will use Label B for this program.

Program Listing:


Key Codes Key
001 42 21 12 LBL B
002 42 7 0 FIX 0 * - set decimal setting
003 43 44 INT
004 44 0 STO 0
005 42 21 0 LBL 0 * - loop starts here
006 45 0 RCL 0
007 31 R/S
008 42 5 0 DSE 0
009 22 0 GTO 0
010 43 32 RTN


Instructions:

1. Enter the number on the display.
2. Press [ f ] [e^x] (B).
3. Press [R/S] until the display is counted to 1.


Next time we will learn how to solve equations and integrate functions. Happy programming,

Eddie


This tutorial is property of Edward Shore. © 2011

No comments:

Post a Comment

Adventures in Python: Plotting Sine and Cosine, The numpy Module

Adventures in Python:  Plotting Sine and Cosine, The numpy Module This program will require that you have both numpy and matplotlib.  ...