Javatpoint Logo
Javatpoint Logo

Find max in Array Function C++

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:

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:

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

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.

Next TopicStopwatch in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA