![]() |
| Scott Nazelrod |
| Main » Articles » Sharp Calculator Programming |
By now, you've probably become comfortable with both a standard desktop computer and your Sharp graphing calculator. You know of the things on the desktop computer called programs, and since you've used them so much, you've grasped the basic idea of a program but probably never stopped and thought what it exactly meant.
A program is a logical sequence of coded instructions (program code) specifying the operations to be performed by a computer. In this case, the "computer" is your calculator.
You can't just type random things in the calculator and expect it to work. This is not a program:
ASK ME FOR A NUMBER, TAKE THE SQUARE ROOT OF THREE AND DIVIDE IT BY THAT NUMBER YOU ASKED ME FOR. If THE ANSWER IS NEGATIVE Then IT IS NO REAL ROOTS.
If you tried typing that in your calculator, it wouldn't be able to make heads or tails of it. This is in English, not in computer language, so you're just going to get an error if you run the program.
The very first computers (pre-1986: old!) had to get all instructions in programming language. To make it easier for newer users, a new language called Beginner's All-purpose Symbolic Instruction Code, or BASIC, was put together. BASIC was unique in that the key words used were actual English words and were used in a way that made sense. BASIC does not rely on other constructs like braces and parenthesis, like the popular C and C++ languages do, and handles quite a bit of the details for you, so the programmer needs to know relatively little about the internal workings of the computer.
The language used on your EL-9600c Graphing Calculator is derived from BASIC. On the PC, BASIC has inspired a number of languages, most notably Microsoft's Visual Basic. What does this mean to you? If you learn any one of the three languages, it would be easier for you to learn the other two languages if you wanted to.
A vague description of programming is given in the EL-9600c's instruction manual (Chapter 12, pp. 239-258), but it expressly states on Page 240: "This instruction manual contains no explanation of the knowledge and concepts required for programming." The purpose of this guide, then, will be to give you that knowledge. (The EL-9900 manual is a bit more specific in its description of the language; it may be helpful to refer to that manual at some later point in time.)
All your programming tools can be accessed through the PRGM (Program) menu, accessed by pressing 2nd MATRIX on the EL-9600c, and PRGM on the EL-9900. When you access the Program menu from the Home screen, there are three submenus: EXEC (Execute), EDIT, and NEW. Execute simply runs one of the programs you have input into the calculator. Edit will open the code editor and display the selected program?s code. New starts a new program, and then opens the code editor.
When you are in the Code Editor, the Program menu contains tools essential to developing your program.
A note to users of the EL-9900: Your keyboard will need to be flipped to the blue (Advanced) side to be able to program. From the green (Basic) side, you can execute programs, but cannot edit them.
From the Home Screen, pull up the Program menu. Select "New" and hit ENTER. Your screen will show a line with the cursor above the line, and A-LOCK will be automatically turned on. Type an appropriate file name for the program, and hit ENTER. The cursor will move below the line. You are now ready to enter your code.
The central elements of your program will be variables. Use variables to make formulas to be evaluated, to which the user can plug in constants (normal numbers) at runtime. A variable can be any single letter, including the Greek character theta (θ). Whenever you use a variable, it's a good idea to write it down and what it's used for.
Variables can only be 1 character long. (You can't name a variable, say, RADIUS.)
Try to keep variables associated with what it represents. For example, in a program to find the area of a circle, let R stand for the radius of the circle. If R has been taken, use another variable, but make sure to write it down so you know what it's for.
Variables are created and have values stored to them in two ways: one, by specifically asking the user to enter a value for the new variable, or by storing the result of an equation into the variable (The user is the person who is using the program after it's finished). To store a variable, use the STO button, which puts an arrow in the code. For example:
3 + 5 ⇒ A
will add 3+5 and store the value 8 into variable A.
Later, you could have:
A + 9 ⇒ B
which would take the value of A (defined earlier as 8) and add 9, and store it to B. A would still equal 8, but B would now equal 17.
Note that if you take an old variable and assign it a new value, the new overwrites the old. For example:
A + 8 ⇒ A
Before this line is executed, A equals 8. After the line, A equals 16.
If you use a variable that has not had a value assigned to it yet, it will have a value of 0.
Please note: even though in the examples we've only been doing addition, you can also subtract, multiply, divide, take the square root, etc.
The other way of creating a variable, asking the user, is a bit more challenging but more useful. You use a command called Input, followed by a variable. To insert an Input into your code, pull up the Program menu, submenu A, and select Input (3). Example:
Input Y
When the program hits an Input statement, everything stops until the user enters a number to be stored to the variable. In the example above, Y? will appear on the screen. The user types in a number and hits ENTER. Y now equals whatever the user typed in. For example,
Input Y
Y + 10 ⇒ Z
This simple program asks the user for Y and then adds 10 and stores it to Z.
Please note two things in the example. First, note that the two lines of code should be typed in on separate lines. Each line is one command to the computer. Giving it all the instructions on one line of code will confuse it. Also, note how Input is a dark blue color. I will use this in the sample programs to denote that you have to get it from a menu, not a key on the keyboard.
So far you're able to work with variables: add them, store them, ask the user to define them. However, once the program has worked out the answers, it needs to get them back to the user. It also needs to give the user text messages to tell them what to do next.
Print is the name of the command used to display things on the screen. You can imagine the screen as a piece of paper that you're printing values on. (The usage of the term "Print" to display output dates from the times when computers were attached to teletypes rather than monitors.)
All you need to do is call up a Print, then type the variable you want displayed. Take the example above and add a Print command to it.
Input Y
Y + 10 ⇒ Z
Print Z
Now the program will ask for Y, add 10, store to Z, and put the value of Z on the screen. This would work too, and would be more efficient in this case:
Input Y
Print Y + 10
With long formulas like the distance formula and quadratic formula, or when you have to refer to the formula several times, it would be easier to create a separate variable and then print the variable.
You'll also want to print text on the screen. However, if you type in Print HI, for example, you won't get the word HI, you'll get the value of H times I. You need to tell the program you want the letters to be displayed and are not variables. Use the quotation mark on the Program menu to start a string of letters you don't want to be interpreted as variables. Everything following the quotation mark is now interpreted as text.
Print "HI
The Rem command (submenu A, command 5) causes the calculator to ignore the rest of the line. This is best used to insert a comment into the code telling you (or others) how it works.
Using the Rem command is an easy way to hide code that you don't want to execute, but will want to later. This is called commenting out the code. For example, you want to test the program to see if it will run without a bit of code. If it turns out you need the code in question, you can easily reinsert it by deleting the Rem command.
I will use the color green to designate comment code. Also, although your comments would be all CAPS, I'll use lowercase for easy reading. Example:
Print A + 5
Rem Prints A + 5.
Use comments sparingly - even though they aren't executed, they still take up space in the calculator's memory (and take time to type in).
There is a Key command for inputting a single numerical digit. This is good for menus and similar things, so that users won't accidentally a) give you a number that's too big or b) have to hit ENTER.
The Key command (PRGM menu, number 7) has a simple syntax. You only have to enter Key and the variable you want to store to.
Key K
Besides inputting numbers, you can also let your users hit the arrow keys. The arrow keys are converted to a number that is stored in the variable:
| Arrow | Number |
|---|---|
| Right | 10 |
| Left | 11 |
| Up | 12 |
| Down | 13 |
As you probably suspect, the program runs sequentially. The computer steps through the code, running each line from the top of the program all the way to the end.
Occasionally, however, you might want to isolate a block of code from the rest of the program. For example, you might have a block of code that updates a variable. You need to run this code at several points throughout this program. Obviously, you could type the code every time you want to run the block, but this takes forever to type and contributes to carpal-tunnel syndrome. Not so obvious is the fact that if your code blocks have a fundamental problem to them, you'll have to browse though them and find where all the mistakes are (maintaining the code).
A better way to do this is to type all the code once at the end of the program. Now you can call the code with a single command, placed wherever you feel like it. Code isolated and called like this is called a subroutine.
The first step is writing the code. Type the code that will be called at the very end of your program. Now, where the subroutine starts, insert a Label command. You can get it from the BRNCH (B) submenu, command 1. Following the Label command, on the same line, type the name of your subroutine. It can be up to 10 characters long.
Label SUB1
The Label actually does nothing. It just places a reference point in the code for the program to refer to. There can be as many as 50 labels in a single program, but all of them must have a unique name.
Now, at the end of your subroutine, insert a Return command. It is command 5 from the Branch menu. The Return command exits the subroutine and takes you back to the main routine.
Now for the crucial line of code. Where you want your subroutine to execute, add a Gosub command, then type the subroutine's name. Gosub is located under the Branch menu, command number 4. So, here's how your program looks now:
Rem Code goes here
Gosub SUB1
Rem Code goes here
Label SUB1
Rem Subroutine code goes here
Return
Now, whenever the program hits the Gosub command, it jumps down, executes the subroutine code, and then returns to execute the code that is immediately after the Gosub command.
There's just one problem: since the subroutine is at the end of the program, it will run again after the main routine ends. So we will instruct the calc to quit the program after the main routine ends, but before the subroutine code begins. This is achieved by simply adding an End statement. It may be found under submenu A, command 6).
Rem Code goes here
Gosub SUB1
Rem Code goes here
End
Label SUB1
Rem Subroutine code goes here
Return
One final note on subroutines: you can call subroutines from other subroutines. For example:
Label SUB1
Gosub SUB1
Rem Code goes here
Return
Label SUB2
Rem Code goes here
Return
You can then call another subroutine from SUB2, an so on, but you can only get 10 subroutines deep before you need to start Returning.
Now you can make your programs get and show information, as well as move around with subroutines. But sometimes that's just not enough. An example is when you have several different equations, which you need to pick from based on the user's choice.
To execute this example, you will first get the user's choice of equation, and save it as a variable:
Rem Create a program menu
Print "1. C to F
Print "2. F to C
Input A
Now you need to find out which of the two equations to use based on whether the user typed 1 or 2. This is done using two commands: If and Goto.
First, you insert the If (BRNCH menu, number 3) Then you type the conditions the code has to meet, then the Goto command, then a label to go to. Example:
If A=1 Goto LBL1
To get the equal sign, press ALPHA MATH on the EL-9600c or ALPHA (-) on the EL-9900. What this code does is checks and sees if the value of C is 1. If C is 1, then the code will go to the LBL1 label. Otherwise, code will run to the next line.
The Goto command will appear right next to your condition, without a space in between. It helps you to read the code, though, to have a space. So insert the space using the ALPHA . key sequence.
You can test a whole array of functions by including several different If statements all in a row. For example:
If A=1 Goto LBL1
If A=2 Goto LBL2
If A=3 Goto LBL3
Rem ...
Quite simple, no? One word of warning: you can't use a Gosub with an If. It has to be Goto. And, of course, you can use Goto by itself, without having an If statement, similar to a Gosub without a return function. However, this should be used very rarely, as Goto statements can produce "spaghetti code" which is difficult to decipher. (Programming tip: writing clear code always helps. If there's a bug in the program, it will be simpler to exterminate if the code is simpler.)
If statements are quite versatile. For instance, you can use inequalities rather than the equals sign. These are available on the MATH menu.
Loops are quite useful, but unfortunately, are only available on the EL-9900, not the 9600c. You use them to execute a certain patch of code over and over until some preset condition is met. For example, let's use a less practical example of a blackjack game, where the dealer must stand on 17 or higher. You could use a loop (calling a card-drawing subroutine) that would execute until the dealer's count was greater than or equal to 17, at which point the program would exit the loop, and final counts would be displayed.
The calculators have two types of loops, the For loop and the While loop. The For loop runs a set number of times and exits, while the While loop will run indefinitely until a certain condition is met.
For/Next loops are a basic staple of almost any programming language. It's a shame they were left out of the 9600c. But anyway, For loops work by using a counter variable. Typically, I is used as the counter variable. A For loop will increment its counter variable each time it is run.
The For loop (under the BRNCH menu, number 7) has three conditions that must be set, separated by commas. The first is the variable that you want to use as the counter variable. The second is the initial value of the counter. The third parameter is the end value. That's all you have to set. However, if you want, you can give a fourth option, for how much you want to increment the counter on each run of the loop. If you leave it off, the counter will go up by 1 on each pass.
After the For command you enter the code that will be part of the loop. The loop ends with the Next command.
I will make a number of sample programs avaliable that have been useful to me. Reading through them should be informative.
Last updated 2007-04-20.