Wednesday, 5 August 2015

C Plus Plus function to find maximum in arr[] of size n

Leave a Comment
Questions:

C Plus Plus function to find maximum in arr[] of size n

Code:

// C Plus Plus function to find maximum in arr[] of size n
#include<iostream>
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;
}

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)<<endl;
    return 0;
}



Output


If You Enjoyed This, Take 5 Seconds To Share It