Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: holmes221b on December 11, 2010, 01:35:38 pm

Title: How does this line of code work?
Post by: holmes221b on December 11, 2010, 01:35:38 pm
I was teaching my mom a little bit about using TI-Basic last night while we were waiting for our flu shots, and we ended up trying to reverse engineer one of the programs on my calc--one that I didn't write myself--and we got stuck on this one line:
Code: [Select]
:sum(2(Ans=3)+4(Ans=5
Could someone please explain how this line works for us?
Title: Re: How does this line of code work?
Post by: SirCmpwn on December 11, 2010, 01:36:30 pm
sum( is used to get the sum of a list, that line shouldn't even work O.o
Title: Re: How does this line of code work?
Post by: holmes221b on December 11, 2010, 01:40:07 pm
A list? There's no mention of a list in the other half of the code, though.

Should I post the entire code for the program?
Title: Re: How does this line of code work?
Post by: aeTIos on December 11, 2010, 01:43:56 pm
wow your mom learns basic!? O.O
Title: Re: How does this line of code work?
Post by: DJ Omnimaga on December 11, 2010, 01:44:21 pm
Strange, I tried it on my TI-Nspire running 84+ mode and I get an ERR:DATA TYPE error. Did you run any assembly program recently?
wow your mom learns basic!? O.O
O.O
Title: Re: How does this line of code work?
Post by: holmes221b on December 11, 2010, 01:49:18 pm
wow your mom learns basic!? O.O
We were bored and she had left her Blackberry at the house, so I offered to teach her a little bit about programming calcs while we waited. I never leave the house without my 83+.

The only assembly programming I've ever run on my calc is the one that allows you to have lower-case text.
Title: Re: How does this line of code work?
Post by: aeTIos on December 11, 2010, 01:53:37 pm
haha your like me: completely addicted to calc stuff. in my class they already say things like: "when are you going to use your calc as an internet browser" and "what did you ask for birthday present? a new calc? " lol :w00t:
Title: Re: How does this line of code work?
Post by: willrandship on December 11, 2010, 01:55:35 pm
^ my life story ^

and linux :P
Title: Re: How does this line of code work?
Post by: aeTIos on December 11, 2010, 01:57:07 pm
i dont use linux. i cant get it on my PC, unfortunately :hyper: I :hyper: Lost :hyper: The :hyper: Game :hyper::hyper::hyper::hyper::hyper::hyper::hyper::hyper::hyper::hyper::hyper::hyper:
Title: Re: How does this line of code work?
Post by: FinaleTI on December 11, 2010, 02:00:19 pm
That's an odd line.
From what I can see, unless Ans is a list, you get an error.
So 3:sum(2(Ans=3)+4(Ans=5) gives me an ERR:DATA TYPE, while {3}:sum(2(Ans=3)+4(Ans=5) gives me 2.
It would seem only the first list entry affects it though, as {3,2}:sum(2(Ans=3)+4(Ans=5) just returns 2.
Title: Re: How does this line of code work?
Post by: cooliojazz on December 11, 2010, 02:02:19 pm
Ahh, right, passing a list would do it.  And actually, its not that only the first element matters... try {2,5} vs {2,4} to see what I mean
Title: Re: How does this line of code work?
Post by: holmes221b on December 11, 2010, 02:29:25 pm
haha your like me: completely addicted to calc stuff. in my class they already say things like: "when are you going to use your calc as an internet browser" and "what did you ask for birthday present? a new calc? " lol :w00t:
It took a lot of begging to get my first calc--they didn't like the idea of giving me something that could distract me in classes (I have ADHD, which means I have problems with focusing/paying attention), so it wasn't until 10th grade that I finally got one, when the teacher recommended that we had a calc for graphing (before that I used my mom's old BA-II).

That's an odd line.
From what I can see, unless Ans is a list, you get an error.
So 3:sum(2(Ans=3)+4(Ans=5) gives me an ERR:DATA TYPE, while {3}:sum(2(Ans=3)+4(Ans=5) gives me 2.
It would seem only the first list entry affects it though, as {3,2}:sum(2(Ans=3)+4(Ans=5) just returns 2.
So how does it make a list without making a list?
Code: ("full code") [Select]
:Pause randInt(1,6,5
:sum(2(Ans=3)+4(Ans=5
Title: Re: How does this line of code work?
Post by: cooliojazz on December 11, 2010, 02:31:04 pm
Well, first of all, that pause line should not have changed Ans, second... since when does ranint take three arguments? O_o
Title: Re: How does this line of code work?
Post by: FinaleTI on December 11, 2010, 02:36:25 pm
A third argument makes it create a list of trials, with the third argument being the dimensions of the list/number of trials.
Title: Re: How does this line of code work?
Post by: aeTIos on December 11, 2010, 02:40:07 pm
ah, so the no-list->list problem is solved??
Title: Re: How does this line of code work?
Post by: holmes221b on December 11, 2010, 02:44:42 pm
So that's where the list comes from...

My mom just texted me to remind me about the specific part of that line of code that she was particularly confused by. She wants to know what "Ans=3" and "Ans=5" means.
Title: Re: How does this line of code work?
Post by: aeTIos on December 11, 2010, 02:45:25 pm
wow you have a cool mom
mine never would do that
Title: Re: How does this line of code work?
Post by: holmes221b on December 11, 2010, 02:51:05 pm
wow you have a cool mom
mine never would do that
She won't let me get a link cable unless I buy it with my own money or figure out how to make my own (again, with my own money), though.
Title: Re: How does this line of code work?
Post by: Quigibo on December 11, 2010, 04:32:58 pm
When you have a list of numbers, say you had {1,2,3,4,5,6} and you do Ans=3, it would test if each element is 3 so you would get {0,0,1,0,0,0}.  2(Ans=3) would give: {0,0,2,0,0,0} since every element gets multiplied by 2.  4(Ans=5) would similarly give: {0,0,0,0,4,0}.  Adding each element together: {0,0,2,0,4,0} then doing a sum adds all the elements together so you'd end up with 6 as your answer.

Basically it is generating a random even number with a weird distribution where the average value is 6, minimum value is 0 and maximum value is 24.
Title: Re: How does this line of code work?
Post by: holmes221b on December 11, 2010, 04:38:05 pm
When you have a list of numbers, say you had {1,2,3,4,5,6} and you do Ans=3, it would test if each element is 3 so you would get {0,0,1,0,0,0}.  2(Ans=3) would give: {0,0,2,0,0,0} since every element gets multiplied by 2.  4(Ans=5) would similarly give: {0,0,0,0,4,0}.  Adding each element together: {0,0,2,0,4,0} then doing a sum adds all the elements together so you'd end up with 6 as your answer.
Basically it is generating a random even number with a weird distribution where the average value is 6, minimum value is 0 and maximum value is 24.
Oh wow, that's pretty cool.

And my mom says "thanks".
Title: Re: How does this line of code work?
Post by: AngelFish on December 11, 2010, 04:40:01 pm
So that's where the list comes from...

My mom just texted me to remind me about the specific part of that line of code that she was particularly confused by. She wants to know what "Ans=3" and "Ans=5" means.

Ans=3 just returns 1 if the data in Ans is equal to 3 and 0 if it does not.
Title: Re: How does this line of code work?
Post by: meishe91 on December 11, 2010, 06:56:01 pm
Ya, Quigibo got that explained perfectly. Anything with a = (equal) sign is a Boolean conditional in that it is tested to see if the statement is true or false. If the statement is true then it returns a one and if it is false then it returns zero. When that happens it can then be used in math (which is a good way of avoiding using a bunch of If statements).

As for randInt( it has always taken three arguments :P The first being the lowest number possible, the second being the highest number possible, and then the third is the number of times the the command runs (returning the answers in the form of a list). So randInt(1,10,5 could give an answer like {3,7,5,3,2}. In this case the list you get could be something like {1,3,3,6,5}. When you apply the sum( to it all your final answer would be eight.

Code: [Select]
sum(2({1,3,3,6,5}=3)+4({1,3,3,6,5}=5

sum(2{0,1,1,0,0}+4{0,0,0,0,1

sum({0,2,2,0,0}+{0,0,0,0,4

sum({0,2,2,0,4

8