This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's TI-BASIC subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. "Here's the Breakdown." => TI-BASIC
Author Message
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 07 Mar 2010 06:54:10 pm    Post subject:

Everybody's favorite "guess the number" game, written in TI-BASIC:

randInt(1,100→A
Repeat A=B
Input "GUESS? ",B
If A>B
Disp "TOO LOW."
If A<B
Disp "TOO HIGH."
End
Disp "CORRECT!"


In this program, we'll be using A to stand for the number that the calculator is thinking of (i.e., this is the number that we are trying to guess!), and B is going represent the number that we have just guessed. Whether or not it was the right one, we'll still have to distinguish our guess from the correct answer, because there's no way to actually make any comparisons between the two quantities in terms of "higher or "lower" (or "equal" or "unequal") otherwise.

The program starts out by picking a random integer from 1 to 100 and storing it to the letter variable A. The calculator will remember A's value so that we can check our guesses against it later.

Next, the program enters a conditional loop. What this means is that all of the code from the top of the loop (starting at the Repeat) and going down to the end of the loop (at the End) is now going to repeat itself over and over again until the given condition (A=B) is true.

It doesn't matter that B isn't known yet, because the condition is only checked at the bottom of the loop, and we'll definitely know B by then. (Repeat is the only looping instruction which behaves this way, and right now we're using it to our advantage in that we don't need to store anything to B before the loop begins.)

The first thing that happens within the loop is that the calculator will ask us to guess its number. The text between the quotes to the right of the Input command will show up on the home screen, and whatever digits are entered by the user will be stored to B, due to the fact that B is the variable given at the end of the Input. If C were written in place of B in this program, then the data entered by the user would be passed to C instead.

In line four of the program, a test is given: If A>B. Remember that A is the number that we're trying to accurately guess, and B is the one that we just tried. If the mystery number is larger than (greater than) our current guess, then the calculator will tell us that we guessed too low. The line with the Disp—which means "display this text on the home screen"—is only performed if the test above it is true. After lines four and five, another test checks to see if our guess is too high. (If it is, then the appropriate text is displayed.)

In the second-from-last line is an End. This marks the bottom of the Repeat loop – more importantly, this is where the calculator makes the decision to leave the loop based on whether or not the condition at the top of the loop is presently met. Basically, the A=B (the calculator's secret number versus our guess) is either true or false at this point. If it is true, then the program will break free from the loop (even if it has only gone through once, which means, amazingly, that we got the number right on our first try). If it is false and A does not equal B, then we get to start the loop all over again. The next thing that would happen is that the program would ask us to guess its number.

When we make a correct guess and break free from the loop, the next line that the program deals with is the first one to follow the End. All that there is left is for the program to exclaim, "CORRECT!"

So, what happens when you change the 100 in the first line to 1000000? (I'll see you when you're done!)


Last edited by Guest on 01 Jul 2010 07:06:34 am; edited 1 time in total
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement