It will take n comparisons.....observe this code....

#include<iostream>
using namespace std;
int sec_largest(int ar[],int n)
{
        int i,max=-32767,sec_max=0;
        for(i=0;i<n;i++)
        {
                if(ar[i]>max)
                {
                        sec_max=max;
                        max=ar[i];
                }
        }
        return sec_max;
}
main()
{
        int n,i,res;
        cout<<"enter number of elements\n";
        cin>>n;
        int ar[n];
        for(i=0;i<n;i++)
                cin>>ar[i];
        res=sec_largest(ar,n);
        cout<<"second largest number="<<res<<endl;
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to