The maximum or minimum value may be determined by comparing the values of array elements. Let us assume that the maximum value is represented by ‘max’. The value of the first element of the array is assigned to max.
This is compared with the value of the second element of the array. If the second element is larger in value than max, then we assign the second element to max and compare it with the third element. However, if the second element is smaller than max then no action is taken and the comparison shifts to the third element. If the third element is larger than max, we assign the value of the third element to max and compare it with the fourth element. The process is carried on to the last element of array. The value of max is the maximum value in the array. The same procedure is adopted to determine the minimum value in array. Program illustrates the procedure.
Illustrates finding of the array elements with maximum and minimum values in an array
#include <stdio.h> #define n 11 void main () { int i,j,m,k, max, min; int Array[n]; clrscr(); printf("Enter %d integers:", n); for(i=0; i<n; i++) scanf("%d", & Array [i]); printf("\nyou have entered the following numbers: \n"); for( j =0;j<n;j++) printf("%d ",Array [j]); printf ("\n"); max= Array [0]; { for(k =0;k<n;k++) if(Array [k]>max) max= Array [k]; else max = max; } printf("Maximum number is = %d\n",max); min= Array [0]; { for(m =0;m<n;m++) if(Array [m]<min) min= Array [m]; else min = min; } printf("Minimum number= %d\n",min); }