Go Down

Topic: Compare value to array (Read 1 time)previous topic - next topic

ThePoulsen

Oct 11, 2011, 12:25 pm
Hey all

Yet another noob question from me

I have the following array

Code: [Select]
`int row [12] = {20, 30, 37, 50, 67, 83, 100, 117, 133, 167, 200, 233};`

and a value

Code: [Select]
`int val`

Now I would like to compare the value to the array and find the best match for this, and read this out into a result integer.

Code: [Select]
`int result`

The readout should be the array position, and not the array value..

To clarify. if my "val" is 90, the one closest to this is 83 and this is in "row [5]". Therefore the value of "result" should be 5

sui

#1
Oct 11, 2011, 12:41 pm
(not tested...)

Code: [Select]
`int diff = 32767;           // save the lowest difference in this variable. init with maximum int because it's the worst possible match for this data type. don't know if there is a constant for maxint?int result = 0;             // assuming the array contains only one element, the best match would allways be index #0for (int i = 0; i<sizeof(row); i++) {             // loop trough array  if ( abs( row[i] - val ) < diff )  {            // if the difference between value at index #i and val is lower than the previously found best match...    result = i;                                   // save the better results index    diff = abs( row[i] - val );                   // save the as yet best difference value  }}`

Grumpy_Mike

#2
Oct 11, 2011, 12:42 pm
So you have a loop that steps through all the array.
At each one you subtract the array element from the value and use the abs() function to remove any negative sign.
You have a minimumFoundSoFar variable initially set to a very big value. If the result of this subtraction is less than the minimumFoundSoFar variable you transfer the loop index into the result variable.
At the end of the loop the result variable has the index of the closest match.

Grumpy_Mike

#3
Oct 11, 2011, 12:43 pm
Give a man a fish and he is fed for a day, teach him how to fish and he is fed for life.

PaulS

#4
Oct 11, 2011, 01:19 pm
Quote
Give a man a fish and he is fed for a day, teach him how to fish and he is fed for life.

Or until the fish are all gone.

AWOL

#5
Oct 11, 2011, 05:00 pm
Teach a man to fish and he will sit by a lake drinking beer all day
"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.
I speak for myself, not Arduino.

#6
Oct 11, 2011, 05:34 pm

Teach a man to fish and he will sit by a lake drinking beer all day

According to the proverb, only if somebody has given him or taught him how to make the beer.

Khalid

#7
Oct 11, 2011, 07:56 pm
Thanks Sui..at-least i learn something today... One Fish in Hand is better than two fishes in Pond ]
Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...

Go Up