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 TraversalThe 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 FunctionExample: 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 LoopsOutput: 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
Next TopicStopwatch in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India