Sunday, November 6, 2022

hp 9g: Sample Programs

hp 9g:  Sample Programs


Introduction


As I mentioned in yesterday's blog, today's blog will have some sample programs for the hp 9g.   


The hp 9g's programming language is a mix of C and Basic, and is unique from the rest of the Hewlett Packard calculators.    The hp 9g has 400 steps of programming memory to be allocated among 10 program files, P0 through P9.   


The 9g has 26 variables, but can be extended up to 53 variables at the cost of program steps.  The square brackets indicate indirect addressing.  The number in the square brackets is the number of steps away from the variable.   Any variable beyond Z (Z[0], A[25]) must be addressed in this fashion.  


Example: 

A[0] calls up A

A[1] calls up B

...

A[25] calls up Z. 


We can use any letter, but I recommend using either A or Z. 



hp 9g Program:  Integer Division


This program gives the quotient and remainder of N ÷ D.  The program here assumes you enter positive integers, and some code can be entered for checking inputs.  


INPUT N

INPUT D

Q=INT(N/D)

R=N-Q*D

PRINT Q," R",R

END


Examples:


N = 4379, D = 8;  Result:  547 R3


N = 5884, D = 29;  Result:  202 R26



Note:  The PRINT command converts variable values to strings automatically, without the need for a string or STR$ command.


hp 9g Program:  Arithmetic-Geometric Mean


This program determines the AGM given two numbers A and G.


INPUT A

INPUT B

PRINT "ACC. 10^-8"

Lbl 0:

X=.5(A+G)

Y=√(AG)

If (ABS(X-Y)<10^(-8))

Then {Goto 1}

A=X

G=Y

Goto 0

Lbl 1:

Print "ANS= ", X


Examples:


A = 1.3, G = 1.5,   Result:  ANS ≈ 1.39821143


A = 20, G = 45,  Result:  ANS ≈ 31.23749374



Notes:  


10^ is from [ 2nd ] (10^x) the antilog function.


The label command must have the colon character after the label number, or a syntax error occurs.  


If you run a program from the Main mode by the [ PROG ] key, you will be transferred to Program mode when the program finishes.


hp 9G Program: The Maximum Value of A through X



Y = maximum value

Z = counter variable


PRINT "MAX(A:X)"; ◢

FOR(Z=2;Z<23;Z++){

IF(A[Z]>Y)

THEN{Y=A[Z]}};

PRINT "MAX=",Y;

END


Example:  


Clear all variable in the MAIN mode by CL-VAR

Set up variables: 


A = 12, D = 24, E = 37, G = 40, S = 19, T = 16, U = 7

Result:  MAX= 40


Notes:   


++ is from the Instruction menu, it is a smaller-jointed double plus.


When the program encounters a run/stop instruction (◢), the last message is displayed.  Continue execution by pressing the equals key [ = ].  


hp 9g Program:  Plotting y = A * sin(B * x + C) + D


The programming mode does not offer a lot of support when it comes to graphs.  


PRINT "SINE"

SLEEP(.5)

INPUT A,B,C,D

RANGE(-2π,2π,π/4,-A,A,1)

Graph Y=A*sin(BX+C)+D

PRINT "PRESS G<>T"

END




A = 1, B = 1, C = 0, D = 0



Notes:


Prior to running the program, set the angle mode to Radians through the [DRG] key.  Also, press [ 2nd ] [ → ] (CLS) to clear the graph screen.  Unfortunately, neither of these commands can be programmed.  


Plots from programs cannot be traced.


When the program ends, you will need to switch to Main mode, press [G<>T] to show the graph.   Not a very efficient way of showing a graph.  



That is a sample of the programs for the hp 9g.  Good for number crunching, kind of a little bit to be desired for graphics.


Until next time,


Eddie 



All original content copyright, © 2011-2022.  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. 


HP 20S: Gamma Function Approximation (Stirling's Formula)

HP 20S:  Gamma Function Approximation (Stirling's Formula) Introduction The gamma function uses the approximation sequence: Let t = x + ...