PDA

View Full Version : highest or lowest variable in an array; how do you find it?



DDDvvv
- 12th July 2012, 19:52
Hi everyone.
im trying to determine the highest and lowest variable in a 50 byte array. whats the easiest way to do it?

ScaleRobotics
- 12th July 2012, 20:32
Here's one way:



i var byte
lowest var byte
highest var byte
myarray var byte[50]

'then do something to fill array with data

lowest = 255
highest = 0
for i = 0 to 49
if myarray[i] < lowest then lowest = myarray[i]
if myarray[i] > highest then highest = myarray[i]
next i

Normnet
- 12th July 2012, 20:46
Hi everyone.
im trying to determine the highest and lowest variable in a 50 byte array. whats the easiest way to do it?

yArray VAR BYTE[50]
yLow VAR BYTE
yLow = 255
yHigh VAR BYTE
yHigh = 0
i VAR BYTE

FOR i = 0 TO 49
IF yArray[i] < yLow THEN
yLow = yArray[i]
endif
IF yArray[i] > yHigh THEN
yHigh = yArray[i]
ENDIF
NEXT


Not being duplicative just that ScaleRobotics Posted First.

Norm

Megahertz
- 13th July 2012, 09:37
I'll take a shot at it as well, but the gurus have already replied, this is just a little different.



i var byte
lowest var byte
highest var byte
myarray var byte[50]

for i = 0 to 48
Highest= MyArray[i] MAX MyArray[i+1]
Lowest= MyArray[i] MIN MyArray[i+1]
next i


ScaleRobotics, I hope you had good internet free holidays :)

Dave
- 13th July 2012, 11:58
I don't think Megahertz code will work as it is resetting the maximum and minimum value each pass thru the loop. NO testing is being done against the variables "Highest" and "Lowest"....

Megahertz
- 13th July 2012, 16:53
OOps, my mistake. Instead use the following:



i var byte
lowest var byte
highest var byte
CheckHigh Var Byte
CheckLow Var Byte
myarray var byte[50]

CheckHigh=0 : CheckLow=255
for i = 0 to 48
CheckHigh= MyArray[i] MAX MyArray[i+1]
If CheckHigh>Highest then Highest=CheckHigh
Lowest= MyArray[i] MIN MyArray[i+1]
If CheckLow<Lowest then Lowest=CheckLow
next i


Thanks Dave for correcting me :)

DDDvvv
- 18th July 2012, 02:42
thanks guys .
i was thinking of doing it like post #2, and i thought there was an easier way to skin it. this really helps alot with all this options.:smile: