Search code examples
cminimummath.h

Function to get minimum of three or more arguments


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?


Solution

  • 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;
    }