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.
Largest in given array is 9808 .......................... Process executed in 1.22 seconds Press any key to continue.
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
9808 .......................... Process execute din 1.33 seconds Press any key to continue.
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.
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.