Algorithm Exercise

We're glad you came by, but you might find what you're looking for elsewhere.

TI-Basic Developer is not the site it once was. While its information on commands and other calculator features remains almost second-to-none, its forum, archives, and even hosting service, Wikidot, have been decaying for years. The calculator community would love to see what you're working on, or help you in your next coding adventure, but TI-Basic Developer is no longer the place to do it.

Instead, you should head over to Cemetech (primarily American) or TI-Planet (primarily international). Both are active, well-established forums with their own archives, chatrooms, reference material, and abundant coding tools and resources. We'll see you there, we hope.

Started by: Sitay Huber Sitay Huber
Date: 09 Mar 2021 16:26
Number of posts: 3
rss icon RSS: New posts
Summary:
I'm trying to understand the basic number search algorithm by programming it on my calc, but I'm having trouble.
Algorithm Exercise
Sitay Huber Sitay Huber 09 Mar 2021 16:26

I'm working on a basic binary number search algorithm. I'm learning computer science, and figured the TI-84 should be able to do this type of algorithm.

You might want to skim the code before continuing, so you understand what I'm saying.

The code runs fine if I say the number ("N") is correct, (press 1, and it displays "Woo hoo!"), or when I tell it the number is lower, (it sets the returned number as the upper limit, and re-runs the equation). But when I try to tell it my desired number is *higher,* it all falls apart, and just returns the same number again. Example:

I set "H" to 100, "L" to 0, and let it run. Calculator returns N, which is 50. When I tell it to go higher, (input "2"), the calculator returns 50 again. The only way I can explain that, is that it's putting "50" in both "H" and "L", which makes no sense. How do I tell it to keep one number the same, and only reset the one I'm telling it to?

Here's the code:
ClrHome
Disp "LOW LIMIT"
Prompt L
Disp "HIGH LIMIT"
Prompt H
Lbl 1
(H+L)/2→N
Disp "CORRECT?",N
Disp "1 IF YES"
Disp "0 IF LOWER"
Disp "2 IF HIGHER"
Prompt A
If A=1
Goto 2
If A=0
N→H
Goto 1
If A=2
N→L
Goto 1
Lbl 2
Disp "WOO HOO!"
Pause
ClrHome

Also, if you know how I could get the calculator to round "N" to the nearest integer, I would appreciate that. Something with the "round(" function, right?

P.S. Please do not tell me to go download an program, or copy someone else's. I'm not doing this to actually utilize program, I'm doing this to learn how these things work.

Last edited on 09 Mar 2021 16:27 by Sitay Huber
by Sitay Huber Sitay Huber , 09 Mar 2021 16:26
Re: Algorithm Exercise
Trenly Trenly 09 Mar 2021 17:23

Welcome to TI|BD

It seems your issue is because an if statement only executes the first line after it unless it is an If-Then-End block. It will always encounter the "Goto 1" before it checks if A is equal to 2

The other problem you will have is with Memory Leaks. If you do put in an If-Then-End, using Goto inside of the "Then" section causes a memory leak. When possible, you should try to use while loops and repeat loops. If you want to see an example of how I would write the algorithm you have, avoiding memory leaks, here it is:

Here is how I would write the algorithm you have, using Piecewise expressions:

ClrHome
0→A
Input "LOW LIMIT: ",L
Input "HIGH LIMIT: ",H
While A≠1
 (H+L)/2→N
 Disp "CORRECT?",N
 Disp "1 IF YES"
 Disp "0 IF LOWER"
 Disp "2 IF HIGHER"
 Prompt A
 N(A=0)+Hnot(A=0)→H
 N(A=2)+Lnot(A=2)→L
End
Pause "WOO HOO!"
ClrHome

Here is another way to do it using the + - and Enter keys for control:

ClrHome
0→K
Input "LOW LIMIT: ",L
Input "HIGH LIMIT: ",H
While K≠105
 mean({H,L→N
 Disp "CORRECT?",N
 Disp "+/-/Enter"
 Repeat sum(K={105,95,85
 getKey→K
 End
 N(K=85)+H(K≠85)→H
 N(K=95)+L(K≠95)→L
End
Pause "WOO HOO!"
ClrHome
Last edited on 09 Mar 2021 17:25 by Trenly
by Trenly Trenly , 09 Mar 2021 17:23
Re: Algorithm Exercise
Sitay Huber Sitay Huber 09 Mar 2021 18:26

Great, thank you very much! I think I understand it better now.

by Sitay Huber Sitay Huber , 09 Mar 2021 18:26
/forum/t-14025186/algorithm-exercise#post-
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.

AltStyle によって変換されたページ (->オリジナル) /