Find Max In Array Function C++ - C++ Programming Tutorial
C++ Course / Functions / Find Max In Array Function C++

Find Max In Array Function C++

BLUF: Mastering Find Max In Array Function C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Find Max In Array Function C++

C++ is renowned for its efficiency. Learn how Find Max In Array Function C++ enables low-level control and high-performance computing in the tutorial below.

An array is a collection of interconnected data elements stored contiguously in memory. Accessing each element is only possible by referencing its unique index, establishing it as the fundamental data structure.

Organizing the elements of the array in ascending order simplifies the process of pinpointing the item with the highest value. Once the sorting is completed, the initial item will stand for the smallest value in the array, followed by the next smallest value, the largest value, and so forth.

In this guide, we will explore the process of determining the highest value within an array by employing a function.

Using Linear Traversal

The most straightforward and basic approach to address this issue is to simply iterate through the complete list and choose the one that has the maximum value.

Example:

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:

Output

Largest in given array is 9808
..........................
Process executed in 1.22 seconds
Press any key to continue.

Explanation

To retain the topmost value within the list, establish a new variable named max locally. To kick off the evaluation, set max to the initial element. Proceed through the given array beginning from the second element and continuing until the zeroth element, evaluating each element against the maximum. Update max with the current element if it surpasses the current max value. Finally, output and showcase the value of the highest array element stored in max.

Using Library Function

Example:

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:

Output

9808
..........................
Process execute din 1.33 seconds
Press any key to continue.

Explanation

To identify the highest value, many programming languages offer a built-in function like max such as C++'s std::max_element. This function enables us to efficiently find the maximum element.

Using Loops

Example

#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:

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience