Find Maximum and Minimum from an array

Back to Programming

Description

The array of elements is taken as input from the user to find the minimum and maximum element from the array. The minimum element means the smallest element, and the maximum element means the largest element.

Algorithm

INPUT: Array elements

OUTPUT: the minimum and the maximum element of the array

PROCESS:

Step 1:[taking the input]

For i=0 to n-1 repeat

[end of ‘for’ loop]

Step 2: [Function find_min(int a[],int n)]

If n=1 then

Return a[0]

[End of ‘if’]

Return min(a[n-1],find_min(a,n-1))

[Calling the find_min() function recursively to find the minimum element]

[End of ‘find_min()’ function]

Step 3: [Function find_max(int a[],int n)]

If n=1 then

Return a[0]

[End of ‘if’]

Return max(a[n-1],find_max(a,n-1))

[Calling the find_max() function recursively to find the maximum element]

[End of ‘find_max()’ function]

Step 4: Set minimum<-find_min(a,n)

Set maximum<-find_max(a,n)

Print minimum,maximum

Step 5: Stop.

Related