C program to find maximum and minimum in array
Written by
Let us now understand how to find the maximum and the minimum or the largest and the smallest element of a particular one-dimensional array.
Suppose, if you have an array consisting of student grades, you might want to know which individual has secured the highest or the lowest marks and what is the highest and the lowest grade.
Logic: The idea is so that, initially we consider the array’s first element to be both maximum and minimum value. As and when we traverse the array, we compare the value of the elements in the array with that of current value of ‘max’ or ‘min’ and respectively assign the values if the current value is not maximum for ‘max’ and not minimum for ‘min’.
There are three methods using which we can write a program to find max or min element. They are as follows:
Technique 1: Basic without any functions
The logic used for solving the problem remains same across all the approaches, however the flow of the program differs a bit. We shall depict each approach’s flowchart for better understanding.
Flowchart:
We first compare to check and assign value for max no; once done next check is for min no. The value of each element of array is compared against them to derive the final result.
Code:
#include <stdio.h>
#define SIZE 50 //Defining max size of array
int main(){
int array[SIZE];
int i, max, min, size, pos1, pos2;
// Input size of the array
printf("Enter size of the array: ");
scanf("%d", &size);
// Input array elements
printf("Enter elements in the array: ");
for(i=0; i<size; i++){
scanf("%d", &array[i]);
}
// Assume first element as maximum and minimum
max = array[0];
min = array[0];
//Find maximum and minimum in all array elements.
for(i=1; i<size; i++){
// If current element is greater than max
if(array[i] > max){
max = array[i];
pos1 = i + 1;
}
// If current element is smaller than min
if(array[i] < min){
min = array[i];
pos2 = i + 1;
}
}
// Print maximum and minimum element
printf("Maximum element = %d is at position %d\n", max, pos1);
printf("Minimum element = %d is at position %d\n", min, pos2);
return 0;
}
Output:
Enter size of the array: 5
Enter elements in the array: 20
58
74
2336
25
Maximum element = 2336 is at position 4
Minimum element = 20 is at position 1
Technique 2 : Using Functions
Here we use two functions : one for finding the largest number and the other the smallest one. We pass the array and the size of the array to functions in order to compute the result.
Refer flowchart 2 to understand this program.
Code:
#include <stdio.h>
int FindMax(int[],int); //Function delcaration to find maximum number in array
int FindMin(int[],int); //Function delcaration to find minimum number in array
int main(){
int i, array[50], size, maximum, minimum;
printf("Input number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
maximum = FindMax(array,size); //calls the max function
minimum = FindMin(array,size); //calls the min function
// Print maximum and minimum element
printf("Maximum element in the array is:%d\n",maximum);
printf("Minimum element in the array is: %d\n",minimum);
return 0;
}
int FindMax(int a[],int n) //function to find largest element
{
int i, max;
max = a[0];
for(i=1;i<n;i++){
if(a[i]>max){
// If current element is greater than max
max =a[i];
}
}
return max; //returns the largest number to main function
}
int FindMin(int a[],int n) //function to find smallest element
{
int i, min;
min = a[0];
for(i=1;i<n;i++){
if(a[i]<min){
// If current element is smaller than min
min =a[i];
}
}
return min; //returns the smallest number to main function
}
Output:
Input number of elements in array
5
Enter 5 integers
25
69
875
12036
13
Maximum element in the array is : 12036
Minimum element in the array is : 13
Technique 3: Using Pointers
As we know, the name of the array is equal to the address of its first element; similarly a pointer is equal to the address of the element pointed to by it. Thus arrays and pointers use the same concept. ( recall array and pointers to understand better).
Here we declare two pointer variables maximum and minimum which will initially point to the address of the first element of the array. i.e when we write maximum = array; we are actually assigning the address pointed to by our array to the pointer variable.
In order to access the values located at address pointed by pointer we use the unary operator *. Thus, maximum* converts to ” value at location pointed by maximum”.
Refer flowchart 3 to understand this program.
Code:
#include <stdio.h>
int main()
{
int array[50], *maximum, *minimum, size, i; //pointer variables declared for max and min
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter array elements\n");
for ( i = 0 ; i < size ; i++ )
scanf("%d", &array[i]);
maximum = array; //maximum pointer will now point to the address of the first element
minimum = array; //minimum pointer will now point to the address of the first element
for (i = 0; i < size; i++){
//Finding the largest element in the array
if (*(array+i) > *maximum){
*maximum = *(array+i);
}
}
printf("Maximum element in the array is %d.\n", *maximum);
for (i = 0; i < size; i++){
//Finding the smallest element in the array
if (*(array+i) < *minimum){
*minimum = *(array+i);
}
}
printf("Minimum element in the array is %d.\n", *minimum);
return 0;
}
Output:
Enter the number of elements in array
4
Enter array elements
89
25
47
15
Maximum element in the array is 89.
Minimum element in the array is 15.