( 30 )
Previous Table Of Contents Next


The Example Programs

To help you learn to program the RPN calculator I have prepared a number of example programs. Each program is very short and has comment statements that explain its purpose and how it accomplishes that goal. I would definitely recommend that you study every one of these programs. I have carefully designed them to introduce new topics in a logical order and at a pace that hopefully won't rip your head from your shoulders. The example programs are integrated into these help pages. That is, read these help pages in order from the beginning and you will be told the appropriate time to explore each of the example programs.

IncrementForever.txt

This program does nothing but endlessly count by ones. I use it to introduce the R/S, GTO .nn, LBL n, GTO n, GSB n, SST, BST, DEL, and ClearPRGM statements.

IncrementForever2.txt

This program builds on the previous IncrementForever.txt program by adding the feature of passing the initial value into the program at run time instead of having it hard-coded in the program. This program also introduces comments.

IncrementForever3.txt

This is a silly program that simply demonstrates that the program counter (PC) will wrap-around from 98 back to 0.

SecondOrderEq.txt

This program solves a particular second order (i.e., quadratic) equation for a particular value of x which is passed to the program on the operand stack.

SecondOrderEq2.txt

This program introduces the calculator's data memory and employs it (rather than the operand stack) to store a temporary copy of a number. This program introduces the STO n statement.

SecondOrderEq3.txt

This program employs data memory rather than the operand stack to pass in the program's argument at run-time.

SecondOrderEq4.txt

This program doesn't just solve the second order equation for a single y value, but rather for a never-ending sequence of y values corresponding to the sequence of x values starting with the particular value passed to the program.

SecondOrderEq5.txt

This program is identical to SecondOrderEq4.txt except the R/S statement is replaced with a PAUSE and hence the program will continue working overnight without operator intervention.

SecondOrderEq6.txt

This program solves the same quadratic equation as the prior programs but instead of displaying the computed y axis values one at a time to the operator this program draws an actual graph of the results. This program introduces the ClearG, SumG, and SetG keystrokes. The window that shows the plot is a general-purpose piece of software that I call cGraph. If you have a need for viewing or printing 2D (i.e., Cartesian) plots then you can use this software independently of the RPN calculator via my GRAPHIT.EXE Windows program that I distribute with my RPN calculator simulator.

DrawSineWave.txt

This is the first program that I ask you to create from scratch. It should draw a plot of a sine wave in the same manner as the SecondOrderEq6.txt example program. You can find my solution in the "Solutions" folder but no peeking! My solution introduces the STO+ n statement. While this program is running the cGraph window is being constantly erased and redrawn and this causes a stroboscopic flashing that you may find annoying. After the program has run for a few minutes you can even begin to observe the type of visual aliasing effect that causes wagon wheels to appear to rotate backwards in movies. Once you halt the calculator program the cGraph window will look fine. You can eliminate the flashing by selecting a fixed plot range for the cGraph window before initiating the calculator program. To learn how to do this read the Help pages for the cGraph window (that is, select Help/Help from the cGraph window's menu bar).

DrawCircle.txt

This program employs the sin() and cos() functions to draw a circle. However the plot produced in the cGraph window will look like an oval rather than a circle until you drag the cGraph window border to force the x and y axes have an aspect ratio of 1.

DrawSpiral.txt

This is another program that I ask you to create from scratch. Actually, it requires only a few changes from the earlier DrawCircle.txt program. As always, you can find my solution in the "Solutions" folder.

DrawFlower.txt

This program draws a 3 leaf flower. By changing only a single number you can produce 4 leaf flowers, 5 leaf flowers, etc.

DrawSpirograph.txt

This program draws the type of artwork you may have produced as a kid using the Spirograph toy. You can produce an almost unlimited number of different designs just by varying the 3 parameters you pass to this program.

FillMemory.txt

This program initializes all of data memory with a constant value. I use it to introduce indirect addressing which is the trick that allows a single program statement to keep touching a different address during every iteration of a loop. This program introduces the DSZ and STO i statements.

ThirtySquares.txt

This program again demonstrates indirect addressing and the DSZ statement.

SecondOrderEq7.txt

Now that we know about the DSZ statement we can write a final version of our SecondOrderEq sequence of programs. Whereas SecondOrderEq6.txt just kept producing data points forever, this new SecondOrderEq7.txt program computes 100 data points and then stops.

Factorial.txt

This program computes the factorial of whatever number is in the X stack location when the program is initiated via GSB 0. It introduces the conditional statements such as x<0 and it introduces the STO* n statement.

BadCombination.txt

This program INCORRECTLY computes the mathematical operation known as "combination" which is the number of distinct ways that you can take n of m items (ignoring ordering). Because the equation for combination involves three factorials, this program motivates us to write our first subroutine. Subroutines are a way to package code such that it can be employed again and again at different moments in your program. This program introduces the GSB n and x<>y statements.

Combination.txt

This program CORRECTLY computes the mathematical operation known as "combination" which is the number of distinct ways that you can take n of m items (ignoring ordering). We fix the contention problem in the factorial subroutine that was messing up the stack for the calling code. This program introduces the Rolldown and CLX statements and discusses the calculator's auto-stack lift feature.

Combination2.txt

The RPN calculator cannot deal with any number larger than 9.999999999 x 10^99 (usually written 9.999999999e99). This fact causes the prior Combination.txt program to fail when asked to compute a combination such as 73C4 which is supposed to equal 1088430. But there is a way to re-write the algorithm to allow it to compute larger combinations without hitting the 9.999999999e99 number limit. The technique is described in the comments in the Combination2.txt example program but the technique is a bit too tedious for me to want to describe it here. I include it nonetheless for the bravest among you. In other words, "this is left as an exercise for the motivated reader".

SquareRoot.txt

This program uses a successive approximation algorithm to solve for the square root of an arbitrary positive number. Successive approximation algorithms are cool because they demonstrate that by merely repeating simple operations you can converge on a complex result.

BadSyntax.txt

This program isn't an example of anything other than what happens when you write your program using Notepad and then import it into the RPN calculator. Any statement that the calculator can't understand is flagged with the special notation "--" instead of being assigned to a program memory slot.

IncrementForever4.txt

I don't discuss this example program anywhere but here. It exists just to document how the RPN calculator handles the GTO i and GSB i statements if it discovers a negative value in register #0. The GTO i statement is supposed to find one of the ten values 0 through 9 in register #0 and then branch to the corresponding LBL n statement. If the corresponding LBL n statement does not exist in program memory than an error is generated. If the value in register #0 is fractional then the fractional component is ignored. If the value in register #0 is positive and greater than or equal to 10, an error is generated since there can't possibly be such a LBL n statement. If the value in register #0 is negative, the calculator branches backwards by the number of statements given by the absolute value of the integer portion of this negative value. What a mouthful! I don't give this technique much prominence since it doesn't carry over to higher-level languages which all branch to labels rather than by number of statements. But I use it in this example program to cause the calculator to count forever.

QuadraticEq.txt

Here's another program that I don't discuss anywhere but here. It employs the quadratic equation to solve for the roots of any second order equation. Any second order equation will always have two roots. The complication is that these two roots might be unique numbers, or they might be the same number (this is called a root with multiplicity of two), or they might be complex numbers involving i which is the symbol for the square root of -1. The RPN calculator has no facility for displaying a complex number in the usual a + bi notation and thus it is difficult for this program to present a satisfactory user interface. But it is a reasonable assignment for a hand calculator so I programmed up an approach and offer it to the motivated reader who can consult the comments in the QuadraticEq.txt file.

GeneralSecondOrderEq.txt

Anyone up for a final exam? Let's imagine that your teacher -- the one that gave you the assignment that motivated us to write the SecondOrderEq series of example programs -- has discovered you are using a computer program that can solve the equation:

     y = x2 + 2 * x + 3

This teacher decides that in honor of your resourcefulness you should get a bunch more second order equations to plot. You realize that you could keep changing the coefficients that are hard-coded into the SecondOrderEq7.txt program. But instead you decide to write the mother of all second order equation programs, one that allows these coefficients to be specified at run-time. That is, to obtain a plot of any second order equation:

     y = a * x2 + b * x + c

you merely need to use the keystroke sequence:

a ENTER b ENTER c GSB 0

Clearly this program will take 3 passed parameters: the a,b, and c coefficients. In the older SecondOrderEq7.txt program we had only a single passed parameter which was the initial x axis value where the plot should start. The plot then consisted of 100 data points corresponding to the next 100 integer x axis values. In the new program we will hard-code the x axis range as being the 101 integer x axis values from -50 to +50. Try writing this program yourself before checking my solution which you can find in the "Solutions" folder.


Previous Table Of Contents Next