An array is a group of related data pieces kept in close proximity to one another in memory. The sole way to retrieve each data piece directly is by using its index number, making it the most basic data structure.
Arranging the array's items in ascending order makes it easy to identify the one with the biggest value. Following sorting, the first element will represent the array's smallest element, followed by the second-smallest element, the biggest element, and so on.
In this article we will see how to find the maximum element in an array using function
Using Linear Traversal
The easiest and most fundamental way to handle this problem is to just go through the entire list and select the one with the highest value.
Example:
// C++ program to find maximum
// in arr [ ] of size n
#include < bits / stdc + + . h >
#include < sdtlib >
#include < iostream >
#include < stdio >
#include < conio. H >
using namespace std ;
int largest ( int arr [ ] , int n )
{
int i ;
// Initialize maximum element
int max = arr [ 0 ] ;
// Traverse array elements
// from second and compare
// every element with current max
for ( i = 1 ; i < n ; i + + )
if ( arr [ i ] > max )
max = arr [ i ] ;
return max ;
}
// Driver Code
int main ( )
{
int arr [ ] = { 10 , 324 , 45 , 90 , 9808 } ;
int n = sizeof ( arr ) / sizeof (arr [ 0 ] ) ;
cout << " Largest in given array is " << largest ( arr , n ) ;
return 0 ;
}
Output:
Largest in given array is 9808
..........................
Process executed in 1.22 seconds
Press any key to continue.
Explanation
To save the highest value in the list, create a local variable called max. To begin the comparison, initialize max with the first element. Then go through the supplied array starting at element two and going all the way to element zero, comparing each element to the maximum. Replace the value of max with the current element if the current element exceeds max. Return and display the value of the greatest array element that was put in max at the end.
Using Library Function
Example:
// C++ program to find maximum in arr[] of size n
#include < bits / stdc + + . h >
#include < sdtlib >
#include < iostream >
#include < stdio >
#include < conio. H >
using namespace std ;
// returns maximum in arr [ ] of size n
int largest ( int arr [ ] , int n )
{
return * max_element ( arr , arr + n ) ;
}
int main ( )
{
int arr [ ] = { 10 , 324 , 45 , 90 , 9808 } ;
int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ;
cout << largest ( arr , n ) ;
return 0 ;
}
Output:
9808
..........................
Process execute din 1.33 seconds
Press any key to continue.
Explanation
To determine the largest element, most languages include an appropriate built-in function of the max type, such as C++'s std : : max element. This function allows us to quickly determine the largest element.
Using Loops
#include < stdio . h >
#include < bits / stdc + + . h >
#include < iostream >
#include < stdlib >
#include < conio . h>
int main ( ) {
int n ;
double arr [ 100 ] ;
cout << " enter the number of elements ( 1 to 100 ) : " << endl ;
cin >> n ;
for ( int i = 0 ; i < n ; + + i ) {
cout << "enter number : " << i + 1 << endl;
cin >> arr [ i ] ;
}
// storing the largest number to arr [ 0 ]
for ( int i = 1 ; i < n ; + + i ) {
if ( arr [ 0 ] < arr [ i ] ) {
arr [ 0 ] = arr [ i ] ;
}
}
cout<< " Largest element = " , arr [ 0 ] ) ;
return 0 ;
}
Output:
enter the number of elements ( 1 to 100 ) : 5
enter number1 : 34.5
enter number2 : 2.4
enter number3 : -35.5
enter number4 : 38.7
enter number5 : 24.5
Largest element = 38.70
..........................
Process executed in 1.22 seconds
Press any key to continue.
Explanation
- The user inputs n items, which this software keeps in the arr array.
- To determine the biggest ingredient,
- The largest of the array's first two members is verified, and it is then stored in arr [ 0 ].
- After checking the first and third items, the larger of these two is placed in arr [0].
- until the first and last components are examined, this procedure continues.
- The arr [ 0 ] location will be used to hold the greatest number.