Beijer Electronics (formerly QSI Corporation)

Manufacturer of Mobile Data and Human Machine Interface Terminals.
It is currently Wed Nov 22, 2017 5:35 am

All times are UTC - 7 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: questions about arrays
PostPosted: Thu Nov 10, 2011 12:43 am 
Offline

Joined: Thu Aug 09, 2007 4:44 pm
Posts: 67
I have a few questions about arrays:

Is there a way to delete an element from an array and have the values that proceed it shift over 1 space to take its place? For example: I have an array of integers and I know that the number 17 is in there somewhere, and I want it gone and the array resized down by 1. Can I do something like:

Code:
dim location as integer
location = find(array,0,-1,17)
delete_array_position (location) from (array) ****MAKING THIS UP****
redim(array,len(array)-1)


How can I go about re-organizing an array of objects by their xpos value, so that the object with the lowest xpos value appears first in the array? Lets say I have an array of 20 buttons arranged in ascending order like:

dim buttons[] as buttonV2
init buttons := [empty, button_1, button_2, ..., button_20]

I want to evaluate the xpos values of all the buttons and re-create the array where the first element of the array is the button with the lowest xpos value, the second element is the button with the second lowest xpos, etc...
I have a vague notion on how to do this but it seems really long an inefficient, I'd appreciate some guidance.

Thanks much


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 2:56 pm 
Offline

Joined: Thu Aug 09, 2007 4:44 pm
Posts: 67
I'm kinda hung up on both of these questions... so info on either would be so greatly appreciated. Thanks :)

Aaron


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 3:04 pm 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Code:
func RemoveElement(arr[] as reference to integer, value as integer)
    dim tmp[1] as integer
    dim pos as integer
    tmp[0] := value

    pos = find(arr, 0, -1, tmp)
    if pos >= 0 then
        arr = left(arr, pos) + mid(arr, pos+1, -1)
    endif
endfunc

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 3:15 pm 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Sorting an array is a bit trickier. There is no built-in sort function in Qlarity. On the rare occasion I have needed it, I simply implemented a simple selection sort. (Not particularly efficient, but gets the job done).

Are your buttons moving at runtime? (that would be somewhat unusual). If not, just put the buttons in the array in the right order.

Anyway here is code that will sort them by yPos. You can modify it to sort by any property you want though.
Code:
func SortBtns(btns[] as reference to objref buttonV2)
    dim i, j as integer
    dim max as integer
    dim lowIdx, lowYPos as integer
    dim tmpBtn as objref ButtonV2

    max = len(btns) -1

    for i = 0 to max
        lowYPos = 0x7FFFFFFF
        lowIdx = -1
        for j = i to max
            if btns[j].yPos < lowYPos then
                lowYPos = btns[j].yPos
                lowIdx = j
            endif
        next
        if lowIdx <> i then
            tmpBtn = btns[i]
            btns[i] = btns[lowIdx]
            btns[lowIdx] = tmpBtn
        endif
    next
endfunc

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 3:29 pm 
Offline

Joined: Thu Aug 09, 2007 4:44 pm
Posts: 67
Jeremy,

Yes, my buttons are moving at runtime. The buttons are representing keyframes, whose position on the screen represent the time at which motion is commanded on a Galil motion controller. Button xpos is changed +/- depending on the start time, and the button width represents the duration of the motion.

Thanks for that code... I'm looking through it now.

Aaron


Top
 Profile  
 
PostPosted: Fri Nov 11, 2011 1:19 am 
Offline

Joined: Thu Aug 09, 2007 4:44 pm
Posts: 67
Jeremy,

I've been studying the code you provided... and I grasp what each section does, but I don't understand the formatting well enough to rework it to do a few other functions I need. I've been sticking to do/while loops so far and haven't used for/next loops before... but I'm starting to get them.

I want to do something similar with two arrays of integers, so let me know if I am reworking your code correctly... I'm not sure if I'm setting up the function correctly because it's 2 arrays now instead of just one + the Ypos values associated.

arrayA[] contains 20 random numbers
Numbers[] has the numbers 1 - 20, which correspond to positions 1-20 in array1.
I want to arrange the elements in Nubers[] in an order based on the lowest to highest values found in arrayA[].

Code:
func SortIntegers(numbers[] as reference to integers,arrayA[] as reference to integers)
    dim i, j as integer
    dim max as integer
    dim lowIdx, lowInteger as integer
    dim tmpinteger as integer

    max = len(numbers) -1

    for i = 0 to max
        lowInteger = 0x7FFFFFFF
        lowIdx = -1
        for j = i to max
            if arrayA[j]< lowInteger then
                lowInteger = arrayA[j]
                lowIdx = j
            endif
        next
        if lowIdx <> i then
            tmpinteger = numbers[i]
            numbers[i] = numbers[lowIdx]
            numbers[lowIdx] = tmpinteger
        endif
    next
 


Last edited by aaronkeit on Fri Nov 11, 2011 11:00 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Fri Nov 11, 2011 7:39 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
At a glance the code looks OK. Have you tried it out? What went wrong? If you are ever in doubt, you can run the code in simulation view and place a breakpoint on the function in question to debug it.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Fri Nov 11, 2011 12:44 pm 
Offline

Joined: Thu Aug 09, 2007 4:44 pm
Posts: 67
Jeremy,

I just tested my code out and it didn't quite work. I used the following code and instead of getting the expected result of [0,5,4,3,2,1] I got [0,5,1,2,3,4]. I'm not sure whats wrong.

Code:
    dim order[6] as integer
    dim neworder[6] as integer
    init order := [0,1,2,3,4,5]
    init neworder := [0,5,4,3,2,1]
    sortintegers(order,neworder)


func SortIntegers(numbers[] as reference to integer,random[] as reference to integer)
    dim i, j as integer
    dim max as integer
    dim lowIdx, lowInteger as integer
    dim tmpinteger as integer

    max = len(numbers) -1
    'max = 20

    for i = 0 to max
        lowInteger = 0x7FFFFFFF
        lowIdx = -1
        for j = i to max
            if random[j]< lowInteger then
                lowInteger = random[j]
                lowIdx = j
            endif
        next
        if lowIdx <> i then
            tmpinteger = numbers[i]
            numbers[i] = numbers[lowIdx]
            numbers[lowIdx] = tmpinteger
        endif
    next
endfunc


Top
 Profile  
 
PostPosted: Fri Nov 11, 2011 1:28 pm 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Your problem is that you were not actually treating the index array as an array of indices. You need to do a second level of indirection during the value compare if you are going to sort like that. Here is the revised code. I had to rename some of the variables to keep my sanity in looking at the sort algorithm (I kept getting confused as to which array was being sorted and which contained the numbers)

Code:
func TestSort()
    dim indexed[6] as integer
    dim indexedOld[] as integer
    dim randomNums[6] as integer
    init indexed := [0,1,2,3,4,5]
    init randomNums := [0,5,4,3,2,1]
    indexedOld := indexed
    sortintegers(indexed,randomNums)
endfunc

func SortIntegers(indexes[] as reference to integer,random[] as reference to integer)
    dim i, j as integer
    dim max as integer
    dim lowIdx, lowInteger as integer
    dim tmpinteger as integer

    if len(indexes) <> len(random) then
        throw ("SortIntegers", "Arrays must be the same length")
    endif

    max = len(indexes) -1
    'max = 20

    for i = 0 to max
        lowInteger = 0x7FFFFFFF
        lowIdx = -1
        for j = i to max
            if indexes[random[j]]< lowInteger then
                lowInteger = indexes[random[j]]
                lowIdx = j
            endif
        next
        if lowIdx <> i then
            tmpinteger = indexes[i]
            indexes[i] = indexes[lowIdx]
            indexes[lowIdx] = tmpinteger
        endif
    next
endfunc

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Fri Nov 11, 2011 2:06 pm 
Offline

Joined: Thu Aug 09, 2007 4:44 pm
Posts: 67
Jeremy,

It's all a bit confusing to me... I'm not sure if I was clear enough with what I wanted when I labeled my second array "neworder". That array was not meant to specify the actual new order of the first array. For whatever reason I used the values 5 through 1 in the array neworder[], but these values could have been any numbers in descending order, or in a random order.

When I use your new code with values other than 0-5 in the array RandomNums[] I get an "ilegal index in to array" message at this line:

if indexes[random[j]]< lowInteger then

For example, I want to use:

init randomNums := [0,9,8,7,6,5]

in your code, and I expect the order to come out [0,5,4,3,2,1]

Am I using the code wrong or was I just unclear on what I was trying to do?


Top
 Profile  
 
PostPosted: Fri Nov 11, 2011 2:09 pm 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Yeah -- as I said this code is making my brain hurt. I had the comparison backwards. This is the right comparison

Code:
            if random[indexes[j]]< lowInteger then
                lowInteger = random[indexes[j]]
                lowIdx = j
            endif


Now, the entries in the indexes array MUST be in the range of 0 to len(random)-1 as they represent indexes into that array.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Fri Nov 11, 2011 2:37 pm 
Offline

Joined: Thu Aug 09, 2007 4:44 pm
Posts: 67
Great! That did the trick. Thanks so much. My brain feels bruised now too...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 7 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group