Ti-83 Programming Tutorials |
|||||
| |||||
Chapter 9 - Lists and MatricesIn chapter 2, we mentioned lists and matrices as some variables we could use. We haven't used them yet since then because simple programs usually don't require lists or matrices. As you begin to program more advanced programs, you'll find the list and matrix can really be of help, especially in programs that require storing blocks of data.Unlike some previous chapters, we will provide coding examples of using the lists and matrices and explain from there what the code does and how we used the list or matrix. This is a simple lottery program, using part of the custom menu engine from chapter 8. 20->C 1->D Lbl 0 ClrHome If 0=C Then Pause "YOU'RE BROKE!" Stop End Output(1,1,"=LUCKY--LOTTERY=") Output(2,1,"****************") Output(3,3,"RANDOM NUMBERS") Output(4,3,"PICK NUMBERS") Output(5,3,"QUIT!") Output(6,1,"----------------") Output(7,1,"CASH:") Output(7,6,C) Output(8,1,"JP:") Output(8,5,iPart(D^.5D)) 1->A Output(3,1,"=>") Repeat K=105 getKey->K) If (K=25) or (K=34) Then Output(A+2,1," ") A-(K=25)+3(K=25)(A=1)+(K=34)-3(K=34)(A=3)->A Output(A+2,1,"=>") End End If A=1 Goto 1 If A=2 Goto 2 If A=3 Stop Lbl 2 ClrHome Disp "VALID RANGE IS","FROM 1 TO 5." For(X,1,5,1) Repeat (Z>0)(Z<6) Disp "","NUMBER "+sub("12345",X,1) Input "?",Z End Z->L1(X) End Goto A Lbl 1 randInt(1,5,5)->L1 Lbl A C-1->C ClrHome randInt(1,5,5)->L2 Output(1,1,"LOTTERY DRAWING") Output(2,1,"****************") Output(7,1,"YOUR NUMBERS:") Output(3,1,"LOTTERY NUMBERS:") Output(5,1,"----------------") For(X,1,5,1) Output(8,2+(X-1)3,L1(X)) End For(X,1,5,1) For(Z,0,200,1) End Output(4,2+(X-1)3,L2(X)) End Pause SortA(L1) SortA(L2) 1 For(X,1,5,1) Ans(L1(X)=L2(X)) End If Ans Then ClrHome Disp "YOU WON! THE","JACKPOT WAS" Disp D^.5D,"DOLLARS!" C+D^.5D->C 1->D Pause Goto 0 Else Output(6,1,"YOU DIDN'T WIN!") D+1->D Pause Goto 0 EndSimple, really. Cash is stored in C (default of 20), and the day the lottery is on is stored in D. An equation of iPart(D^.5D) is used to determine the jackpot amount. The menu system is used to display relevant information and three options: buy a random ticket; choose your own numbers for the ticket; or quitting. However, there isn't an "about" menu, though you can easily modify the program to include one. Note also that we changed the command button to [ENTER] (105). When the user selects a random ticket, one is created with RandInt(min,max,n), where min is the minimum number and max is the maximum number in the range, and n is how many random numbers you want. If n is omitted, then only 1 random number is generated. If n is more than one, then a list with the random number is created. This is your ticket and is stored in L1. If, instead, you choose to pick your own number, then a simple loop is used for inputting 5 numbers, with a range check of 1 to 5. The sub("12345",X,1) is a string function (chapter 10) and means from the string "12345", create a string starting at location X and ending after 1 character. This string is then added to "NUMBER ". The reason for this line of code is because we want to display the "NUMBER " with the number all on one line, but you can add strings and numbers together! The random lottery numbers are then created and stored in L2. A graphical display is showed, with your numbers on the bottom and the lottery numbers appearing one after another. The For(Z,200,1) loop simply delays the program for a bit so the numbers seem to be generated on the fly, even though they are not. The SortA(L1) and SortA(L2) simply sorts the lists in ascending order. We need to do this in order to check if the lists contain the same number (that is, if your ticket matches the lottery numbers). The rest of the logic in the program is pretty much straightforward. Use of the And variable is implemented. If a the numbers in the list are the same (L1(X)=L2(X)) then a 1 would result, multiplying itself with the previous answer (defaulted to 1). If the numbers are not the same, then a 0 would result, making Ans also 0. A simple If-Then statement is then used, giving you money and resetting the day if you win, or displaying the "you didn't win" message and incrementing the day. Program control then jumps back to the first menu. This program is simple, fun, and relatively easy to program. You can see in the program how we've used lists. There are far more functions you can use with lists, found under the [2nd] LIST menus. One last thing to mention is that you can create your own lists using the [2nd] LIST ->OPS B:L character and then the name of your list (5 character max). You can now see how you can implement your own high score list by storing a high score in your own list! |
|||||
|