Programming Using TI-Nspire Presenter: Raymond Rozen

Programming Using TI-Nspire
Presenter: Raymond Rozen
4 National Conference on
Graphing Calculators
th
"Realizing Innovative Teaching-Learning-Assessment
through Graphing Technology"
June 21 - 23, 2011
Venue:
Hotel Equatorial, Penang
Organized by:
School of Mathematical Sciences
Programming the TI-Nspire
© R. Rozen
Page 1
Program 1
Write a program to simulate throwing one coin, a number of times and counting the number of
heads and tails.
Press
•
home c
use the arrow keys on the Touchpad, to select the
Calculator icon and press:
•
enter ·
This will open a new document with a calculator page.
Then press
•
menu b
•
9: Functions & Programs 9
•
1: Program Editor 1
•
1: New 1
Name the program "onecoin" and leave the Libary
Access as the default None, for now, Tab e to OK
and press enter ·.
Programming the TI-Nspire
The screen will be split in two, with the calculator page
on the left and the program editor on the right. Notice
that the basic template for the program is already typed
in.
It is easier to write the program in a full screen, so
ungroup the pages, press:
•
doc ~
•
5: Page Layout 5
•
8: Ungroup8
Then press
•
ctrl /
• ¢
to get back to the program page.
Move the cursor to the line under Prgm and press
•
menu b
•
6: I/O 6
•
4: Text 4
This will create a message to the user when the
program is running. Then press Ó , this will insert a
field to type in the instructions for the program.
Complete the line as
Text "Program to simulate throwing one coin"
© R. Rozen
Page 2
© R. Rozen
Programming the TI-Nspire
To get a newline into the program, you will need to
press the newline character @, it is located to the right
of the letter U, now press
•
menu b
•
3: Define Variables
•
1: Local 1
3
This is where we define the variables that will be used
in the program. A local variable is one that exists only
while the program is running, its values are deleted
when the program has finished.
The variables that we will use in this program are i,
maxthrows, coin, heads and tails.
Complete the line as
Local i,maxthrows
Local coin,heads, tails
Repeat for the other variables as shown.
i is a counter for the loop, maxthrows, will be the
maximum number of times to throw the coin, coin will
allocated as a random number, and heads and tails will
be used as counters, to count the number of heads and
tails that appear.
We now want to initialize some of these local variables,
that is assign zeros, to the values of heads and tails,
which will count the number of times heads and tails
appear, when the program is run.
Complete the lines as
heads:=0
tails:=0
Note that := is one way to give a variable a value.
Now press
•
menu b
•
6: I/O 6
•
2: Request 2
Then press Ó , this will insert a field to type in the in
the prompt for the variable maxthrows as the program
is running.
Page 3
Programming the TI-Nspire
Complete as
Request "How many times to throw the coin",
maxthrows
Now, we want to loop, that is use a for .. loop, to do
this press
•
menu b
•
4: Control 4
•
5: For...EndFor 5
Complete the line by typing
For i,1,maxthrows
Insert a couple of blank lines.
Notice the *Unsaved at the top of the screen, this
indicates that we have not as yet saved our document.
We should save the document often and give it a name.
To save the document, press
•
doc ~
•
1: File 1
•
5: Save As ... 5
© R. Rozen
Page 4
© R. Rozen
Programming the TI-Nspire
In the dialog box, type the name "onecoin", tab e to
Save and press enter ·.
To simulate throwing a coin, we will use the randInt
command, we can type this in, or press
• catalogue k
• 1: 1
• R: R
using the down arrows on the Touchpad, select randInt(
then press enter ·.
Complete the line as
coin := randInt(1,2)
This will produce a 1 or 2.
Now, we want use an If…Then…Else...EndIf
command, to do this, press
•
menu b
•
4: Control 4
•
3: If...Then...Else...EndIf
3
Page 5
Programming the TI-Nspire
This will count the number of heads and tails, from the
random number generator. Note that it produces only a
one or a two, so we will assign a one as a head and a
tail otherwise. Complete as
If coin =1 Then
heads:=heads+1
Else
tails:=tails+1
EndIf
Note that in this construct, we are comparing coin to
the value of 1, not storing, so, coin = 1 is correct.
To see the output of the program, we use the Display
command, press
•
menu b
•
6: I/O 6
•
1: Disp 1
Complete the entry line as follows, all on one line, after
the EndFor line.
Disp "In",maxthrows," throws of one coin ",heads,"
heads and ",tails," tails"
Note that strings within quotes appear in the output
directly, while the actual values of the variables will be
shown in the output.
To resave the document, press
•
ctrl /
•
S
S
These and many other commands are similar to those
used with Microsoft applications.
Notice that there is no asterisk next to the document
name at the top of the screen, but there is an asterisk
next to the program name. Note that the program name
and file document name can be the same, however they
don’t have to be.
© R. Rozen
Page 6
Programming the TI-Nspire
We have not as yet checked the syntax of the program,
to do this press
•
menu b
•
2: Check Syntax & Store 2
•
1: Check Syntax & Store 1
Provided you have typed the program in exactly as
displayed, and there are no syntax errors, the message
"onecoin" stored successfully appears. If there are
syntax errors, you will have to edit and correct these.
As a shortcut, notice that
•
ctrl /
•
B
B
will check the syntax and store the program.
However notice now the asterisk in front of the
document name. This now indicates that we have
made changes to the document and not as yet
resaved.
To run the program, we need to move into the
calculator page, to do this, press
•
ctrl /
•
¡
Press h or type
• onecoin()
Then press
•
enter ·
The program should run successfully, with input boxes
appearing and echoed to the calculator page, along with
the final output.
Note that we could modify the Request statement and
place a 0 at the end of the line, now the prompt and
response will not appear in the history in the calculator
page, when the program is run.
Request "How many times to throw the coin",
maxthrows,0
© R. Rozen
Page 7
Programming the TI-Nspire
Of course this is a very simple program, but it
illustrates how to write, edit and save and check the
syntax of a program. It shows how to get input into a
program while the program is running and displays the
output from the program to a calculator page. Any
program must have these basic ideas of obtaining input
from the user, while the programming is running and
giving output back to the user on the calculator page.
With these basic constructs, and controlling the flow of
execution, many more complicated programs can easily
be written.
However the program at the moment can only be run
from within this saved document. To change this, go
back to the program page, by pressing
•
ctrl /
• ¢
Now we can change the library access by pressing
•
menu b
•
1: Actions 1
•
7: Change Library Acccess 7
Using the touchpad ¤, choose LibPub
( Show in Catalogue ), now press
•
•
e
enter ·
tab
Move the cursor to before the first Local, and insert a
new blank line, now press
•
menu b
•
1: Actions1
•
8: Insert Comment 8
© R. Rozen
Page 8
Programming the TI-Nspire
Then type on that line, as shown, after the ©
© Simulates throwing one coin
Now we need to check the syntax and store the
program again
•
ctrl /
•
B
B
We need to save the document in the folder MyLib
•
doc ~
•
1: File 1
•
5: Save As ... 5
Now we need to refresh the libraries
•
doc ~
•
6: Refresh Libraries 6
Now when we open up any new document, and add a
calculator page,
Then press:
•
catalogue k
•
6:
6
Scroll down to the document onecoin, press
•
enter ·
to expand the current problem.
We see that our comment is included as a help, at the
bottom of the screen.
© R. Rozen
Page 9
Programming the TI-Nspire
Pressing enter · again.
The program can now be run from any document and
behaves as any intrinsic function or program.
The program can easily be transferred to other
handhelds as a document, however you will need to
refresh the libraries, on the handheld to run the
program.
Below are some more advanced programs, which also
have some error checking, to ensure valid input. These
also write the output to global lists variables, whose
contents can been seen in the Lists and Spreadsheets,
after the program has finished running. These results
can easily be graphed on a data and statistics page, to
show the output graphically.
© R. Rozen
Page 10
Programming the TI-Nspire
© R. Rozen
Page 11
Program for Throwing Two Coins
Program for Throwing Two Dice
Define twocoins()=
Prgm
Text "Simulates throwing two coins"
Text "Program written by R. Rozen"
DelVar heads,nhead
Local coin1,coin2,i,maxtoss
Loop
Request "How many throws",maxtoss
If iPart(maxtoss)= maxtoss and maxtoss >0
Goto validthrows
EndLoop
Lbl validthrows
For i,1,3,1
heads[i]:=i-1
nheads[i]:=0
EndFor
For i,1,maxtoss,1
coin1:=randInt(1,2)
coin2:=randInt(1,2)
If coin1=1 and coin2=1 Then
nheads[1]:=nheads[1]+1
ElseIf coin1 = 2 and coin2 = 2 Then
nheads[3]:=nheads[3]+1
Else
nheads[2]:=nheads[2]+1
EndIf
EndFor
Disp "No Heads ",nheads[1]
Disp "One Head ",nheads[2]
Disp "Two Heads ",nheads[3]
Text "Open a lists and Spreadsheet
name column A heads, column B nheads"
EndPrgm
Define twodie()=
Prgm
Text "Program to simulate throwing two
dice"
Text "Program written by R. Rozen"
Local i,maxrolls
Local die1,die2,dt
DelVar n,dice
Loop
Request "How many times to throw the
dice ", maxrolls
If iPart(maxrolls) = maxrolls and
maxrolls > 0
Goto validrolls
EndLoop
Lbl validrolls
For i,1,11,1
n[i]:=i+1
dice[i]:=0
EndFor
For i,1,maxrolls,1
die1 := randInt(1,6)
die2 := randInt(1,6)
dt := die1+die2
dice[dt-1]:=dice[dt-1]+1
EndFor
Text "Open a lists and Spreadsheet
name column A n and column B
dice"
EndPrgm
Programming the TI-Nspire
© R. Rozen
Page 12