Index array
edwgeo
Posts: 8
I have a array (vector[5]={5,12,11,16,23}).
I need the position the vector for example.
vector[3]=3, vector[1]=1
how get the index of the elements of vector.
I need the position the vector for example.
vector[3]=3, vector[1]=1
how get the index of the elements of vector.
0
Comments
Do you mean how do you populate the array or how do you recall the array. In your array...
nVector[3]=11
nVector[1]=5
If you can clarify what you need to do I can help a little.
I make no guarantees to the function as I just wrote it here, so you may need to fix a couple of things, but I think that is the idea you are trying to accomplish.
Jeff
Jeff
P.S.
I keep forgetting about that approach. Nice code Joe.
Joe,
The only question I have with your code is if the same value is in multiple positions then x will change as it runs through the for loop, right?
The code will return the index of the first value that matches in the array.
Joe I just looked through the code again. The While Loop will return the first position, the For loop will return the last position if multiple instances occur, because the while loop is dependent on the finding a match, the for loop will run through its sequence and will keep running until it ends the loop, which means if there are multiple values in there the last position gets returned and sets that variable.
Hi jazzwyld,
No, that?s not true. As soon as the IF is satisfied the function will exit and RETURN the position, which in this case will always be the first match it finds.
Here is some code that verifies it:
And the output when button 1 is pushed:
This:
Is the same as this:
Maybe that makes it clearer?
"'Index for value 4 = ',ITOA(fnGetIndex(nTest,2))"
Jeff
my vote is for --> a_riot42 code:
Don't listen to your core it is wrong. Listen to your head which is telling you the compiler has initialized your variable to 0 for you.
Jeff
GSLogic wrote: Yep to both!
It may not matter what the array size is. C doesn't keep track of array lengths, but Java does and I wonder if Netlinx does too. Since there is no string terminator in Netlinx I am going to guess that the length of the array is stored somewhere so calling length_array returns that stored number rather than recalculating every time. I could be wrong but that is my intuition. Still, any function call avoided is good especially if the number of times it is called depends on the input.
Paul
a_riot42 wrote: I was wondering how that worked too. Any one know how the function works? Is it just reading a stored value or what?
Yes it is significant. Using Big O notation, and assuming that length_array has to count the number of indices every time, length_array runs in O(n) time. In other words, it has to do something n times, n being the size of the array. Now if you write a function that returns the index of an array, worst case it has to look at every element, again running in O(n) time. Now you are calling an O(n) function from an O(n) function so that your function now takes O(n²) comparisons for every call. A call to your function with an array of 1000 elements will make 1,000,000 comparison in the worst case.
If the length of an array is stored somewhere the length_array just does one thing regardless of the size of the input and runs in O(1) time. So now your function will run in O(n) time since your function looks at every element in the array and calls length_array n times but length_array runs in O(1) time for a total of O(n). My function, calling length_array only once and assuming the length is stored somewhere runs in O(n) time. O(n) is linear time and isn't bad so I can live with that. But 1,000,000 comparisons on an array of 1000 elements is pretty bad and gets much worse the larger the number obviously since its quadratic growth. It may not seem like a lot but O(n²) functions are sufficiently bad in most software that they simply cannot be used except in certain scientific applications that have no other way to calculate a result and it is acceptable for the computer to take hours, days or weeks to calculate a result.
If you really need it to be faster and your array is sorted like a list of increasing channel numbers or cable channels, you can find the index in O(log n) time which is very fast using a binary search technique. So for the function I wrote an array of 100 elements requires 100 comparisons O(n), but if its sorted, then a O(log n) function will make approx 6 comparisons. This gets better the bigger the array as an array with 1,000,000 elements requires a 1,000,000 comparisons but with a binary search only requires 20 comparisons. Well worth the little bit of extra code.
Faster still, but likely requiring more memory if your data has gaps is to keep the value in the element number. Then no look up is required since the index of the element 124 will be 124. That only works if you are storing integers though not some other data.
Paul
The short answer is because the code needs to.
How do you know if x<=y is true or false after it drops out of the WHILE? It doesn?t matter that it was just checked since it was tested with an AND so either side or both sides could have been false and caused the fallout.
If the function doesn?t do the test and just returns the value of x then when a value is not found the function will return LENGTH_ARRAY() + 1 instead of 0.
Yes, I?m aware of that. The example addresses the original question posed at the top of the thread and has nothing to do with the placement of LENGTH_ARRAY(). The code is testing a handful of values and I chose brevity at the expense of a few micro seconds. You chose an extra variable and an extra line of code. Jeff went with the WHILE loop instead of a FOR. Bottom line is who cares? It doesn?t make a bit of difference in this case.
If a function doesn?t work then that?s one thing and it?s probably best to point the errors out in an effort to help. But if it does work, who are we to say it?s not the right way to do it. I like to keep and open mind and enjoy reading code that uses methods that I may have never thought of before. That?s what makes programming so much fun. There is no right answer. It doesn?t matter what the vote count is. You can look at it however you want to look at it.
Here?s the way I choose to look at it:
If the code is using something like LENGTH_ARRAY(nDigitsOnOneHand)or perhaps LENGTH_ARRAY(nHundredBottlesOfBeerOnTheWall) then I?ll go with LENGTH_ARRAY() as the condition for the FOR loop and skip the extra typing.
But if the code is using something like LENGTH_ARRAY(nHugeEgo)then yes of course I?ll go with the extra code and call the function once before the FOR loop. If the array is big enough, I may even consider throwing a ?Please Wait? popup on the touch screen.
You can?t count on Netlinx initializing variables to 0 automatically for you.
Consider the following: Netlinx will initialize nFinger to 0 but nFingers will hold garbage.
Getting back on topic? If you?re asking what gets RETURNed if you pass in a value of 2, the answer is 2 (the first match), not 5 (the last match). The example code and corresponding output posted demonstrates that.
You are right of course, since your while guard is a compound statement. It seems like an overly complicated way to do something simple though. Whenever I see duplicate code in a function it usually points to a design flaw. Sometimes its unavoidable but in this case it certainly isn't. I don't know why you would risk using a while loop when a safer for loop accomplishes the same thing.
I care, and it does make a difference as I explained above. Good code is efficient code, no way around that. Calling a function that returns the same value over and over again is not brevity, its just inefficient. How can you assume there is a 'handful' of values? Because the poster only typed a few in his question? Surely you didn't expect him to type out an array of a thousand elements to ask a question about large arrays.
Just because a function works doesn't mean its well written. It has to be maintainable, easy to understand, efficient and hopefully elegant. Not sure why you would sacrifice those qualities. Just because you don't know the right way to do something doesn't mean there isn't one.
To me this is just bad code and I would never write something like this. You are declaring local_var variables in a function but without explicitly using the keyword. Is this just to prove a point or do you actually write code like this?
From the Netlinx Docs:
STACK_VAR defines a non-static local variable. STACK_VAR defines local variables the same way as LOCAL_VAR, and like LOCAL_VAR, STACK_VAR can appear in any statement block. The difference is that the value stored in the variable is initialized to zero whenever the statement block is called, and the value is destroyed when the statement block is finished.
If you are going to use functions that run in O(n²) time, then you will need a "Please wait..." popup. Perhaps it should say "Please wait a long time for my inefficient code to complete ..."
That just makes no sense to me at all. Why would you have two different ways of writing a function based on an assumption of the array size? To save some typing?
Paul
If you want to end Joe's version of the code after the first successful IF, simply add a BREAK Statement as shown below.
This code will run until the first nValue is found. It will then immediately save the idx in X and jump to the line after the FOR loop.
First off, I didn?t write the WHILE loop, I was simply answering your question. Second, there is no risk in the WHILE loop function that was posted. If you feel safer with FOR loops then use FOR loops.
A trash talking programmer, how amusing. You?re right though, I?m totally inept. If you can give me your contact info I?ll consult with you first before I write a line of code and make sure it gets your approval.
You?re insults (although not very creative) crack me up. No, those aren?t LOCAL_VARs. You?ve got it backwards. STACK_VAR is assumed. If you want a LOCAL_VAR (a variable that retains its value) you have to use the LOCAL_VAR keyword.
If your statement is based on the fact that the help file states ?If neither the LOCAL_VAR nor the STACK_VAR keyword is specified, LOCAL_VAR is assumed.? all I can say is the help file is wrong and here is code that proves it.
And the output when button 1 is pushed:
I couldn?t find that exact quote but I assume it?s there someplace. Anyway here is code that proves that statement incorrect also.
And here is the output when button 1 is pushed:
Peace Brother Paul.
I'm not sure which code you're looking at but in Joe's original response to the posters question he had a RETURN x after the "if" to exit upon the first match. The thread started drifting after that.
So STACK_VARS will always initialize to 0 and LOCAL_VARS can hold what ever junk is left over from the previous user of that memory location similar to the interger array initialization that was discussed in the REBUILD_EVENT thread? Of course that's just an issue the first time the code runs after compiling, correct?
I wouldn't expect arrays or structures to be initialized as not many languages do that automatically, but stack_var and local_var integers get initialized to 0 as seen above. If an unspecified variables default is a stack_var rather than a local_var like the docs say, that makes more sense, but then there is a mistake in the docs or a compiler bug. But that just provides even more reason to not write ambiguous code like that.
The compiler will allow:
stack_var stackNum
or
integer stackNum
but to my thinking this creates ambiguity and looks like a mistake. I never use local_vars so I haven't come across the compiler bug/help file mistake and always explicitly determine the scope and type of my variables to avoid these types of things.
Paul
Strangely enough, structure arrays, and integer arrays inside a structure array do indeed get initialized to 0 in a function as shown below. However, as shown above integer arrays alone don?t get initialized to 0 in functions. Go figure. (AFAIK all Globals do get initialized without issue.)
Output when button 1 is pushed: