Shop OBEX P1 Docs P2 Docs Learn Events
sorting numbers in an array — Parallax Forums

sorting numbers in an array

ArchiverArchiver Posts: 46,084
edited 2002-12-22 03:55 in General Discussion
Hi all: I want to sort an array of numbers and find the largest number in
the array. I then want to know which cell of the array contained such a number.
I found some code to do this in Qbasic, but the code uses conventional
if-then statments which cannot be done with the Stamp. I tried converting it
to the Stamps IF statments but made a hash of things.
My ultimate goal is to use it on a robot with a Devantech sensor. I want to
take 5 readings in an arc facing forward, and find the largest value. I then
can turn the robot to face that largest opening.
for example If I got the values 12, 45, 16, 8, 97 I want to return 97 and
the array position. I than will know that the 97 is in the farthest right
position (or value5 of array) and I can turn the robot that direction.
It is the darn sorting routine that is giving me fits. I am a poor
programmer at best.
Could anybody please send me a sorting routine to do this?
Thankyou all very much.
Happy Holidays: Visit my Christmas webpage at
http://mntnweb.com/xmas.htm
Very respectfully, your obedient servant.
Kerry
Admin@M...
WWW server hosting [url=Http://mntnweb.com]Http://mntnweb.com[/url]
Kerry Barlow
p.o. box 21
kirkwood ny
13795
607-775-1575

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-12-22 00:49
    If you only need to know the largest value and position, you can do it
    without sorting the array. Like this:

    maxDist = 0
    maxPos = 6

    Find_Max:
    FOR idx = 0 TO 4
    IF (sonar(idx) <= maxDist) THEN Skip_Val
    maxDist = sonar(idx)
    maxPos = idx

    Skip_Val:
    NEXT


    BTW ... it's been a long time coming, but PBASIC 2.5 is scheduled to be
    released next month. Yes, it actually exists [noparse][[/noparse]in final stages of development
    and testing]. The search routine from above looks like this in PBASIC 2.5:

    Find_Max:
    FOR idx = 0 TO 4
    IF (sonar(idx) > maxDist) THEN
    maxDist = sonar(idx)
    maxPos = idx
    ELSE
    ' no else required here!
    ENDIF
    NEXT

    PBASIC 2.5 also includes DO-LOOP and SELECT-CASE. The best part is that it
    is compatible with the BS2 modules you have today -- no upgrades are
    required!

    See my article in the January issue of Nuts & Volts magazine for details.

    -- Jon Williams
    -- Parallax


    In a message dated 12/21/2002 6:15:53 PM Central Standard Time,
    admin@m... writes:

    > Hi all: I want to sort an array of numbers and find the largest number in
    > the array. I then want to know which cell of the array contained such a
    > number.
    > I found some code to do this in Qbasic, but the code uses conventional
    > if-then statments which cannot be done with the Stamp. I tried converting
    > it
    > to the Stamps IF statments but made a hash of things.
    > My ultimate goal is to use it on a robot with a Devantech sensor. I want to
    > take 5 readings in an arc facing forward, and find the largest value. I
    > then
    > can turn the robot to face that largest opening.
    > for example If I got the values 12, 45, 16, 8, 97 I want to return 97 and
    > the array position. I than will know that the 97 is in the farthest right
    > position (or value5 of array) and I can turn the robot that direction.
    > It is the darn sorting routine that is giving me fits. I am a poor
    > programmer at best.
    > Could anybody please send me a sorting routine to do this?
    > Thankyou all very much.
    > Happy Holidays: Visit my Christmas webpage at
    > http://mntnweb.com/xmas.htm
    > Very respectfully, your obedient servant.
    > Kerry
    > Admin@M...
    > WWW server hosting [url=Http://mntnweb.com]Http://mntnweb.com[/url]
    > Kerry Barlow
    > p.o. box 21
    > kirkwood ny
    > 13795
    > 607-775-1575



    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-12-22 01:06
    Hi Kerry,

    I have routines that might help you out posted at:
    http://www.emesys.com/BS2math5.htm
    and choose the link to "median, maximum, minimum, mode and frequency".

    -- best regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...





    >Hi all: I want to sort an array of numbers and find the largest number in
    >the array. I then want to know which cell of the array contained
    >such a number.
    >I found some code to do this in Qbasic, but the code uses conventional
    >if-then statments which cannot be done with the Stamp. I tried converting it
    >to the Stamps IF statments but made a hash of things.
    >My ultimate goal is to use it on a robot with a Devantech sensor. I want to
    >take 5 readings in an arc facing forward, and find the largest value. I then
    >can turn the robot to face that largest opening.
    > for example If I got the values 12, 45, 16, 8, 97 I want to return 97 and
    >the array position. I than will know that the 97 is in the farthest right
    >position (or value5 of array) and I can turn the robot that direction.
    > It is the darn sorting routine that is giving me fits. I am a poor
    >programmer at best.
    >Could anybody please send me a sorting routine to do this?
    >Thankyou all very much.
    >Happy Holidays: Visit my Christmas webpage at
    >http://mntnweb.com/xmas.htm
    >Very respectfully, your obedient servant.
    >Kerry
    >Admin@M...
    >WWW server hosting [url=Http://mntnweb.com]Http://mntnweb.com[/url]
    >Kerry Barlow
    >p.o. box 21
    >kirkwood ny
    >13795
    >607-775-1575
  • ArchiverArchiver Posts: 46,084
    edited 2002-12-22 03:21
    jonwms@a... wrote:


    > BTW ... it's been a long time coming, but PBASIC 2.5 is scheduled to be
    > released next month. Yes, it actually exists [noparse][[/noparse]in final stages of development
    > and testing]. The search routine from above looks like this in PBASIC 2.5:

    > PBASIC 2.5 also includes DO-LOOP and SELECT-CASE. The best part is that it
    > is compatible with the BS2 modules you have today -- no upgrades are
    > required!


    Woooohoooo!

    > See my article in the January issue of Nuts & Volts magazine for details.

    I sure will - love your articles in N&V.

    Michael Burr
  • ArchiverArchiver Posts: 46,084
    edited 2002-12-22 03:55
    WOO WOO: Thankyou to Jon and Tracy. I was so close with my own code. I had a
    branch routine in the wrong place and the < symbol pointing the wrong way,
    but my old tired brain just could not leap the final hurdle on its own.
    Thankyou very much for the speedy responses.
    I too look forward to Pbasic 2.5
    -Kerry


    At 07:49 PM 12/21/02 EST, you wrote:
    >If you only need to know the largest value and position, you can do it
    >without sorting the array. Like this:
    >
    > maxDist = 0
    > maxPos = 6
    >
    >Find_Max:
    > FOR idx = 0 TO 4
    > IF (sonar(idx) <= maxDist) THEN Skip_Val
    > maxDist = sonar(idx)
    > maxPos = idx
    >
    >Skip_Val:
    > NEXT
    >
    >
    >BTW ... it's been a long time coming, but PBASIC 2.5 is scheduled to be
    >released next month. Yes, it actually exists [noparse][[/noparse]in final stages of development
    >and testing]. The search routine from above looks like this in PBASIC 2.5:
    >
    >Find_Max:
    > FOR idx = 0 TO 4
    > IF (sonar(idx) > maxDist) THEN
    > maxDist = sonar(idx)
    > maxPos = idx
    > ELSE
    > ' no else required here!
    > ENDIF
    > NEXT
    >
    >PBASIC 2.5 also includes DO-LOOP and SELECT-CASE. The best part is that it
    >is compatible with the BS2 modules you have today -- no upgrades are
    >required!
    >
    >See my article in the January issue of Nuts & Volts magazine for details.
    >
    >-- Jon Williams
    >-- Parallax
    >
    >
    >In a message dated 12/21/2002 6:15:53 PM Central Standard Time,
    >admin@m... writes:
    >
    >> Hi all: I want to sort an array of numbers and find the largest number in
    >> the array. I then want to know which cell of the array contained such a
    >> number.
    >> I found some code to do this in Qbasic, but the code uses conventional
    >> if-then statments which cannot be done with the Stamp. I tried converting
    >> it
    >> to the Stamps IF statments but made a hash of things.
    >> My ultimate goal is to use it on a robot with a Devantech sensor. I want to
    >> take 5 readings in an arc facing forward, and find the largest value. I
    >> then
    >> can turn the robot to face that largest opening.
    >> for example If I got the values 12, 45, 16, 8, 97 I want to return 97 and
    >> the array position. I than will know that the 97 is in the farthest right
    >> position (or value5 of array) and I can turn the robot that direction.
    >> It is the darn sorting routine that is giving me fits. I am a poor
    >> programmer at best.
    >> Could anybody please send me a sorting routine to do this?
    >> Thankyou all very much.
    >> Happy Holidays: Visit my Christmas webpage at
    >> http://mntnweb.com/xmas.htm
    >> Very respectfully, your obedient servant.
    >> Kerry
    >> Admin@M...
    >> WWW server hosting [url=Http://mntnweb.com]Http://mntnweb.com[/url]
    >> Kerry Barlow
    >> p.o. box 21
    >> kirkwood ny
    >> 13795
    >> 607-775-1575
    >
    >
    >
    >[noparse][[/noparse]Non-text portions of this message have been removed]
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >
    Happy Holidays: Visit my Christmas webpage at
    http://mntnweb.com/xmas.htm
    Very respectfully, your obedient servant.
    Kerry
    Admin@M...
    WWW server hosting [url=Http://mntnweb.com]Http://mntnweb.com[/url]
    Kerry Barlow
    p.o. box 21
    kirkwood ny
    13795
    607-775-1575
Sign In or Register to comment.