Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any predefined function in C to find the minimum or maximum element from a given array?

I have an array: int a[6] = {3, 4, 1, 2, 5, 6} and I want to find the minimum and maximum element by using a pre-defined C function; is there one?

like image 854
Shubham Wadkar Avatar asked Sep 21 '21 05:09

Shubham Wadkar


People also ask

How do you find the maximum and minimum of a function in C?

Say max() function is used to find maximum between two numbers. Second, we need to find maximum between two numbers. Hence, the function must accept two parameters of int type say, max(int num1, int num2). Finally, the function should return maximum among given two numbers.

Is there MIN MAX function in C?

the MIN and MAX Function in C. The MIN and MAX functions are used to find the minimum and maximum number from two values and are not predefined in C language. If we want to use the MIN and MAX functions, we must define them in C. We can use macros to define the MIN and MAX functions in the C language.

How do you find the minimum and maximum of an array?

We can use min_element () and max_element () to find minimum and maximum of array. // in an array. # in an array. // (or maximum) element in an array. // element in an array. echo "Minimum element of array: " . echo "Maximum element of array: " . // in an array.

What is the maximum and minimum value of element in C++?

Maximum Value = 21 Minimum Value = 1 This problem can also be solved using the inbuild functions that are provided in the standard template library of the C++ programming language. The methods to find the solution are min_element () and max_element () and these methods are found in the bits/stdc++.h library in C++.

How do you find the minimum value in C programming?

C programming code to find minimum using function. Our function returns index at which minimum element occur. int find_minimum(int[], int); int c, array[100], size, location, minimum; printf("Input number of elements in arrayn");

How to find the smallest number in an array?

How to find smallest number in an array? Our algorithm assumes the first element as the minimum and then compares it with other elements, if an element is smaller than it then it becomes the new minimum, and this process is repeated till complete array is scanned.


Video Answer


2 Answers

No, there isn't a minmax function in the standard library, but you could create one yourself.

Example:

#include <limits.h>
#include <stddef.h>
#include <stdio.h>

// a struct to hold the min and max result
typedef struct {
    int min;
    int max;
} mm;

// a function to find the min and max values
mm minmax(const int *arr, size_t len) {

    mm res = {INT_MAX, INT_MIN}; // start with min = INT_MAX, max = INT_MIN

    for(const int *end = arr + len; arr != end; ++arr) {
        if(*arr < res.min) res.min = *arr;
        if(*arr > res.max) res.max = *arr;
    }

    return res;
}

int main() {
    int a[6] = {3, 4, 1, 2, 5, 6};
    mm res = minmax(a, sizeof a / sizeof *a);
    printf("min: %d  max: %d\n", res.min, res.max);
}


This could be generalized into a function that can find the min and max elements in any type of array, much like the qsort function can sort an array of any type of elements that are comparable in a strict weak ordering kind of way.

#include <stddef.h>

// a struct to hold pointers to the min and max elements
typedef struct {
    const void *min;
    const void *max;
} mm;

// The signature of a function for comparing elements.
// Such a function should return
// -1 if the left hand side is less than the right
// +1 if the right hand side is greater than the left
//  0 otherwise

typedef int (*comp_func)(const void *, const void *);

// the minmax function now takes these arguments:
// in_arr : a "const void*" to the array
// count  : the number of elements
// size   : the size of an element
// compare: a pointer to a function capable of comparing two elements

mm minmax(const void *in_arr, size_t count, size_t size, comp_func compare) {
    mm res = {0}; // both the min and max pointers a NULL

    if(count) {
        // "cur" and "end" are here pointers to "const char[size]" elements,
        // so "++cur" will step "size" bytes in memory:
        const char (*cur)[size] = in_arr;
        const char (*end)[size] = cur + count;

        res.min = cur; // use the pointer to the first value as the pointer to min...
        res.max = cur; // ...and max

        for(++cur; cur != end; ++cur) {
            // call the compare function
            if(compare(cur, res.max) == 1) res.max = cur;
            else if(compare(cur, res.min) == -1) res.min = cur;
        }
    }
    return res;
}

With that minmax function in place, you could use it for an array of int or any type. You just have to supply a function to do the actual comparison of two elements.

Example:

#include <stdio.h>

int int_compare(const void *lhs, const void *rhs) {
    return *((int*)lhs) < *((int*)rhs) ? -1 :
           *((int*)lhs) > *((int*)rhs) ?  1 : 0;
}

int main() {
    int a[6] = {3, 4, 1, 2, 5, 6};
    mm res = minmax(a, sizeof a / sizeof *a, sizeof *a, int_compare);

    // dereference the min and max pointers to get the values:
    printf("min: %d  max: %d\n", *((int*)res.min), *(int*)res.max);
}
like image 200
Ted Lyngmo Avatar answered Nov 15 '22 00:11

Ted Lyngmo


No there is no standard C function. But You can do this yourself

int max = arr[0];
for (i = 1; i < n; i++)
    if (arr[i] > max)
        max = arr[i];

Whether it's big or small is obvious from the comparison inside the if. if >, is large. if <, is small

like image 36
Emin Niftiyev Avatar answered Nov 15 '22 01:11

Emin Niftiyev