- The size of the array whose maximum and minimum values have to be found out is not fixed, hence we will define a macro called max of size 100:
#define max 100
- We will define an arr array of the max size, that is, 100 elements:
int arr[max];
- You will be prompted to specify the number of elements in the array; the length you enter will be assigned to the n variable:
printf("How many values? ");
scanf("%d",&n);
- Execute a for loop for n number of times to accept n values for the arr array:
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
- Invoke the maxmin function to pass the arr array and its length, n, to it. The array that will be returned by the maxmin function will be assigned to the integer pointer, *p:
p=maxmin(arr,n);
- When you look at the function definition, int *maxmin(int ar[], int v){ }, the arr and n arguments passed to the maxmin function are assigned to the ar and v parameters, respectively. In the maxmin function, define an mm array of two elements:
static int mm[2];
- To compare it with the rest of the elements, the first element of ar array is stored at mm[0] and mm[1]. A loop is executed from the 1 value till the end of the length of the array and within the loop, the following two formulas are applied:
- We will use mm[0] to store the minimum value of the arr array. The value in mm[0] is compared with the rest of the elements. If the value in mm[0] is greater than any of the array elements, we will assign the smaller element to mm[0]:
if(mm[0] > ar[i])
mm[0]=ar[i];
- We will use mm[1] to store the maximum value of the arr array. If the value at mm[1] is found to be smaller than any of the rest of the array element, we will assign the larger array element to mm[1]:
if(mm[1]< ar[i])
mm[1]= ar[i];
- After the execution of the for loop, the mm array will have the minimum and maximum values of the arr array at mm[0] and mm[1], respectively. We will return this mm array to the main function where the *p pointer is set to point at the returned array, mm:
return mm;
- The *p pointer will first point to the memory address of the first index location, that is, mm[0]. Then, the content of that memory address, that is, the minimum value of the array, is displayed. After that, the value of the *p pointer is incremented by 1 to make it point to the memory address of the next element in the array, that is, the mm[1] location:
printf("Minimum value is %d\n",*p++);
- The mm[1] index location contains the maximum value of the array. Finally, the maximum value pointed to by the *p pointer is displayed on the screen:
printf("Maximum value is %d\n",*p);
The returnarray.c program explains how an array can be returned from a function. Basically, the program returns the minimum and maximum values of an array:
/* Find out the maximum and minimum values using a function returning an array */
# include <stdio.h>
#define max 100
int *maxmin(int ar[], int v);
void main()
{
int arr[max];
int n,i, *p;
printf("How many values? ");
scanf("%d",&n);
printf("Enter %d values\n", n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
p=maxmin(arr,n);
printf("Minimum value is %d\n",*p++);
printf("Maximum value is %d\n",*p);
}
int *maxmin(int ar[], int v)
{
int i;
static int mm[2];
mm[0]=ar[0];
mm[1]=ar[0];
for (i=1;i<v;i++)
{
if(mm[0] > ar[i])
mm[0]=ar[i];
if(mm[1]< ar[i])
mm[1]= ar[i];
}
return mm;
}
Now, let's go behind the scenes.