Showing posts with label while. Show all posts
Showing posts with label while. Show all posts

Sunday, February 3, 2019

Calculator Programming: Making FOR, WHILE, and REPEAT Loops with IS>, DS<, GOTO, IF, LBL

Calculator Programming:  Making FOR, WHILE, and 
REPEAT Loops with IS>, DS<, GOTO, IF, LBL

Introduction

Say your programming capabilities on your calculator, BASIC portable computer, or programming app does not have the FOR, WHILE, and REPEAT loop structures.  No worries, there are some workarounds we can use.

Today the programs will be demonstrated with the language of the TI-81 and TI-84 Plus CE, but this blog isn't limited to these two calculators.


Simulating FOR Loops with IS> and DS<

The TI-81 (and every Texas Instruments graphing calculator of that family inclusive to the TI-84 Plus CE) has the commands IS> and DS<.

IS>:  Increment and Skip

Syntax:  IS>(variable, target number)

IS> increases the value stored in a variable by 1 and performs a comparison test.  The next command is excited if the increased value is less than or equal to the target number.

IS>(var, target)
[do this command if var + 1 ≤ target]
[skip to this command if var + 1 > target]

Simulating the FOR Loop:

FOR var = begin TO end*
[commands]
NEXT
[commands after loop is over]


* Syntax varies.  For the TI-84 Plus CE:  For(var,begin, end) : [commands] : End

with:

begin → var
LBL [label]
[commands]
IS>(var, end)
GOTO [label]
[commands after loop is over]

The program PRGM1 demonstrates how to accomplish a FOR loop with IS>.  PRGM1 displays the numbers 1 to 10.  The TI-81 section can be programmed by on the TI-81 and TI-84 Plus CE, which the TI-84+ section is for calculators TI-82 and later.

"TI-81"
1→K
Lbl 1
Disp K
IS>(K,10)
Goto 1
Disp "END"
Wait 2
"TI-84+"
For(K,1,10)
Disp K
End
Disp "END"

DS<   

Decrement and Skip

Syntax:  DS<(variable, target number)

DS< decreases the value stored in a variable by 1 and performs a comparison test.  The next command is excited if the increased value is greater than or equal to the target number.  DS< is the opposite of IS>.

DS<(var, target)
[do this command if var - 1 ≥ target]
[skip to this command if var - 1 < target]

Simulating the FOR Loop:

FOR var = begin TO end STEP -1*
[commands]
NEXT
[commands after loop is over]


* Syntax varies.  For the TI-82 to the TI-84 Plus CE, including TI-80 and TI-73:
For(var,begin, end, -1) [commands] End

with:

begin → var
LBL [label]
[commands]
DS<(var, end)
GOTO [label]
[commands after loop is over]

PRGM2 demonstrates the use of DS< and the associated For loop.


"TI-81"
10→K
Lbl 1
Disp K
DS<(K,1)
Goto 1
Disp "END"
Wait 2
"TI-84+"
For(K,10,1,­1)
Disp K
End
Disp "END"

Simulating WHILE and REPEAT Loops

With the proper use of the IF, LBL, and GOTO we can simulate WHILE and REPEAT loops.

Simulated WHILE Loops

WHILE [condition is true]
[commands]
END

* While is available for TI-82 to the TI-84 Plus CE, including TI-73.

can be simulated by:

LBL [label]
[commands]
IF [while condition] 
GOTO [label]

Simulated REPEAT Loops

REPEAT
[commands]
UNTIL [condition]

* The syntax for the TI-82 to the TI-84 Plus CE, including TI-73 is:
Repeat [condition] : [commands] : End

LBL [label]
[commands]
IF [inverse of the repeat condition] 
GOTO [label]

Example:  If the repeat condition is T>5, then the inverse is T≤5.

PRGM3 is a demonstration program on how all three techniques are used to accomplish this:

Start with 1000.  Take the square root.  Keep going until the calculator gets to 1 (by the precision of the calculator).

"TI-81"
1000→K
Lbl 1
√(K)→K
Disp K
If K≠1
Goto 1
Disp "END"
Wait 2
"84+ WHILE"
1000→K
While K≠1
√(K)→K
Disp K
End
Disp "END WHILE"
Wait 2
"84+ REPEAT"
1000→K
Repeat K=1
√(K)→K
Disp K
End
Disp "END REPEAT"

I hope you find these tips useful and helpful.

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Wednesday, April 4, 2018

HP Prime Programming Tutorial is now available


HP Prime Programming Tutorial is now available

The HP Prime Programming tutorial is a set of twelve tutorials designed to get up and running on HP’s PPL programming language.  Knowledge of the programming language will help you take your HP Prime further.  The topics covered are:

1.  Help Key, Local Variables, RETURN command
2.  Message box, IF-THEN-ELSE structure, FOR loop
3.  WHILE loop, REPEAT loop, Detecting key presses
4.  CHOOSE command (providing a pop-up menu), CASE structure
5.  Colors, Switching Apps
6.  Subroutines
7.  Drawing Graphics, Cartesian vs Pixel Coordinates
8.  Drawing Lines, Polygons, Arcs, Circles
9. Numerical Calculus
10. Using App Functions in Programs
11. Drawing and using a soft menu
12. Creating custom apps

Firmware Version 13441



A little history

If you want to look at my original tutorial series that posted during October through December 2013, the first of the series is here: https://edspi31415.blogspot.com/2013/10/hp-prime-programming-tutorial-1-local.html 

However, the HP Prime’s firmware had a lot of updates since then.  And the PDF file has more topics and is up to date. 

On the date of this entry, this blog is one week away from its 7th Anniversary.  This blog’s “birthday” is April 11.  I want to thank you all for your support and compliments, this is labor of love for. 

Enjoy!

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Monday, July 3, 2017

Programming Languages: TI-85 vs. TI-84 CE Plus

Programming Languages:  TI-85 vs. TI-84 CE Plus

I purchased a 1995 edition of the TI-85 at a Pasadena City College swap meet.  The TI-85 was my go-to and favorite calculator through high school (1991 – 1995). 

Today, I am going to compare common programming commands between two of my favorite Texas Instrument calculators, the TI-85 and the current TI-84 CE Plus.  The manuals for each calculator were consulted to answer questions.

Arguments in vector brackets [ ] means the argument is optional.

Command
TI-85 (1992 – 1997)
(probably also applies to the TI-86)
Software Version 10.0
TI-84 CE Plus (2016-)
(most of the this applies to TI-84 Plus 2004-present)
Software Version 5.2.2
Variable Names
Up to 8 characters, must begin with a number, no spaces.
All variables are global.
Single letters A through Z, n, θ
All variables are global.
Complex Number display, Rectangular
(X, Y)
X + Yi
Complex Number display, Polar
(R θ)
R e^θi
Graphing Modes
Function, Polar, Parametric, Differential Equation
Function, Polar, Parametric, Recurring Sequences
Number of Colors
0 (monochrome display)
15: blue, red, black, magenta, green, orange, brown, navy, light blue, yellow, white, light gray, medium gray, gray, dark gray (backlit display)
**CE and C versions only
Input
Input by itself displays the graph screen

Input [“prompt string”], varname
Input by itself displays the graph screen

Input  [“prompt string”], variable
Prompt:  asking for and storing multiple values
Prompt var1, var2, var3 ,…
Prompt var1, var2, var3, …
Disp
Disp by itself displays the home screen

Disp var/string, [var/string …]
Disp by itself displays the home screen

Disp var/string, [var/string…]
Pause
Pause [var/string]
Pause [var/string], [time in seconds]
Wait
N/A
Wait time in seconds up to 100
Display the graph screen
DispG
displays the graph screen
DispGraph
displays the graph screen
Display the function table
N/A
DispTable
Output
Outpt(line, col, var/string)
8 lines, 21 columns
Output(row, col, var/string)
10 rows, 26 columns
InpSt
InpSt stores entered text or equations as strings
N/A.  Use Input
If – Then – Else structure
If condition
Then
do if true
[Else
do if false]
End
If condition
Then
do if true
[Else
do if false]
End
For structure
For(var, begin, end, [step])
commands
End
For(var, begin, end, [step])
commands
End
While structure
While condition
do while condition is true
End
While condition
do while condition is true
End
Repeat – Until structure
Repeat condition
do while condition is false
End
Repeat condition
do while condition is false
End
Menu
Menu(nn, string, label…)
Where nn is from 1 to 15
Menu(“title”, “text”, label…)
Up to 9 options
Labels (Lbl/Goto)
Label names can have up to 8 characters.  All labels are local.
A-Z, 0 -99, θ.  All labels are local.
Increment (by 1) and Skip (if greater to value)
IS>(variable, value)
IS>(variable, value)
Decrement (by 1) and Skip (if less than value)
DS<(variable, value)
DS<(variable, value)
Stop program execution
Stop
Stop
Return from a subroutine
Return
Return
Execute a subroutine
Type the name of the program
Call program by pressing [prgm], choosing it from the Program submenu during editing
Clear the graph screen
ClDrw (clear all drawings)
ClrDraw (clear all drawings)
Display text on a graph screen
N/A
TextColor(color) sets the color.
Text([-1], row, col, string)
0-164 row pixel,
0-264 column pixel
-1 is for large text
Draw a temporary function
DrawF f(x)
DrawF f(x), [color]

Draw a temporary inverse function (f^-1(x) = y, x and y are swapped)
DrInv f(x)
DrawInv f(x), [color]
Shading
Shade (lower, upper, left, right)
Shade (lower, upper, [left, right, pattern, pattern resolution, color])
Draw a line
Line(x1, y1, x2, y2)
Line(x1, y1, x2, y2, [0/1, color, line style])
0/1: 0 to erase, 1 to draw
Draw a circle
Circle(x, y, radius)
Circle(x, y, radius, [color, line style])
Draw a Point
Only the TI-84 Plus CE has similar commands for pixels
PtOn(x,y)
PtOff(x,y)
PtChg(x,y)
Pt-On(x,y, [mark, color])
Pt-Off(x,y,[mark])
Pt-Change(x,y,[color])
Convert a value to a string
N/A
eval(expression) → Str#
No complex results
Convert a string to a value
N/A
expr(string)
Convert a string to a graph variable
St>Eq(string, graph variable)
String>Equ(string, graph variable)
Length of a string
lngth string
length(string)
(call from the catalog)
Extract part of a string
sub(string, begin point, length)
sub(string, begin point, length)
(call from the catalog)
Execute Linear Regression analysis (other regressions are similar)
LinR xlist, ylist
LinReg(ax+b) xlist, ylist, [freqlist, Y= variable]
Turn a stat plot on
N/A (stat plots are not available on the TI-85)
PlotsOn [1,2,3]
Turn a stat plot off
N/A (stat plots are not available on the TI-85)
PlotsOff [1,2,3]
Turn graph functions on
FnOn [1 – 99]
FnOn [1-9, 0]
Turn graph functions off
FnOff [1 – 99]
FnOff [1-9, 0]

Notes

As we can see, the programming language of the TI-85/86 family is similar to the TI-84 Plus family, translating the programs should not be difficult.  I believe that stat plots are available on the TI-86, but not the TI-85.

Keep in mind if you have complex numbers, that the complex mode on the TI-84 Plus (a+bi or re^(θi) has to be turned on.  Furthermore, logarithmic, power, exponential, and trigonometric functions for complex numbers return error on the TI-84 Plus (substitute expressions must be used).

(use radians mode)
With z = X + Yi = R*e^(θ*i), X = real(z), Y = imag(z), R = abs(z), θ = arg(z)

z^n = R^n * (cos(n*θ) + sin(n*θ)*i), n is a real number
ln (z) = ln R + θ*i
e^(z) = e^X * cos Y + e^X * sin Y * i
sin(z) = sin X * cosh Y + cos X * sinh Y * i
cos(z) = cos X * cosh Y – sin X * sinh Y * i
sin¯¹ (z) = asin(z) = -i * ln (zi ± √ (1 – z^2)
z1 ^ z2 = e^(z2 * ln z1)

Eddie


This blog is property of Edward Shore, 2017

Tuesday, November 5, 2013

HP Prime Programming Tutorial #3: WHILE, INPUT, KILL, REPEAT, GETKEY

This tutorial is going to cover a lot, each with some new programming commands in this series. I hope you are ready for the intensity. :)


WHILE, INPUT, KILL

HP Prime Program: TARGET. TARGET is a game where you provide a guess to get a desired number. If you miss, the calculator will tell you if number is higher and lower. At the end of the game, the calculator gives you how may picks you needed to get the target number.

WHILE: Repeat a number of commands while a specific condition is test.

WHILE condition is true DO
commands
END;


Access: Tmplt, 3. Loop, 5. WHILE

Caution: Watch your ENDs! Make sure an END is with each loop and the program itself. Press the soft key Check to check your work.

INPUT: Creates an input screen for variables. On the HP Prime, the input can asked for more than one input. TARGET demonstrates INPUT with one prompt.

One Variable:
INPUT(variable, "title", "label", "help text")
Multi-Variable:
INPUT(list of variables, "title", list of "labels", list of "help text")

Note: Pressing Cancel will store a 0 in variable. You may include code of what to do if the user presses Cancel, but it is not required.

Access: Cmds, 6. I/O, 5. INPUT

KILL: Terminates program execution. Nothing dies, I promise.

Access: Tmplt. 1. Block, 3. KILL


Program:
EXPORT TARGET()
BEGIN
LOCAL C:=0, N:=RANDINT(1,20), G:=-1;
WHILE G≠N DO
C:=C+1;
INPUT(G,"Guess?","GUESS:","1 - 20");

IF G==0 THEN
KILL;
END;

IF G < N THEN
MSGBOX("Higher");
END;

IF G > N THEN
MSGBOX("Lower");
END;

END;

MSGBOX("Correct! Score: "+C);

END;


Try it and of course, you can adjust the higher limit. Here is some thing for you to try with TARGET:

1. Add a limited amount of guesses.
2. Can you display the list of guesses?


REPEAT

ULAM Algorithm: take an integer n. If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. ULAM counts how many steps it takes to get n to 1.

REPEAT:

Access: Tmplt, 3. Loop, 6. REPEAT

Featured:
CONCAT(list1, list2): Melds list1 and list2 into one.

Access: Toolbox, Math, 6. List, 4. Concatenate


EXPORT ULAM(N)
BEGIN
LOCAL C:=1, L0:={N};

REPEAT

IF FP(N/2)==0 THEN
N:=N/2;
ELSE
N:=3*N+1;
END;

C:=C+1;
L0:=CONCAT(L0,{N});
UNTIL N==1;

MSGBOX("NO. OF STEPS="+C);
RETURN L0;
END;


Examples:

ULAM(5) returns:
Message Box: "NO. OF STEPS=6"
List: {5, 16, 8, 4, 2, 1}

ULAM(22) returns:
Message Box: "NO. OF STEPS=16"
List: {22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1}



GETKEY

The next section will introduce a super-important command, GETKEY. We will be working with GETKEY over the entire series.

The Program KEYNO: The person presses key presses. Which each key press, the code returns to the terminal screen. The program terminates when the Enter key is pressed.

GETKEY: Returns the key code of last key pressed. The Prime's key map is below. (Picture is from the HP Prime User's Guide)

Access: Cmds, 6. I/O, 4. GETKEY



EXPORT KEYNO()
BEGIN
LOCAL K;
PRINT();
PRINT("Press any key to get its code.");
PRINT("Press Enter to exit.");

REPEAT
K:=GETKEY;
IF K ≥ 0 THEN
PRINT(K);
END;
UNTIL K==30;

END;


Example Key Codes:
33: 8 key
2: up
7: left
8: right
12: down
50: plus
45: minus


This concludes Part 3. Again, it can't be said enough, thanks for all the comments and compliments. And until next time,

Eddie


This blog is property of Edward Shore. 2013

Tuesday, October 11, 2011

RPL Programming Tutorial - Part 8 - HP 49g+/50g: The WHILE Loop

While We Are Here...

Welcome to Part 8 of the RPL Programming Tutorial. Today, we will cover the WHILE-REPEAT-END programming structure. This structure is similar to the WHILE-WEND programming structure in BASIC. In this loop certain commands are repeated while a condition is true.

The WHILE-REPEAT-END structure looks like this:

WHILE this condition is met
REPEAT these instructions
END


The Program DBY2

This program takes the absolute number from the stack. As long as the number is greater than 1, the program keeps dividing the number by 2.

Note the use of DUP in this program. The DUP (Duplicate) command duplicates the contents of Level 1. It is necessary because tests consume the contents of levels 2 and 1.

[RS] [ + ] (<< >>)
[LS] [ ÷ ]
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)

* The DUP command
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F6] (WHILE)
* Enters the WHILE-REPEAT-END structure
1 [LS] [1/X] ≥
* Enters the while condition
[ &darr ] 2 [ ÷ ] [LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
* Enters the loop instructions
[EVAL] [ENTER]
* Simplifies the number and terminates program entry

[ ' ] [ALPHA] [ALPHA] [F4] (D) [F2] (B) [1/X] (Y) 2 [ENTER] [STO>]

The Complete Program:

<< ABS DUP
WHILE 1 ≥
REPEAT 2 / DUP
END EVAL >>


Instructions:

1. Enter a number.
2. Run DBY2.

Examples:

10 returns 5/8
96 returns 3/4
-105 returns 105/128

This concludes Part 8 of our tutorial. Next time, we will create custom menus. It's going to be a lot of fun! Until next time, Eddie.


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

Earth's Radius by Latitude

Earth's Radius by Latitude Introduction: Calculating the Earth’s Radius In quick, general calculations, we assume that the...