Solved programming problem

Finding Max & Min value of 1D array with Recursion in C.

Source code

#include <stdio.h>

int array[100]; ///declaring global array,max and min.

int max=0;

int min=100;

int max_min(int *ptr,int len, int i)

{

    if(i==len)

    {

        return; ///check until last index

    }

    else

    {

        if(*(ptr+i)>max)  ///checking for the maximum

        {

            max=*(ptr+i);

        }

        if(*(ptr+i)<min) ///checking for the minimum

        {

            min=*(ptr+i);

        }

        max_min(ptr,len,i+1); /// after checking calling the same function

    }

}

int main()

{

    int len,i,*ptr;

    printf("Please Enter The array size: ");

    scanf("%d",&len);

    printf("Please Enter %d Element of the array: 
",len);

    for(i=0;i<len;i++)

    {

        scanf("%d",&array[i]);

    }

    ptr=array;///saving the array address in pointer

    max_min(ptr,len,0); ///calling the function

    printf("
Max: %d",max);

    printf("
Min: %d
",min);

    return 0;

}

Output

Please Enter The array size: 5
Please Enter 5 Element of the array:
1 6 -9 5 10

Max: 10
Min: -9

 

Join the discussion

Add your comment