1

Can anybody tell me what's going on with the following code:

int compareX(const void* a, const void* b)  
 {  
    Point *p1 = (Point *)a, *p2 = (Point *)b;  
    return (p1->x - p2->x);  
 }

I have taken this code from geeks for geeks, the closest pair of points. can anyone explain to me the similar code in a much simpler and easy way with line by line explanation

also, I am unable to understand this piece of code as well:

float bruteForce(Point P[], int n)  
{  
    float min = FLT_MAX;
    for (int i = 0; i < n; ++i)  
        for (int j = i+1; j < n; ++j)  
            if (dist(P[i], P[j]) < min)  
                min = dist(P[i], P[j]);  
    return min;  
}  

What's the use of FLT_MAX here?

puio
  • 1,208
  • 6
  • 19
Jayant Nigam
  • 171
  • 1
  • 9
  • Refer to this for more details about FLT_MAX https://stackoverflow.com/a/7973772/14165412 – Taha Habib Aug 26 '20 at 16:13
  • 1
    Please only ask 1 question per posted question. See [One post with multiple questions or multiple posts?](https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts). – François Andrieux Aug 26 '20 at 16:18
  • _geeks for geeks_ I've found that site to be have too much misinformation for my tastes. – Eljay Aug 26 '20 at 17:29

3 Answers3

1
Point *p1 = (Point *)a, *p2 = (Point *)b;

This line is initialising two variables in the same line. Like

int a = 2, b = 3;

For pointers, the * is kept near the variable. The following will give you an int * and an int:

 int *a = nullptr, b = 2;

https://cdecl.org


what's the use of FLT_MAX here?

Everything is smaller than FLT_MAX. So this is safe to check against in the first comparison for the minimum number.

puio
  • 1,208
  • 6
  • 19
1

FLT_MAX is a constant defined here and the logic if used to find the Point with the lowest distance between all the pair of points in the array, the idea of setting the variable min with a FLT_MAX is to define a threshold limit value that can be exceeded by the 1st comparison of the 1st 2 Points...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

The compareX function in your first code snippet has the signature required if it is to be passed as the comparator in a call to the standard qsort() function (or other, similar sorting routines provided by the Standard Library). The two pointer arguments must be given as const void* types.

However, when that function is used, the code will actually be passing pointers to Point objects, so the function must first explicitly cast them as such, so that they can be dereferenced in order to do the comparison of their respective x members.

Note: Although using "C-Style" casts will work here, many would frown on such usage. It is, perhaps, more in keeping with "Modern C++" to use static_cast operations:

int compareX(const void* a, const void* b)
{
    const Point *p1 = static_cast<const Point*>(a), *p2 = static_cast<const Point*>(b);
    return (p1->x - p2->x);
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83