Program to find largest element in an array | C ++ program to find maximum in arr [ ] of size n ; Method to find maximum in arr [ ] ; Initialize maximum element ; Traverse array elements from second and compare every element with current max ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largest ( int arr [ ] , int n ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ; return max ; } 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 ; }