C program to find maximum and minimum elements in array using recursion

Write a C program to find maximum and minimum elements in a given array using recursion. How to find maximum and minimum elements in any given array using recursion in C programming. Using recursion finding maximum or minimum elements in array in C program.

Example:
Input size of array: 10
Input elements in array: 5, 1, 6, 10, 2, 3, 6, 50, -7, 4
Output maximum: 50
Output minimum: -7

Required knowledge

Basic C programming, Array, Function, Recursion

Till now we have done many exercises on minimum and maximum we have learnt how to find maximum or minimum in array using loops. We have also seen some examples on array programs using recursion such as printing array elements using recursion. Here in this program we will be using similar approach to find maximum and minimum elements in array.

Program to find maximum and minimum elements in array

/**
 * C program to find maximum and minimum elements in array using recursion
 */

#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the array


/* Function declarations */
int maximum(int array[], int start, int len);
int minimum(int array[], int start, int len);




int main()
{
    int array[MAX_SIZE], N, max, min;
    int i;

    /* Reads size and elements of array */
    printf("Enter size of the array: ");
    scanf("%d", &N);
    printf("Enter %d elements in array: ", N);
    for(i=0; i<N; i++)
    {
        scanf("%d", &array[i]);
    }

    max = maximum(array, 0, N);
    min = minimum(array, 0, N);

    printf("Minimum element in array = %d\n", min);
    printf("Maximum element in array = %d\n", max);

    return 0;
}



/**
 * Recursive function to find maximum element in the given array.
 */
int maximum(int array[], int start, int len)
{
    int max;

    /*
     * If last and second last element only needs to be matched
     */
    if(start >= len-2)
    {
        if(array[start] > array[start+1])
            return array[start];
        else
            return array[start+1];
    }


    /*
     * Recursively calls maximum to find maximum element
     * in right side of the array from current start index.
     */
    max = maximum(array, start+1, len);

    /*
     * If maximum element in right side of the array is
     * greater than current start index element
     */
    if(array[start] > max)
        return array[start];
    else
        return max;
}



/**
 * Recursive function to find minimum element in the array.
 */
int minimum(int array[], int start, int len)
{
    int min;

    if(start >= len-2)
    {
        if(array[start] < array[start+1])
            return array[start];
        else
            return array[start+1];
    }

    min = minimum(array, start+1, len);

    if(array[start] < min)
        return array[start];
    else
        return min;
}

Note: You can also use conditional operators to write the same program in short.

Program using conditional operator

/**
 * C program to find maximum and minimum elements in array using recursion
 */

#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the array


/* Function declarations */
int maximum(int array[], int start, int len);
int minimum(int array[], int start, int len);




int main()
{
    int array[MAX_SIZE], N, max, min;
    int i;

    /* Reads size and elements of array */
    printf("Enter size of the array: ");
    scanf("%d", &N);
    printf("Enter %d elements in array: ", N);
    for(i=0; i<N; i++)
    {
        scanf("%d", &array[i]);
    }

    max = maximum(array, 0, N);
    min = minimum(array, 0, N);

    printf("Minimum element in array = %d\n", min);
    printf("Maximum element in array = %d\n", max);

    return 0;
}



/**
 * Recursive function to find maximum element in the given array.
 */
int maximum(int array[], int start, int len)
{
    int max;

    if(start >= len-2)
        return (array[start] > array[start + 1])
                    ? array[start]
                    : array[start + 1];

    max = maximum(array, start+1, len);

    return (array[start] > max)
                ? array[start]
                : max;
}



/**
 * Recursive function to find minimum element in the array.
 */
int minimum(int array[], int start, int len)
{
    int min;

    if(start >= len-2)
    {
        return (array[start] < array[start + 1])
                    ? array[start]
                    : array[start + 1];
    }

    min = minimum(array, start+1, len);

    return (array[start] < min)
                ? array[start]
                : min;
}


Output
Enter size of the array: 10
Enter 10 elements in array: -1 0 5 4 3 10 999 -1 100 -100
Minimum element in array = -100
Maximum element in array = 999

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment