On Mon, Sep 28, 2009 at 11:56 AM, harit agarwal <agarwalha...@gmail.com>wrote:
> It will take n comparisons.....observe this code.... > > > The code fails for the input 4 3 2 1. > #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; > > } > > > > > -- Shishir Mittal Ph: +91 9936 180 121 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---