ti-nspire programming - ti-basic developer

14
TI-83 Basic Home 68k TI-Basic Home TI-Nspire Basic Page Program Archives Discussion Forums Preparation The Calculators Why TI-Basic? Using This Guide TI-Basic Starter Kit TI-Basic FAQ Main Content Commands Development Techniques Miscellaneous References Resources Downloads Showcases Sidebar Token Size Text Sprites Binary & Hex TI-83+ Font Error Conditions File Extensions Key Codes Abbreviations Glossary + Add Page TI-Nspire Programming The TI-Nspire is a unique calculator in the BASIC series. The calculator's programming system is remarkably similar to that of the 68k's hardware, but because the Nspire's internal core is different, it needs a section on its own. Unlike all the older TI versions, the Nspire does not have a home screen or a graph screen. Instead, the calculator runs on a document that has five applications: Calculator, Graph and Geometry, Spreadsheet, Notes, and Statistics. The programming operates under the Calculator application. Although the Nspire makes good use of form and screen resolution, the programming is very limited compared to older versions. This article is meant to give you a brief overview of the programming in the Nspire and how older versions are preferable if you want to program. Fold Table of Contents The Nspire: Introduction Pros and Cons Creating a Program or Function The Commands Define Variables Control Transfers I/O Mode Gaining Input Programming Math Programs Probability Programs Game Programs A Game Example Break a Calculation Forum Further Research The Nspire: Introduction The Texas Instruments Nsipre is an upgraded version of all the other TI models. It uses very much the same type of formatting as the 89 family but does not have a CAS. When first observing the calculator, you will notice the keypad. It is highly different from any other calculator. It has a very strange layout compared to the other TI graphing calculators. First, all the letters get their own buttons, making it a little easier to type. Also, it has a ctrl key rather than a 2nd key. It has a clicker key, menu key, catalogs, enter, and even a caps key. If you observe it long enough, it begins to resemble a keyboard. This is because the Nspire is designed to be like a computer in a way. It uses documents to store information. These documents hold information about anything you did within it, not just programs. You can choose to save it, or load different documents, a lot like a computer. Also, the Nspire utilizes a mouse type system. It replaces the arrow keys with one round button. It acts as the arrows, and as a way to move a mouse-like pointer across the screen. The Nspire is a transition of a calculator to a computer, yet held back enough to retain calculator status. The second thing you would notice is the screen resolution. Very nice! The screen is much better than any other calculator in history. The screen displays curves very well, it graphs cleanly, it uses letters and other characters with different size (much like a computer font style), and it is bigger. Also, the screen has many different shades of gray. In using inequalities, the calculator can actually shade in the region instead of drawing lines that represent it, and it darkens regions of overlap. The screen is very nicely done. The final most notable thing is you might get lost on where every function is. The calculator has a very different key layout, and it also has a different internal layout. You might have trouble finding where some of the functions are. This article does not discuss how to find different functions, but it discusses the programming attribute which is the main topic of this site. For finding various functions, visit the Catalog (the book button) or check your CD manual. It has loads of information you can use. Membership Editing Pages Site Information Related Sites Search this site Search Create account or Sign in

Upload: helloworld124

Post on 30-Sep-2014

99 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: TI-Nspire Programming - TI-Basic Developer

TI-83 Basic Home68k TI-Basic Home

TI-Nspire Basic Page

Program Archives

Discussion Forums

Preparation

The Calculators

Why TI-Basic?

Using This Guide

TI-Basic Starter Kit

TI-Basic FAQ

Main ContentCommands

Development

Techniques

Miscellaneous

References

Resources

Downloads

Showcases

Sidebar

Token Size

Text Sprites

Binary & Hex

TI-83+ Font

Error Conditions

File Extensions

Key Codes

Abbreviations

Glossary

+ Add Page

TI-Nspire Programming

The TI-Nspire is a unique calculator in the BASIC series. The calculator's programming system is remarkably similarto that of the 68k's hardware, but because the Nspire's internal core is different, it needs a section on its own.Unlike all the older TI versions, the Nspire does not have a home screen or a graph screen. Instead, the calculatorruns on a document that has five applications: Calculator, Graph and Geometry, Spreadsheet, Notes, andStatistics. The programming operates under the Calculator application. Although the Nspire makes good use of formand screen resolution, the programming is very limited compared to older versions. This article is meant to give youa brief overview of the programming in the Nspire and how older versions are preferable if you want to program.

FoldTable of Contents

The Nspire: IntroductionPros and ConsCreating a Program or FunctionThe Commands

Define VariablesControlTransfersI/OMode

Gaining InputProgramming

Math ProgramsProbability ProgramsGame Programs

A Game ExampleBreak a CalculationForumFurther Research

The Nspire: IntroductionThe Texas Instruments Nsipre is an upgraded version of all the other TI models. It uses very much the same type offormatting as the 89 family but does not have a CAS. When first observing the calculator, you will notice thekeypad. It is highly different from any other calculator.

It has a very strange layout compared to the other TI graphing calculators. First, all theletters get their own buttons, making it a little easier to type. Also, it has a ctrl key ratherthan a 2nd key. It has a clicker key, menu key, catalogs, enter, and even a caps key. If youobserve it long enough, it begins to resemble a keyboard. This is because the Nspire isdesigned to be like a computer in a way. It uses documents to store information. Thesedocuments hold information about anything you did within it, not just programs. You canchoose to save it, or load different documents, a lot like a computer. Also, the Nspire utilizesa mouse type system. It replaces the arrow keys with one round button. It acts as thearrows, and as a way to move a mouse-like pointer across the screen. The Nspire is atransition of a calculator to a computer, yet held back enough to retain calculator status.

The second thing you would notice is the screen resolution. Very nice! The screen is muchbetter than any other calculator in history. The screen displays curves very well, it graphs

cleanly, it uses letters and other characters with different size (much like a computer font style), and it is bigger.Also, the screen has many different shades of gray. In using inequalities, the calculator can actually shade in theregion instead of drawing lines that represent it, and it darkens regions of overlap. The screen is very nicely done.

The final most notable thing is you might get lost on where every function is. The calculator has a very different keylayout, and it also has a different internal layout. You might have trouble finding where some of the functions are.This article does not discuss how to find different functions, but it discusses the programming attribute which is themain topic of this site. For finding various functions, visit the Catalog (the book button) or check your CD manual. Ithas loads of information you can use.

Membership Editing Pages Site Information Related Sites

Search this site Search

Create account or Sign in

Page 2: TI-Nspire Programming - TI-Basic Developer

Pros and ConsThe programming for the Nspire is very similar to the 68k series. However, the Nspire simply isn't part of it. For onething, the Nspire has no graph screen, or even a home screen. Everything is formed with documents, problems, andpages. You can have a document, problems within that document, and pages within those problems. Even thougheverything within a document is connected in some way, the calculator doesn't seem capable of accessing otherpages or problems without manual control. Because of this limitation, the calculator has no graphing commands. Itlost commands like Pt-On( and Line(. Anything under the Draw category has been deleted. The Draw command wasthe basis of advanced graphics, so that means the Nspire cannot produce any games with any sort of graphics,even though its resolution is so many times better than that of older models.

Furthermore, the calculator has no home screen. Instead, it uses a very strict line by line layout. The calculatordeals with an operation or command very systematically. Until the calculator completes an assigned command oroperation, it will not let the user interfere. New inputs are automatically in a new line. This sounds a lot like othercalculators completing operations without interruption. However, this becomes a huge problem when pertaining toprograms. The Nspire does the same thing to programs; it doesn't allow user interference. That means that thecalculator will not accept user input until the program is finished. That defeats the purpose of programs. If you can'tinput anything, what use is programing? It happens that the program accepts input before a program is executed,but not during. So, to make a program with the same effects of programs in 68k or 83 family BASIC, you have tocreate multiple programs that must each be individually accessed, which becomes incredibly cumbersome.

So, the calculator is terrible at programming. It can't produce graphics or advanced layouts, and it won't acceptinput. So what use is programing on an Nspire. Truthfully, if you want to program, don't get the Nspire. Simply put,there is no real way to work around these obstacles. However, the regular Nspire comes with an 84+ keypad. It hasall the capabilities of a regular TI-84+. Thing is, even though the calculator has capabilities of using extremeresolution, the 84+ keypad doesn't utilize it. So it's still not all that good.

So what if I already have an Nspire? Don't worry. If it comes with the 84+ keypad, go ahead and use that toprogram. In any case, the Nspire is definitely superior in math. Through sacrifice of programing capabilities, it hasgained many useful math tools. For example, it checks primes, has trigonometric functions like secant andcotangent, a newly optimized numerical solver, point plot er, geometry grapher, spreadsheets, and more. It evenutilizes templates, so instead of writing a math equation like ((3+4)/(5(6+3)) 4̂, the calculator can write it as

instead. The Nspire also can hold numbers way higher than 1E100. So, it is superior in math (which can

be a real big help on math tests!), but not a good programming apparatus.

This article will further discuss the topic of programming on an Nspire calculator describing how to create programs,describe all the commands, ways to get input, ways around the major obstacles, and a game example in the end.

Creating a Program or FunctionTo create a program, make sure you are in the Calculator application. Press Menu and go to 8. Then, press 1 and 1again. A window will pop up prompting the name of the program. Put in the name, select the type (program orfunction) and press OK. Now, you are in the program editor and you can now create your program.

There is are differences between programs and functions. Depending on what you are doing with the program, youwill want to select one of them.

Program Function

Return a result? No Yes

Can be used in an expression? No Yes

Can run in applications other than Calculator? No Yes

Can use all the commands? Yes No

Can use multiple variable types? Yes Yes

Can access global variables? Yes Yes

Can modify and create global variables? Yes No

Can call subprograms? Yes No

Can call functions? Yes Yes

A program is what the other calculators typically use. The function can be used inline in an expression, such as5+func(7)-2. However, functions do not support most of the commands on this page.

Format

The format of the TI-Nspire is a little more organized than the 83 series. The program editor keeps much the sameformat except it indents for every group of commands. For example, it indents If…Then blocks, While…EndWhileblocks, and others.

1) 23&4) 1'

2

Page 3: TI-Nspire Programming - TI-Basic Developer

Def ine t es t (a,b)=PgrmIf a=1 Then…Disp 1…c:=5ElseIf a=2 Then…Disp 2…While b>0……b:=b+2c……Disp b…EndWhileElseIf a=3 Then…Disp 3EndIfEndPgrm

Variables on the Nspire function in much the same way as 68k variables. A variable can be a string of letters. Anundefined variable is italicized. For example, if the variable height was undefined, or had nothing stored to it yet,then on the calculator the variable would look like height. Defined variables are bolded, so if height was defined,it would appear as height. Commands are neither bold or italicized, and you cannot store information into them.

Execution

To execute the program, go to the Calculator application. You must then type out the entire program name. Then,put parentheses with values that must be defined for the program within them. The syntax for the above would looklike:

t e s t (2, -3)

You could also select the program from the var button.

The CommandsMost of the commands on the Nspire are the same or variations of the 68k commands. However, the Nspire lacksmost graphical commands, and some variable commands are limited. Some commands require a Computer AlgebraSystem, or CAS, so if your Nspire doesn't have CAS, many of the commands will not function.

There are a few commands for exclusive use in programming. These commands help direct how the program worksso that it can complete various tasks. The commands are divided into categories based on their overall function.

Define Variables

These commands have to do with the variables. Commands listed here will affect the way the program treatsvariables.

Local

Local is a command that tells the calculator how to identify the variable that comes after it. Local tells the programto make the variable a temporary variable that only functions within the program itself. So, when you local a variable,that variable is only used within the program, and if that same variable is used anywhere else, you will find that itwas not affected. Here is the syntax.

Local varname

The varname can be any variable name or type. This command is useful in the sense that it won't mess with yourprevious usage of the variable. If you want to use the variable name, but it is used in other programs and you don'twant your program to mess it up, use local. If there is a case where you want the variable to be altered globally,then don't use local, and the calculator will treat it like any other number.

Define

The command Define will allow you to use subroutines within a program. This command creates a new program orfunction within the program so that the subroutine can be used again. For it to work, you must have either Pgrm…

Page 4: TI-Nspire Programming - TI-Basic Developer

EndPgrm or Func…EndFunc for it to work.

Def ine t es t 2 (x ,y )PgrmDisp x ,yEndPgrm

Delete Variable

This command will delete a variable making it undefined. This command will not set a number equal to 0, but itinstead changes the variable to become unusable until defined later with a store command.

DelVar he i gh t

This command would be good to use when cleaning up a program. If you don't want your variables to have valuesafter a program finishes, use DelVar to reset the variables.

Func…EndFunc and Pgrm…EndPgrm

These sets can be used to create subroutines when used with Define. Func…EndFunc will create a functionwhereas Pgrm…EndPgrm will create a program. Use these after a Define in order to create the program which canbe defined later in the main program.

Def ine t es t 2 (x ,y )PgrmDisp x ,yEndPgrm

Control

These are the main commands that will be used. The control commands control the flow of the program affectingwhat group of commands to activate or how many times to commence a group of commands.

If

This is exactly like the If condition used in other BASIC languages. You put a condition after the If, and if thecondition is true, the next line will be executed. If it is false, it will be skipped. Here is the syntax.

If he i gh t >0he i gh t *w i d t h→area

This is very, very useful in programming, if not essential. The If command makes games possible. It detects limits,exacts, and special circumstances, which can affect the reality of any program.

If…Then…EndIf

In BASIC, this is the equivalent of the If:Then combination. A condition goes after the If. If the condition is true,everything after the Then until the EndIf will be executed. If it is false, the calculator will begin at the EndIf andexecute those commands. Here is an example.

If h=3 Then…3+3→a…a+h→c…c/h+a→hEndIfDisp h

In this example, if h is 3, then the number displayed will be 9. If h isn't three, then it will display any number that isnot three. Again, this conditional command is highly valued in programming. This is a must in programming! Thisalso determines limits and special instances, but this opens a new field of the programing.

Page 5: TI-Nspire Programming - TI-Basic Developer

If…Then…Else…EndIf

We probably don't need to tell you that this is the same as in BASIC. In this, it is an If conditional followed by Thenand Else. If the condition after the If is true, then everything after the Then will be executed, but everything after theElse will be skipped. If the condition was false, then everything after the Then is skipped, and everything after theElse is executed. Here is an example.

If a>b Then…Disp "Yes"Else…Disp "No"EndIf

In this code, if a is greater than b, the program will say "yes", otherwise, it will say "no". This command is veryuseful. It is an efficient way to test something, and then have the program do one thing or the other depending on itsfallacy. This command control is a very good way to maneuver your program, and it is highly recommended that thisbe used.

ElseIf…Then

This is a special form of the If…Then…Else…EndIf command. This command allows you to test multiple differentoptions instead of only one. This command must come after an If…Then, and you may have a lot of ElseIf's. First, itwill test if the first condition is true. If it is, everything after Then is executed, and the program goes to EndIf at thefirst ElseIf it encounters. If it is false, the condition after the next ElseIf is tested. It does this until all ElseIfconditions are tested or one is true, and the following commands are executed.

If a=1 Then…Disp "a is one"…4→cElseIf a=2 Then…Disp "a is two"…2→cElseIf a=3 Then…Disp "a is three"…8→cEndIf

This code tests the value of a three times. First it finds if a=1. If it does, the text "a is one" will appear, and c willbecome four. Then, it proceeds after EndIf. If a is not one, then the next condition a=2 is tested. If a is two, then thetext "a is two" is displayed and c becomes 2. This is how the ElseIf works.

The ElseIf is a lot like the If…Then…Else…EndIf. These two codes are the same.

If a>6 Then…2h+3→c…a+2-h→d…Disp "Next move?"Else…Disp "You Lose"…StopEndIf

If a>6 Then…2h+3→c…a+2-h→d…Disp "Next move?"ElseIf a≤6 Then…Disp "You Lose"…StopEndIf

For…EndFor

This is a calculator loop. This command, when used as For variable,start,stop,optional increment, will start withstart, store that into variable, and execute every command until it reaches EndFor. Then, the program will go backto For, and add optional increment to variable and does do until it reaches the number stop. After that, it will end theloop and start executing commands after EndFor. The optional increment is an optional input, and the program willassume it is 1 unless stated otherwise. Here is an example of a For loop in action.

For a,1,11,2…Disp a2

Page 6: TI-Nspire Programming - TI-Basic Developer

EndFor

This code will display 1, 9, 25, 49, 81, and 121. This is because it starts with a as 1, then a as 3, then a as 5, andso on.

While…EndWhile

This command is another type of loop. This loop requires a conditional after While. It loops for as long as thatcondition is true, and the loop ends when that condition becomes false. For the loop to be executed, the conditionmust initially be true. Also, make sure the variables within the condition change otherwise the loop will becomeinfinite.

0→xWhile x<5…Disp x…x+1→xEndWhileDisp x

This loop will display 0, 1, 2, 3, 4, and then 5. The loop ends when x becomes 5, and it displays the numbers from0 to 4 because the starting number is 0. While loops are essential to game creating or animation.

Loop…EndLoop

This command creates an infinite loop. This type of loop requires no arguments and will not stop unless you insert aStop, Exit, or Goto. To exit the loop, it is best to create conditionals within the loop to signify when to jump out ofthe loop.

0→xLoop…Disp x…x+1→x…If x>5…ExitEndDisp x

This code displays the same thing as the While…EndWhile loop above. The only real difference is that with Loop…EndLoop, you can create a conditional within the loop where the While…EndWhile only checks the conditional atthe EndWhile.

Try…Else…EndTry

This command allows you to literally "try" a group of commands, and if they error out, the ones after Else arecommenced. If an error occurs in the Try part, then the calculator recovers from the error and picks up at the Else.

DelVar bTry…6+b→c //note that b is undefinedElse…6+7→cEndDisp c

The program displays 13. The program first needs to try 6+b→c, but since b is undefined, the program errors.However, since it is in a Try block, it skips to the Else and tries 6+7→c which works. So, it displays c which is 13.

ClrErr

This command clears the current error and sets the error system variable to zero.

Page 7: TI-Nspire Programming - TI-Basic Developer

PassErr

PassErr will pass an error to the next level of a Try…EndTry block.

Transfers

The transfer commands direct what a program does without the need of an input. These commands simply activatewhen executed and tell the program specifically what to do.

Return

This command will exit out of a subroutine and progress directly back to the parent program.

Cycle

The Cycle command acts like an early End in the program loop. When encountered, it immediately shoots back tothe beginning of the loop. When the loop ends, it doesn't go to Cycle, but it instead goes to the correct End.

Local a0→aWhile a<10…Disp a…a+1→a…If a>5…Cycle…a+1→aEndWhile

This code displays 0, 2, 4, 6, 7, 8, and 9. What happens is a+1→a occurs twice thus making a+2→a. However,when a becomes greater than five, the second a+1→a does not occur because the Cycle makes the loop restart.

Exit

This command will immediately exit the current loop you are in. So, it stops the loop and starts where the End is.This command is used to escape Loop…EndLoop's, but it can be used with For…EndFor and While…EndWhile. Itrequires no arguments.

0→xLoop…Disp x…x+1→x…If x>5…ExitEndDisp x

Lbl

This places a Label in a program. A label is like a flag or marker in a program that can be used as a location bywhich a program can go to using goto. Labels have a name after them, like Lbl here, When the programencounters a goto name, it starts looking for the label with that specified name. When it does, the program resumesat that point.

Go to Lbl

This will create a Goto in the program that will send the program out to find the matching label. This can be used tojump entire sections of the program or be used as a loop.

Local arandInt(1,2)→aIf a=1Goto 1Disp "Heads"

Page 8: TI-Nspire Programming - TI-Basic Developer

StopLbl 1Disp "Tails "

This is a simple coin program that uses Goto. First, a random number is stored to local variable a. If a is 1, then itis tails. The program encounters Goto 1 and so it goes to Lbl 1. From there, it finishes the program. If a is two, thenGoto 1 isn't encountered, so it displays the Heads and stops.

Stop

This command halts the program. That is all it does. When the program encounters this command, it willcompletely end the program wherever it is.

3:=aStopDisp "How did you get here?"

This code stores 3 into a and ends. The code will not give any output at all. This is useful when you reach a pointwhere the user inputs and ending command (other than ON) so you can use an If type statement to end theprogram immediately. Then, you can put stuff after it in case the user did not want it to stop. Stop is also redundantat the end of a program.

I/O

There is only one command in this category, and it is a simple output command.

Disp

This command displays whatever comes after it on the screen. This is what you see when you run a program; it iswhat the calculator produces as a result of the internal commands and calculations. Simply, put whatever you wantto show after the Disp. If you use quotations, it will display the literal text. If there are no quotations, then thecalculator will attempt to solve the expression you put.

Disp ab+aDisp "Hello"

This code will display the answer to ab+a and then display "Hello" after it. This command is needed unless you aimfor the program not to tell the user anything. So use this command, because it is the only way to convey amessage to the user.

Mode

The mode commands are the ones that change the overall output and computing system of the calculator. Thisincludes changing things from degrees to radians, and from rectangular to polar. Under normal circumstances,these commands would probably not be used unless you are making a math program.

Gaining InputGetting user input on the Nspire is rather difficult and highly limited. The main problem is that the only I/O commandis Disp. Unlike the TI-83 series or 68k, the Nspire doesn't have Input, Prompt, or getKey. The lack of these threecommands makes the programming fairly weak on the calculator even though it has dozens more capabilities.Rather, what TI has done, is they expect the user to input the variables before program execution, and that istypically the one and only way Nspire programs will receive input. The problem becomes the user not knowing whathe/she is inputting.

There are three ways to receive input. The first way is to simply have the user store desired values into all thevariables before executing the program. For example, say that a program uses the variables width and height. Theuser can store what he or she wants to be the width or the height by using the syntax value→height. If there isalready something in the variable, then it would look more like value→height. Then, the user can execute theprogram.

The second way exists within the reason for the ending parenthesis during execution. In the program editor, theprogrammer can go to the top line where the Define Program() is. Then, you put the variables you want defined inthe parenthesis separated by commas. So, still using width and height, the top line would look like: DefineProgram(width,height). Then, the user types out the program name with the parenthesis, but instead of leaving the

Page 9: TI-Nspire Programming - TI-Basic Developer

parenthesis blank, the user would put the values he wants to assign width and height in the parenthesis separatedby commas.

The final way is a way to get input during a main program. Unfortunately, the only way to receive input midexecution is the completely stop the program and let the user activate another program (or the same one) with thenew values. This of course is not all that user-friendly. It can also be frustrating having to redefine a program everytime.

ProgrammingIn truth, the only real application for programs is the use of math and probability programs. Although the thought ofgames is conceivable, it would be futile to even attempt making something even like pong or another simple game.Even creating a guessing game is almost impossible. Even so, programming does have the application of helpingyou with your math or giving you an idea of likelihoods.

Math Programs

Making a math program can be pretty useful. These programs are the ones that ask for an input and begin solving.This can be easy to program because most math programs only require one input at the beginning. One suchprogram would be a quadratic equation solver.

To make a math program, you must first determine what you are solving for and what variables are being given. Forour quadratic equation solver, we want the two roots, determinant, and vertex when inputting a, b, and c whenax2+bx+c=0. The user must input a, b, and c at the beginning of the program. Since that is the only input, this canbe done easily. Define the program and put the variables within the parentheses.

Def ine quad ra t i c (a,b,c )Pgrm

EndPgrm

The next step is to plug in your formulas. We want the calculator to solve for the roots with one special condition:For two roots, it must display both roots and "Two Solutions", for one root, the answer and "One Solution", andwhen both roots are complex to display a solitary "No Solution". We must first find the determinant which will tell ushow many solutions will result.

Def ine quad ra t i c (a,b,c )Pgrmb2-4a*c→dEndPgrm

Now, we can add our conditionals with If…EndIf blocks.

Def ine quad ra t i c (a,b,c )Pgrmb2-4a*c→dIf d>0 Then…Disp "Two Solut ions"

…Disp

…Disp EndIfIf d=0 Then…Disp "One Solut ion"…Disp EndIfIf d<0Disp "No Solut ions"Disp "Determinant"Disp dEndPgrm

That finishes the program.

‚ ; ) =È0:

‚ ; ‚ =È0:

‚ ;0:

Page 10: TI-Nspire Programming - TI-Basic Developer

Probability Programs

This is probably the most conceivable types of programs you can make on an Nspire. This would be the type ofprogram where you experiment to define the likelihood of occurrences under certain conditions. This would be likespinning a spinner 1000 times or flipping a coin 25 times. These programs are easy to make because all you needis a random command and a For loop. For this program, we are going to say we need to spin a spinner 100 timeswhere the blue section has a theoretical probability of 0.5, the yellow 0.25, and the red 0.25. First, define yourprogram. The user doesn't need any variables defined, so we will just close the parentheses on the first line.

Def ine s p i nne r ()Pgrm

EndPgrm

Now, we want to define our variables within the program, so we need to local them. We want our variables to benamed blue, red, and yellow. Also, initialize them.

Def ine s p i nne r ()PgrmLocal blueLocal redLocal yel low0→blue0→red0→yellowEndPgrm

Next, we need to start the For loop. Since we want 100 trials, we will start at one and end at 100.

Def ine s p i nne r ()PgrmLocal blueLocal redLocal yel low0→blue0→red0→yellowFor a,1,100…EndForEndPgrm

Now, use randInt() to find the color landed on. Since we are dealing with fourths, use numbers 1 to 4.

Def ine s p i nne r ()PgrmLocal blueLocal redLocal yel low0→blue0→red0→yellowFor a,1,100…randInt(1,4)→spinEndForEndPgrm

Next, we need to set up each conditional so that if the variable spin is 1 or 2, blue goes up one, if it is 3, yellow isincremented, and if it is 4, red is incremented. We can use the ElseIf commands for this.

Page 11: TI-Nspire Programming - TI-Basic Developer

Def ine s p i nne r ()PgrmLocal blueLocal redLocal yel low0→blue0→red0→yellowFor a,1,100…randInt(1,4)→spin…If spin<3 Then……blue+1→blue…ElseIf spin=3 Then……yellow+1→yellow…ElseIf spin=4 Then……red+1→red…EndIfEndForEndPgrm

Now, the last step is outputting the results. Simply create a list of displays to show the number of spins.

Def ine s p i nne r ()PgrmLocal blueLocal redLocal yel low0→blue0→red0→yellowFor a,1,100…randInt(1,4)→spin…If spin<3 Then……blue+1→blue…ElseIf spin=3 Then……yellow+1→yellow…ElseIf spin=4 Then……red+1→red…EndIfEndForDisp "Yellow"Disp yel lowDisp "Blue"Disp blueDisp "Red"Disp redEndPgrm

Game Programs

The game program is very hard to accomplish in Nspire Basic. These types of programs will usually take more thanone program to accomplish in order to accept more user input. However, a game such as "Guess my Number" canbe done in one program.

The general strategy is to create a starter program that sets up the game. Then, every time user input is required,separate it into another program and leave a display telling what the next program is. For one program, initially starta global variable with a strange name so the program can recognize if you are starting a new game.

Here is a game where you have to guess a number between 1 and 100 in seven tries. First, you need to createvariable gmn as 0. This variable will be our deciding number that finds what try you are on and whether a new gamestarts or not.

Create the program. Variable a will be the user guess.

Page 12: TI-Nspire Programming - TI-Basic Developer

Def ine gues s (a)Pgrm

EndPgrm

Define what happens if gmn is 0. The program stores a random number between 1 and 100 into b. Then, 1 is storedinto gmn to represent guess 1.

Def ine gues s (a)PgrmIf gm n=0 Then…Disp "Guess a number between 1 and 100"…Disp "Type guess( ) with the guess in the parentheses"…randInt(1,100)→b…1→gm n…StopEndIfEndPgrm

This next part says that if your guess, a, is larger than b to display guess lower. If it is smaller, guess larger. If a isequal to b, then it displays "You Win!" and the gmn resets to 0.

Def ine gues s (a)PgrmIf gm n=0 Then…Disp "Guess a number between 1 and 100"…Disp "Type guess( ) with the guess in the parentheses"…randInt(1,100)→b…1→gm n…StopEndIfIf a>b Then…Disp "Lower"ElseIf a<b Then…Disp "Higher"ElseIf a=b Then…Disp "You Win!"…0→gm n…StopEndIfEndPgrm

Finally, the program will check if you won or not. If the gmn variable (our turn keeper) goes higher than 7, theprogram ends and gmn is reset to 0.

Def ine gues s (a)PgrmIf gm n=0 Then…Disp "Guess a number between 1 and 100"…Disp "Type guess( ) with the guess in the parentheses"…randInt(1,100)→b…1→gm n…StopEndIfIf a>b Then…Disp "Lower"ElseIf a<b Then…Disp "Higher"ElseIf a=b Then…Disp "You Win!"…0→gm n…StopEndIf

Page 13: TI-Nspire Programming - TI-Basic Developer

gm n+1→gm nIf gm n>7 Then…Disp "You Lose."…Disp "Answer was"…Disp b…0→gm nEndIfEndPgrm

A Game ExampleThis game, called FighterX, is a test against the calculator to see how well of a guesser you are. It is a relativelysimple program where all the user does is guess a number from 1 to 3. If you guess the number the calculator wasthinking of, the calculator loses HP. Otherwise, you lose HP. Try to understand the code.

Program 1

Def ine f i gh t e rx ()Pgrm100→you100→calcDisp "Are you ready?"Disp "Type gs( ) with your guess in the ( ) to begin"EndPgrm

Program 2

Def ine gs (g)PgrmrandInt(1,3)→gcIf g=gc Then…c a l c -randInt(5,13)→c a l cElse…y ou -randInt(2,7)→y ouEndIfDisp "You"Disp y ouDisp "Calc"Disp c a l cIf y ou<1 Then…Disp "You Lose"…DelVar y ouElseIf c a l c <1 Then…Disp "You Win!"…DelVar c a l cEndIfIf y ou>0 and c a l c >0Disp "Type gs( ) and guess again"EndPgrm

Break a CalculationThe Nspire version of the ON-Break is a little different than for the 83/84 series. For the Nspire, you must press andhold the On button and wait until it asks for confirmation. Then, you can break the calculation. This is useful in theevent that an overly complicated function is being performed and you want the calculator to stop thinking so that youcan do something else, without having to reset the calculator or remove batteries.

ForumTo visit the TI-Nspire forum, click here. If you have any questions about the Nspire programming or calculator itself,please ask in the forums, and someone will help you as soon as possible.

Further Research

Page 14: TI-Nspire Programming - TI-Basic Developer

Help | Terms of Service | Privacy | Report a bug | Flag as objectionable

You can discover much about the Nspire and its commands through the 68k portion of this site since they are quitesimilar.

TI-Basic Developer © 2006-2009 — "A wiki for the TI-Basic beginner and advanced alike!"

Powered by Wikidot.com