Go Down

### Topic: Get array value index by searching value (Read 21817 times)previous topic - next topic

#### lyron

##### Sep 01, 2012, 07:18 pm
In a project i'm making, i need to use arrays. Now i have a question:
Is there a way to find a value's index just by searching for the value?

For example:

int arrayVals[] = {2, 4, -8, 3, 5, 1};

Now for example when i input "-8" into a function, i need it to return the index of -8, which is 2. How would i go about doing that?

Oh and how can i get the length of an array?

#### WizenedEE

#1
##### Sep 01, 2012, 07:26 pm
To do inverse lookup you need to traverse the array
Code: [Select]
`const int arysz = 6;int ary[arysz] = {4, 32, 6, 2, -1, 5}int wantedval = -1;int wantedpos;for (int i=0; i<arysz; i++) {   if (wantedval = ary[i]) {     wantedpos = i;     break;   }}`
If they're in order, you can also do a binary search (google it)

For finding array length, the best method is to use the above method and store a separate variable for the length. However, this works, too
Code: [Select]
`int ary[12];int len = sizeof(ary)/sizeof(ary[0])`

#### lyron

#2
##### Sep 01, 2012, 07:30 pm

To do inverse lookup you need to traverse the array
-code-

Thank you. Now what happens when i search for a value that isn't in the array?

#### Grumpy_Mike

#3
##### Sep 01, 2012, 07:38 pm
Then wantedval = -1 and you know you didn't find it.
Note make the initial value of wantedval outside the range of numbers you are expecting in the array.

#### lyron

#4
##### Sep 01, 2012, 07:42 pm
Will this code work if i expand the array during execution of the code?

#### AWOL

#5
##### Sep 01, 2012, 07:42 pm
If your array is ordered, you can shorten search times by doing a binary search.

#### lyron

#6
##### Sep 01, 2012, 07:44 pm

If your array is ordered, you can shorten search times by doing a binary search.

It's not ordered.

#### Grumpy_Mike

#7
##### Sep 01, 2012, 07:49 pm
Quote
Will this code work if i expand the array during execution of the code?

You can't do that, the array size is fixed by:-
Code: [Select]
`int ary[arysz] = {4, 32, 6, 2, -1, 5}`
You can't expand this once it has been set.

#### tigger

#8
##### Feb 22, 2018, 10:16 pm
Useful bit code from Wizend, just what I was looking for.
Works better with
Code: [Select]
`if (wantedval == ary[i]) {`
, not
Code: [Select]
`if (wantedval = ary[i]) {`
Had me going for a bit.

#### robtillaart

#9
##### Feb 23, 2018, 10:41 am
You must also have a flag that indicates the value is actually found in the array.

A common way is to initialize the position with -1, as this is (In C++) not a valid array index.

int wantedpos = -1;
... search...

if (wantedPos != -1) // it really is found in the array.

Another problem can be that value occur more than once in an array.
In that case you need to define if you want to find the 1st 2nd .... last instance.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### Taufiq_Umar

#10
##### Aug 15, 2018, 01:24 am
Thankyou WizenedEE , I have used your script above and run very well...I just modified symbol of "=" be "=="
I've spent a lot of time to find this script that I use to get index numbering..

const int arysz = 6;
int ary[arysz] = {4, 32, 6, 2, -1, 5}
int wantedval = -1;
int wantedpos;
for (int i=0; i<arysz; i++) {
if (wantedval == ary) {    //modified here
wantedpos = i;
break;
}
}

Nice job sir...

#### DKWatson

#11
##### Aug 15, 2018, 03:22 am
if (wantedval == ary)

missing the index with ary
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

#### vaj4088

#12
##### Aug 15, 2018, 05:14 am
Not exactly.  The post was made without code tags and turned into italics at that point.  The array index was interpreted as a command to use italics, instead of being displayed.  This is one of the reasons why code tags MUST be used!

#### DKWatson

#13
##### Aug 15, 2018, 05:18 am
Any way you can get them all to read that?
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Go Up