Fill another array with abs(x-40), where x is the corresponding value in the first array. Then find the index of the smallest element in the second array and return the element in the first array, at that index. Example:
first = {50, 30, 20, 10, 70};
secon = {10, 10, 20, 30, 30};
smallest index = 0
closest value = first[smallest index] = 50
I would also like to add that the numbers are always changing and are usually random, it changes every 3secs and between those 3secs i need to get the closest number from all the numbers. Then do something when the number is found or send the output to a label in my form.