What size data (Byte, Word)? Why do you want to throw out the high and the low? What if there are more than one high and/or low values? What if they are all the same?
But if you must, then put the 10 data points into an array, sort the array (low to high or high to low, doesn't matter), sum up the middle 8 values and divide by 8.
If you keep all the values, then sum them up as they come in, and after 10 data points divide the sum by 10.
Like Sapphire said, put the 10 values into an array and use a simple sort-in-place. Ignore the first (lowest) and last (highest) values. Add up the 8 remaining values and divide by 8 to get the average. The BS2 can have 26 bytes of variable values. An array of 10 takes 20 bytes of those. Once you're done with the first and last, you can re-use those bytes for other variables. A BubbleSort (http://en.wikipedia.org/wiki/Bubble_sort) will do ... it's slow, but that won't matter for a list of values this short.
You can also eliminate the lowest and highest values during a separate phase to free up those storage locations before you do the sort.
If you need to save space you can add them as they come in saving the data value if it is the lowest or highest so far then subtract out the lowest and highest and divide by 8
'9 bytes used instead of 20 byte array
lowpoint var word
highpoint var word
sumpoint var word
datapoint var word
i var byte
lowpoint = 65535
highpoint = 0
sumpoint = 0
' get_data routine returns value in datapoint
for i = 1 to 10
gosub get_data
if datapoint < lowpoint then
lowpoint = datapoint
endif
if datapoint > highpoint then
highpoint = datapoint
endif
sumpoint = sumpoint + datapoint
next
sumpoint = sumpoint - (highpoint + lowpoint)
sumpoint = sumpoint / 8
Comments
But if you must, then put the 10 data points into an array, sort the array (low to high or high to low, doesn't matter), sum up the middle 8 values and divide by 8.
If you keep all the values, then sum them up as they come in, and after 10 data points divide the sum by 10.
-Phil
You can also eliminate the lowest and highest values during a separate phase to free up those storage locations before you do the sort.
'9 bytes used instead of 20 byte array
lowpoint var word
highpoint var word
sumpoint var word
datapoint var word
i var byte
lowpoint = 65535
highpoint = 0
sumpoint = 0
' get_data routine returns value in datapoint
for i = 1 to 10
gosub get_data
if datapoint < lowpoint then
lowpoint = datapoint
endif
if datapoint > highpoint then
highpoint = datapoint
endif
sumpoint = sumpoint + datapoint
next
sumpoint = sumpoint - (highpoint + lowpoint)
sumpoint = sumpoint / 8
-Phil