Day 3 Program

Today we wrote our Nspire version of a dice roll simulation. Here’s an example of the function that simulated 1 or more trials of 6 dice.

Define dice(trials)=
Func
Local i,roll,counts,total_rolls
total_rolls:=6*trials
counts:={0,0,0,0,0,0}
For i,1,total_rolls
roll:=randInt(1,6)
counts[roll]:=counts[roll]+1
EndFor
Return counts
EndFunc

And here’s the program that ran multiple trials to determine the experimental probability of rollling 1,2,3,4,5,6 each the same number of times.

Define rollsim(trials,c)=
Prgm
Local i,counter,roll
counter:=0
For i,1,trials
roll:=dice(c)
If roll={1,1,1,1,1,1}*c Then
counter:=counter+1
EndIf
EndFor
Disp "all the same: "&string(counter)
Disp "EP:"&string(((counter)/(trials))*100.)&"%"
Return ((counter)/(trials))*1.
EndPrgm

Day 2 Program — Guess!

Here’s at least one way to make the program from class. I’ve added some spaces to make it more readable, but they’re NOT required on the calculators.

Define guess()=
Prgm
Local num, trial, guessed, guesses

guesses:=0
guessed:=false

num:=randInt(1,10)
Disp num

While not guessed
Request "Guess (1-10)?",trial,0
guesses:=guesses+1

If trial=num Then
guessed:=true
ElseIf trial<num Then
Disp "Too low!"
ElseIf trial>num Then
Disp "Too high!"
EndIf
EndWhile

local prt
prt := string(guesses)
Disp "You got it in" & prt & " guesses."

EndPrgm

Nspire Programming notes

To create a new program:

• Create a new document
• Insert a program with
• Add Program Editor
• New
• Name your program (no spaces or symbols)
• Choose Function or Program

Remember that you must  pick function or program correctly as you cannot change it later.

Useful functions

• Disp — displays output — Menu,  6, 1
ex: Disp “Hello, world!”       Hello, world!
Disp x                                42
• Mean — calculates the mean of a list of values
ex: Mean({3,4,5})                     4
• Abs — calculates absolute value of a number or members of a list
ex: abs({-3,4,-5})                   {3,4,5}
|{-3,4,-5}|                        {3,4,5}
• String — converts a number to a string to allow concatenation
ex: String(42)              42
Disp “It took ” & string(guesses) & ” tries.”

Variables

• Variables must be declared before use
• Variables can be global or local, but we will only use local
• Local variables exist only inside your program
• Don’t confuse the assignment operator, := , with the comparison operator, =

Parameters/Arguments

• Your program may accept parameters
• Parameters can be
• Numbers
• Lists
• Text
• If the parameter is the wrong type (or missing),
your program won’t execute.

Lists

• Lists are created using curly braces {}
• Lists can contain other objects like text or numbers
• The calculator has a number of functions like Mean, Median, etc. that operate on lists
• To access a specific member of a list, use listname[position]
ex: x := {5,6,7,8}                                       {5,6,7,8}
x                                                          6

Day 1 Program — MAD

Find the MAD of a list like {1,2,3,4,5}

Func
Local mean_values, distances, result

mean_values := Mean(values)

distances := Abs(mean_values - values)

result := Mean(distances)

Return result

Run and see what happens…

Day 1 Program — F to C

This function took a temperature in Fahrenheit as input and output in Celsius.

Define f2c(f)=
Func
Local c
c := 5/9*(f-32)
Return c
EndFunc

Some examples:

f2c(212)               100
f2c({32,68,98.6,212})   {0,20,37,100}