C Program to find the maximum and minimum element in the array

In this blog post, we learn how to write a C Program to find the maximum and minimum element in the array? So here will write the C Program to find the maximum and minimum element in the array. We will also see how to display the largest and smallest element of an array using C programming.

So let’s see the logic to find the maximum and minimum elements in the array. Suppose arr is an integer array of size N (arr[N] ), the task is to write the C program to find the maximum and minimum element of the given array.

Examples:

Input: arr[] = {1, 2, 4, -1}
Output:
The minimum element is -1
The maximum element is 4


Input: arr[] = {-1, -1, -1, -1}
Output:
The minimum element is -1
The maximum element is -1

 

 

Logic to find maximum and minimum element in an array in C:

1. Create two intermediate variables max and min to store the maximum and minimum element of the array.
2. Assume the first array element as maximum and minimum both, say max = arr[0] and min = arr[0].
3. Traverse the given array arr[].
4. If the current element is smaller than min, then update the min as the current element.
5. If the current element is greater than the max, then update the max as the current element.
6. Repeat the above two steps 4 and 5 for the element in the array.

 

C program to find maximum and minimum element in the array:

 

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])

// Function to find the minimum and
// maximum element of the array
void findMinimumMaximum(int arr[], int N)
{
    int i;
    // variable to store the minimum
    // and maximum element
    int min = arr[0], max = arr[0];

    // Traverse the given array
    for (i = 0; i < N; i++)
    {
        // If current element is smaller
        // than min then update it
        if (arr[i] < min)
        {
            min = arr[i];
        }
        // If current element is greater
        // than max then update it
        if (arr[i] > max)
        {
            max = arr[i];
        }
    }

    // Print the minimum and maximum element
    printf("minimum element is %d", min);
    printf("\n");
    printf("maximum element is %d", max);

}

int main()
{
    // Given array
    int arr[] = {5, 8, 4, -1 };

    // length of the array
    int N = ARRAY_SIZE(arr);

    // Function call
    findMinimumMaximum(arr, N);

    return 0;
}

Output:

 

 

We can also use the recursive method to find the maximum and minimum elements of an array. If you don’t know about the recursive method, you can read this article “How to use the recursive function in C?“.

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


// Recursive function to find the minimum
// and the maximum element of the array
void recursiveMinMax(int arr[], int N,
                     int* min, int* max)
{
    // Base Case
    if (N < 0)
    {
        return;
    }

    // If current element is smaller
    // than min then update it
    if (arr[N] < *min)
    {
        *min = arr[N];
    }

    // If current element is greater
    // than max then update it
    if (arr[N] > *max)
    {
        *max = arr[N];
    }

    // Recursive call for next iteration
    recursiveMinMax(arr, N - 1, min, max);
}


// Function to find the minimum and
// maximum element of the array
void findMinimumMaximum(int arr[], int N)
{
    int i;
    // variable to store the minimum
    // and maximum element
    int min = arr[0], max = arr[0];

    // Recursive Function to find
    // minimum & maximum element
    recursiveMinMax(arr, N - 1, &min, &max);

    // Print the minimum and maximum element
    printf("minimum element is %d", min);
    printf("\n");
    printf("maximum element is %d", max);

    return;
}


int main()
{
    // Given array
    int arr[] = { 1, 5, 4, -1 };

    // length of the array
    int N = ARRAY_SIZE(arr);

    // Function call
    findMinimumMaximum(arr, N);

    return 0;
}

 

Recommended Articles for you: