Question

In the <math.h> library in C, the function fmin(x,y) returns the minimum value of the two arguments x and y, as documented for instance in the C++ reference.

However, is there a similar function fminimum(a,b,c,...x,y,z) available that finds the minimum of three or more arguments of the same data type?

Was it helpful?

Solution 2

The old fashioned way...

int arr[] = { 1, 3, 6, 100, 50, 72 };

int min_array( int arr[], int len )
{
    int min = arr[0];

    for ( int i = 1; i < len; i++ )
        if ( arr[i] < min )
            min = arr[i];

    return min;
}

OTHER TIPS

Note: The question was originally tagged C++. The original answer works for C++ only, but I have provided a C solution that follows the same principles.

The idea is that pair of pointers representing the start and one past the end of a range is passed to the function, and a pointer to the minimum element is returned.

int* min_element(int *start, int *end)
{
    if (start == end) return end;

    int *min = start++;
    for (; start != end; ++start)
        if (*start < *min) min = start;

    return min;
}

Usage:

int data[] = {1, 5, 3, 66, 4, 81, 23, 2, 6};
int * min = min_element(data, data + 9);
if (min != data + 9)
{
  // range was not empty, min value is *min
}

Original C++ answer There is std::min_element, which gives you an iterator to the minimum element in a range specified by a pair of iterators.

int arr[] = { 3,1,6,8,9,34,17,4,8};
auto it = std::min_element(std::begin(arr), std::end(arr));

Edit 2: From a deleted answer, C++11 has an std::min overload which takes an initializer_list, so you can say

auto minval = std::min({a, b, c, x, y, z});

where a, b, c etc. are all of one type supporting operator<.

The problem with "arbitrary number of arguments" is how do you know how many there are.

The std::min and std::min_element work around this by working on containers - in other words, we know how many thanks to the container object itself knows how many.

In plain C, you can't really do that. So there needs to be some other method.

One way would be to use <cstdarg>, and either mark the end with a special value:

#include <iostream>
#include <cstdarg>

using namespace std;

int countargs(int arg, ...)
{
    if (arg == -1)
    return 0;

    int count = 1;   // arg is not -1, so we have at least one arg.
    va_list vl;
    int cur;
    va_start(vl, arg);
    for(;;)
    {
    cur = va_arg(vl, int);
    if(cur == -1)
        break;
    count++;
    }
    va_end(vl);
    return count;
}


int main()
{
    cout << "Should give 0: " << countargs(-1) << endl;
    cout << "Should give 1: " << countargs(1, -1) << endl; 
    cout << "Should give 3: " << countargs(1, 2, 3, -1) << endl;
    cout << "Should give 6: " << countargs(1, 2, 3, 1, 2, 3, -1) << endl;
    cout << "Should give 12: " << countargs(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, -1) << endl;
    return 0;
}

The above doesn't show how to get the min value, but it shouldn't be that hard to figure out. It's also C++, but doesn't rly on any special C++ features.

The alternative to "marking the end" is to pass the number of elements to the function itself.

Of course, if the arguments are in an array, the real solution is to just iterate through them. And if there aren't that many, you can of course use:

v = min(a, min(b, min(c, d)));; 

The only libc function which could do this would do much more, as it would sort, which can be considered an overkill. It's qsort().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top