Variables find the lowest
Special_K
Posts: 162
is there a way to put 5 or more variables into a table and then lookup the lowest value
LOOKUP find_lowest,[noparse][[/noparse]varx,vary,varz,varp,vard,varq,var1,var2,var2,var3], 'chose lowest
or could I use the branch statement.
I know I can do this with a bunch of if then statments but I know there has got to be a better way. the variables will change rather rapidly so the progran need to determin the losest variable and return to the main code.
any help would be great.
LOOKUP find_lowest,[noparse][[/noparse]varx,vary,varz,varp,vard,varq,var1,var2,var2,var3], 'chose lowest
or could I use the branch statement.
I know I can do this with a bunch of if then statments but I know there has got to be a better way. the variables will change rather rapidly so the progran need to determin the losest variable and return to the main code.
any help would be great.
Comments
· LowVal = 0
· FOR I = 1 TO 10
··· IF MyData(I) < LowVal THEN
····· LowVal = MyData(I)
··· END IF
· NEXT
RETURN ' LowVal will hold the lowest of the 10 values.
' Note this is a 'classic' problem.· You· can speed
' it up if you know something about the arrangement of the values
' -- like they're already sorted.
'
' But if the arrangement of the numbers is random, or in any order,
' then a 'linear search' like the above is the fastest way to insure
' you've checked every number.
LOOKUP MyData,[noparse][[/noparse]varx,vary,varz,varp,vard,varq,var1,var2,var2,var3] 'variables
FindLowest:
LowVal = 0
FOR I = 1 TO 10
IF MyData(I) < LowVal THEN
LowVal = MyData(I)
END IF
NEXT
RETURN ' LowVal will hold the lowest of the 10 values.
debug lowval ' should give me th lowest variable.
After which, the lowest value would be in the variable LowVal.· Again, these are assumed names, you can substitute your own variable names.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
"Lookup" for this purpose.
So, your program would look like
MyData VAR BYTE(10) ' Allocate a 10-byte array
I VAR NIBBLE
LowVal VAR BYTE ' Result holder
Main:
· FOR I = 1 to 10
··· MyData(I) = 10 - I ' Pre-load MyData with something to test with...
· NEXT
· GOSUB FindLowest ' Find lowest value and put in LowVal
· DEBUG [noparse][[/noparse]"Lowest value was ", LowVal, CR]
· PAUSE(1000) ' Pause 1 second, so you can read the result
· GOTO MAIN ' Do it again.
FindLowest:
· LowVal = 255·· ' Init to the HIGHEST possible value
· FOR I = 1 TO 10
··· IF MyData(I) < LowVal THEN
····· LowVal = MyData(I)
··· END IF
· NEXT
RETURN ' LowVal will hold the lowest of the 10 values.
Postscript: Correction from further down the page -- yes, "LowVal" should
be initialized to the HIGHEST possible value (255, here) not the lowest.
My bad, thanks for the correction.
Post Edited (allanlane5) : 6/19/2006 1:59:49 PM GMT
One change: The first line of the routine should be LowVal = 255. Otherwise it will always return 0. That's assuming MyData is an array of bytes. If it's an array of words, use LowVal = 65535
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
links:
My band's website
Our album on the iTunes Music Store
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
It may seem counterintuitive to use the MAX operator to find the minimum, but that is the way it works. Read it like this, "set the new lowVal equal to the old value of lowVal, but not to exceed myData(i)". So, MAX acts as a ceiling, and the ceiling can get lower and lower.
If this is a situation where values enter the table one at a time in a process, and the minimum has to be found at each step, a faster approach may be to maintain a pointer to the current minimum value so that it only have to compare to that one. At least usually. It depends on how the values get into and out of the table.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
also thank you all for the help(chir,SSteve,allanlane5 )
I am embarresd to say that I can not fiqe out where to bput my variables in this statementallanlane5 you said that further down in the text it says ....... can you tell me what text you are looking at.. this is a little above my head and I think I need to do more reading.
also thank you all for the help
I am embarrassed to say that I can not figure out where to put my variables in this statement. I think it should be this
But I have a question would not this statement
FOR I = 1 to 10
MyData(I) = 10 - I ' Pre-load MyData with something to test with...
NEXT
Cause the program to input a variable from the array into the MyData(I)
But then the next statement would send the program back to the for in FOR I = 1 to 10 without getting to the gosub findlowest
Yes, the "Lookup" command doesn't work that way at all, so you can't use
"Lookup" for this purpose.
So, your program would look like
MyData VAR BYTE(10) ' Allocate a 10-byte array
MyData(0) = varx
MyData(1) = vary
MyData(2) = varz
MyData(3) = var1
MyData(4) = var2
MyData(5) = var3
MyData(6) = varp
MyData(7) = vard
MyData(8) = varq
MyData(9) = varp
I VAR NIBBLE
LowVal VAR BYTE ' Result holder
Main:
FOR I = 1 to 10
MyData(I) = 10 - I ' Pre-load MyData with something to test with...
NEXT
GOSUB FindLowest ' Find lowest value and put in LowVal
DEBUG [noparse][[/noparse]"Lowest value was ", LowVal, CR]
PAUSE(1000) ' Pause 1 second, so you can read the result
GOTO MAIN ' Do it again.
FindLowest:
LowVal = 255 ' Init to the HIGHEST possible value
FOR I = 1 TO 10
IF MyData(I) < LowVal THEN
LowVal = MyData(I)
END IF
NEXT
RETURN ' LowVal will hold the lowest of the 10 values.
Postscript: Correction from further down the page -- yes, "LowVal" should
be initialized to the HIGHEST possible value (255, here) not the lowest.
My bad, thanks for the correction.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
links:
My band's website
Our album on the iTunes Music Store
2. The FOR loop should go from zero to nine, not 1 to 10. My fault.
3. It would be faster not to use "varx", "vary", etc, but simply put the values into the array in the first place.
This would save memory too. Or, you could use:
varx VAR MyVal(0)
vary VAR MyVal(1)
Which would make each of your 'var' locations the same as one of the MyVal array locations -- creating an 'alias', as they say. Unfortunately, I don't have a BS2 here to test and debug this code, so there may still be some small gotcha's.
Update:· Oops, just checked, the "varx VAR MyVal(0)" syntax does not work.· Sorry about that.
Post Edited (allanlane5) : 6/19/2006 6:21:28 PM GMT
this code.
varx VAR MyVal(0)
vary VAR MyVal(1)
do I use this to set up the variable and if so when do i declare the variable varx as a nibble.
thanks again
You wanted 5 or more variables in a table, and how to find the least. You've got that.
And I already said the "varx VAR MyVal(0)" syntax doesn't work.
The above creates an array with 5 bytes, indexed 0-4. You could access the elements of the array like this:
If you need nibbles, or words, or bits, you can define arrays for those also:
Arrays are nice, because say you had 3 discrete variables like this...
...you wouldn't have a way to "loop" through them and, say, test each one to see if it's lowest, as in the examples above. Well you could, but you'd have to write lines for each variable, and if you changed the names you'd have to redo code. Also arrays are nice because if you are looking at and/or testing each element in an indexed loop, you can exit the loop when you've found what want, etc.
On a Stamp, the maximum size of any array is 13 words, or 26 bytes, or 48 nibbles, or 96 bits, since that's all the memory there is.
Does this help? Using the same example as above, to find a min. value, you loop through the array and look for the lowest value as compared to whatever the lowest "found" value already is. Start with 255 because nothing can be "higher":
Here's a real world example for my own Bot where I use a lot of two element arrays for doing "left to right" sensor and motor work:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
You can in fact loop through these as an array by addressing it as follows:
This is completely valid as long as these variables are all the same size and declared in order.· Basically what you're doing is creating an implied array (for lack of a better word).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
I have an app where I track servo positions for the PSC, and had done a hack to be able to pass the bytes through SEROUT:
But if I maintain order and size on variable declarations, I could do something like this:
That is so cool. One thing I'm not clear on -- is the size of elements 1..N of the implied array the same size as the first element? Like the example above will always return Words in index loops? And the below would always return Nibs because the var used as element 0 is that size?
A sort of related question, then -- you could loop through the nibs of an implied array too? Fr'instance this isn't valid syntax, nor is logically accurate:
But this is?
There is a discussion of this technique in the Pbasic Manual, but I never quite grasped the utility of it before.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
However, using implicit array,
It works with any size variable and yes the size of the chunk is determined by the first element.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
the concept makes sense to me but my lowval reading is stuck at 0
here is the code I am using
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
This code was commented out --
Which *would* put some vals into MyData. Otherwise your lowval check looks OK. My question is the same as Franklin's -- MyData is a big array, not sure what you are doing with the valright..left etc vars.
One other thing, though, remember that your variable for storing the "lowest" found needs to be the same size as what you are checking or you may get strange results. In your code LowVal is a Byte, but you are checking the MyData array -- which is Words. Additionally, your "initial" highest value would need to be $FFFF for Words, $FF for Bytes, $F for Nibs, etc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
I know the array is large I thought I was getting the error because the array might have been too small.
If the above code makes an array and tell the array that slot 0 is = to valright,
slot 1 is = to valleft..ect
then if I change the value of the variable then is it right that the value in the array will change.
at this point I thought the lowval run the first time though would return valtop or 54 the valtop equivalent
if I change the array set up and place value in at the start
I get a debug of 255 which i know it the highest value of a nibble. But I do not know how it got into lowval since the lowest value is 264
full code follows.
You declare a bunch of Word vars, valright, valleft, etc. Then you declare a totally separate array MyArray. It's not like object oriented programming where:
could be a reference to valleft. An implied array would be like this:
Remember you're actually not defining an array per se, you're just defining a bunch of words IN ORDER. The Stamp lets you be sloppy --- if you ask for MyArray(1), you just get the next Word in memory after MyArray (which is the 0th element).
You only need to do this if you want to be able to use the Word as an indexed element AND because it's handy to have a single variable name for the same element.
In your code, you could dispense with one set or the other, and bring all your inputs into the array:
What may have thrown you is if you had an implied array you have nice mneumonic handles for your array elements, but you could just consider using constants for the index values like this:
I edited your code and put in a few comments that might help. There are two version -- one uses regular array with named index values, the other uses the implied array. I didn't edit this on my Pbasic machine, so I didn't tokenize them -- sorry if there are any typos.
Is this helping at all ??
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST