Finding the Maximum and Minimum Elements of an Array using C++
Written by
C++ provides its users with a feature to store a list of similar items in the form of Arrays.
You must know by now that Arrays are a group of similar data types stored in contiguous memory locations. Once stored, the array can be used in several ways to extract data for various applications, and in this case, the task is to find the largest and smallest elements of an array.
Suppose we have an array of marks obtained by students in Computer Science and we wish to obtain the maximum and minimum marks scored by a student, this can be easily achieved in C++ with the following algorithm:
Algorithm:
Post creation of an array a[ ] of your desired size and insertion of marks
Step 1: Initialize two variables max and min to a[0]
Step 2: Compare every element of the array with max and min sequentially.
Step 3: If an element is found to be greater than max, its value is stored in max.
Step 4: If an element is found to be less than min, its value is stored in min.
Step 5: Print max and min as the largest and smallest element of the array, once the iteration stops.
Program Explanation:
Let's take an array containing the marks of 4 students for convenience.
Array[] = { 56, 78, 49, 64 }
Step 1: Comparing 56 with the other three elements
56
78
49
64
max,min=A[0] A[1] A[2] A[3]
Step 2: Since 78 > 56, the max value is updated.
56
78
49
64
max=A[1] min=A[0] A[1] A[2] A[3]
Step 3: Since 49 < 56, the min value is updated.
56
78
49
64
max=A[1] min=A[2] A[1] A[2] A[3]
Step 4: Since 64 < 78 and > 49, the iteration ends and max, min are obtained.
56
78
49
64
max=A[1] min=A[2] A[1] A[2] A[3]
The same logic can be applied in 4 distinct ways to create a program in C++:
1)Using the main function (without the use of functions)

Using userdefined function

Using pointers

Using library functions
Using the main function:
This is an iterative approach and uses the if condition to check for the largest and smallest element while traversing through the array in the main function.
Source Code:
#include <iostream>
#define SIZE 50 // Defining max size of array
int main()
{
int array[SIZE];
int i, max, min, size;
cout << "Enter size of the array: ";
cin >> size;
// Input array elements
cout << "\n Enter the marks of " << size << " students: ";
for (i = 0; i < size; i++)
cin >> array[i];
max = array[0];
min = array[0];
for (i = 1; i < size; i++)
{
// Checking for max
if (array[i] > max)
max = array[i];
// Checking for min
if (array[i] < min)
min = array[i];
}
// Print maximum and minimum element
cout << "\nMaximum marks =" << max << "\n";
cout << "Minimum marks =" << min;
return 0;
}
Output:
Enter size of the array:
4
Enter the marks of 4 students:
56
78
49
64
Maximum marks =78
Minimum marks =49
Using userdefined functions:
Here, we define two functions FindMax and FindMin to increase the readability and modularity of the code. The entire array of student’s marks is passed to both functions and is accepted as a parameter along with its size by the function. The two functions then return the max and min value respectively to the main function to print the output.
Note: Similar to other data types, arrays can be easily passed to functions as arguments.
However, the manner in which it is passed differs markedly from that of an ordinary variable. While mentioning the actual argument in the function call, only the array name must be mentioned i.e, without any brackets. But, when declaring as a formal parameter in the function definition the array name is written along with a pair of empty square brackets.
Syntax:
<Function\_name>(_array\_name, parameters_) //Function call array\_name as actual parameter
<return\_type>(_array\[ \], parameters_)// Function declaration
Source Code:
#include <iostream>
using namespace std;
#define size 8
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)
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)
min =a[i];
}
return min; //returns the smallest number to main function
}
int main()
{
int arr[] = {57, 54, 69, 92, 78, 67, 85, 91}; //Marks of 8 students
int max, min;
max = FindMax(arr,size); //Passing the array
min = FindMin(arr,size); //and calling the function
cout<<"Maximum marks scored by a student:" << max << "\n";
cout<<"Minimum marks scored by a student:" << min << "\n";
return 0;
}
Output:
Maximum marks scored by a student:92
Minimum marks scored by a student:54
Using Pointers:
An important concept in C++, pointers are variables that hold a memory location and are denoted by the star ‘*’ operator. They are very closely linked with arrays. The name of the array points to the address of the first element. The use of pointers in arrays is an efficient way to access its elements as they are stored in contiguous memory locations.
The program below uses the following steps:
Step 1: Declaring two pointers max and min of data type int.
Step 2: Assigning max and min variables to the array in order to point to the address of the array's first element.
Step 3: Using the unary star operator ‘*’ i.e, the pointer operator to access the values.
Step 4: Using the above mentioned logic or algorithm.
The *max and *min work similar to an ordinary variable containing the largest or smallest value of an array like max and min.
Source Code:
#include <iostream>
using namespace std;
int main()
{
int array[50], *max, *min, size, i; //pointer variables declared for max and min
cout<<"Enter the number of elements in array\n";
cin>>size;
cout<<"Enter array elements\n";
for ( i = 0 ; i < size ; i++ )
cin>>array[i];
max = array; //assigning pointer to the address of the first element
min = array;
for (i = 0; i < size; i++)
{
//Finding the largest element in the array
if (*(array+i) > *max)
*max = *(array+i);
}
cout<<"Maximum element in the array is "<< *max << "\n" ;
for (i = 0; i < size; i++)
{
//Finding the smallest element in the array
if (*(array+i) < *min)
*min = *(array+i);
}
cout<<"Minimum element in the array is"<< *min <<"\n";
return 0;
}
Output:
Enter the number of elements in array
5
Enter array elements
67
32
45
87
66
Maximum element in the array is 87
Minimum element in the array is 32
Using Library Functions:
The C++ Standard Library offers an extensive collection of predefined or inbuilt functions under a number of header files for you to carry out important operations such as error checking, calculations, input/output and manipulations.
To find the maximum and minimum elements of an array we use the std::min_element and std::max_element template functions defined in the
Syntax for the functions:
<function\_name>(_Iterator first, Iterator last_)
, where Iterator first points to the beginning of the range, Iterator last points to the end of the range and function_name= min/max_element.
Source Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int array[50], *maximum, *minimum, size, i; //pointer variables declared for max and min
cout<<"Enter the number of elements in array\n";
cin>>size;
cout<<"Enter array elements\n";
for ( i = 0 ; i < size ; i++ )
cin>>array[i];
minimum= min_element(array,array+size);
cout<<"Minimum element in the array is "<< *minimum <<"\n";
maximum= max_element(array,array+size);
cout<<"Maximum element in the array is "<< *maximum <<"\n";
return 0;
}
Output:
Enter the number of elements in array
4
Enter array elements
67
9
87
97
Minimum element in the array is 9
Maximum element in the array is 97
Note:
std::min_element, std::max_element and min_element, max_element are one and the same.
So these are the four easy techniques to find the maximum and minimum elements of an array in C++
Now: Go, practice and master them!