Pages: [1]   Go Down
Author Topic: Get array value index by searching value  (Read 2698 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To do inverse lookup you need to traverse the array
Code:
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:
int ary[12];
int len = sizeof(ary)/sizeof(ary[0])
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33301
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Will this code work if i expand the array during execution of the code?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 287
Posts: 25682
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

It's not ordered.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33301
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int ary[arysz] = {4, 32, 6, 2, -1, 5}
You can't expand this once it has been set.
Logged

Pages: [1]   Go Up
Jump to: