We will use two arrays in this recipe. The first array will contain the values from which the maximum and minimum values have to be found. The second array will be used to store the minimum and maximum values of the first array.
Let's call the first array arr and define it to contain five elements with the following values:
Let's call our second array mm. The first location, mm[0], of the mm array will be used for storing the minimum value and the second location, mm[1], for storing the maximum value of the arr array. To enable comparison of the elements of the mm array with the elements of the arr array, copy the first element of the arr array at arr[0] to both mm[0] and mm[1]:
Now, we will compare the rest of the elements of the arr array with mm[0] and mm[1]. To keep the minimum value at mm[0], any element smaller than the value at mm[0] will be assigned to mm[0]. Values larger than mm[0] are simply ignored. For example, the value at arr[1] is smaller than that at mm[0], that is, 8 < 30. So, the smaller value will be assigned to mm[0]:

We will apply reverse logic to the element at mm[1]. Because we want the maximum value of the arr array at mm[1], any element found larger than the value at mm[1] will be assigned to mm[1]. All smaller values will be simply ignored.
We will continue this process with the next element in the arr array, which is arr[2]. Because 77 > 8, it will be ignored when compared with mm[0]. But 77 > 30, so it will be assigned to mm[1]:
We will repeat this procedure with the rest of the elements of the arr array. Once all the elements of the arr array are compared with both the elements of the mm array, we will have the minimum and maximum values at mm[0] and mm[1], respectively:
Let's use GCC to compile the returnarray.c program, as follows:
D:\CBook>gcc returnarray.c -o returnarray
Here is the output of the program:
D:\CBook>./returnarray
How many values? 5
Enter 5 values
30
8
77
15
9
Minimum value is 8
Maximum value is 77
Voilà! We've successfully returned the maximum and minimum values in an array.
Now, let's move on to the next recipe!