How to find which value is closest to a number in C?

I have the following code in C:

#define CONST 1200
int a = 900;
int b = 1050;
int c = 1400;

if (A_CLOSEST_TO_CONST) {
  // do something
}

What is a convenient way to check if a is the closest CONST value among a, b, and c?

Edit:

It doesn't matter if I have 3 variables or such an array (it can be more than 3 elements):

int values[3] = {900, 1050, 1400};
+5
source share
8 answers

This works for three variables:

if (abs(a - CONST) <= abs(b - CONST) && abs(a - CONST) <= abs(c - CONST)) {
    // a is the closest
}

This works with an array of one or more elements, where n is the number of elements:

int is_first_closest(int values[], int n) {
    int dist = abs(values[0] - CONST);
    for (int i = 1; i < n; ++i) {
        if (abs(values[i] - CONST) < dist) {
            return 0;
        }
    }
    return 1;
}

See how it works on the Internet: ideone

+4
source

(a-CONST), (b-CONST) (c-CONST). , , .

+3

. min_element() int, . true, . , a < b, . pinouchon() .

#include <stdio.h>
#include <stdlib.h>

#define CONST 1200

int pinouchon(int a, int b)
{
    return abs(a - CONST) < abs(b - CONST);
}

int min_element(const int *arr, int size, int(*pred)(int, int))
{
    int i, found = arr[0];
    for (i = 1; i < size; ++i)
    {
        if (pred(arr[i], found)) found = arr[i];
    }
    return found;
}

int main()
{
    int values[3] = {900, 1050, 1400};
    printf("%d\n", min_element(values, 3, pinouchon));
    return 0;
}
+2

- .....

int is_first_closest(int values[]) {
    int dist = abs(values[0] - CONST),closest;     //calculaing first difference
    int size = sizeof( values )                    //calculating the size of array
    for (int i = 1; i < size; ++i) {
        if (abs(values[i] - CONST) < dist) {       //checking for closest value
             dist=abs(values[i] - CONST);          //saving closest value in dist
             closest=i;                            //saving the position of the closest value
        }
    }
    return values[i];
}

, CONST.

+1

. ( 3- , , - ). , , , - , const)

0

. ( , , , , .)

// I think you need to include math.h for abs() or just implement it yourself.
// The code doesn't deal with duplicates.
// Haven't tried it so there might be a bug lurking somewhere in it.

const int ArraySize = <your array size>;
const int YourConstant = <your constant>;
int values[ArraySize] = { ... <your numbers> ... };
int tempMinimum = abs(YourArray[0] - YourConstant); // The simplest way
    for (int i = 1; i < ArraySize; i++) { // Begin with iteration i = 1 since you have your 0th difference computed already.
        if (abs(YourArray[i] - YourConstant) < tempMinumum) {
            tempMinumum = abs(YourArray[i] - YourConstant);
        }
    }

// Crude linear approach, not the most efficient.
0

For a large sorted set, you should be able to use binary search to find two numbers that (modulo the edge) limit the number, one of which should be the closest.

This way you can achieve O (Log n) performance instead of O (n).

0
source

pseudo code:

closest_value := NULL
closest_distance := MAX_NUMBER
for(value_in_list)              
    distance := abs(value_in_list - CONST)
    if (distance < closest_distance)
        closest_value := value_in_list
        closest_distance := distance
print closest_value, closest_distance        
0
source

All Articles