Find the second maximum in an array?

Answer Posted / jaldeep

int sec_max(int a[])
{
if(a[0]>a[1])
{
max=a[0]
sec_max=a[1]
}
else
{
max=a[1]
sec_max=a[0]
}
for(i=1;i<n-1;i++)
{
if (a[i]>sec_max && a[i]< max)
{
sec_max=a[i];
}
else if(a[i]>max)
{
sec_max=max;
max=a[i];
}
return sec_max;
}

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you establish an is-a relationship?

613


What is constructor and destructor in c++?

607


When does a 'this' pointer get created?

606


Explain function overloading

576


What is rtti in c++?

616






Why do we use double in c++?

587


If a round rectangle has straight edges and rounded corners, your roundrect class inherits both from rectangle and from circle, and they in turn both inherit from shape, how many shapes are created when you create a roundrect?

655


which of the following is not an secondary constant a) array b) real c) union

1245


Explain how an exception handler is defined and invoked in a Program.

576


Is std :: string immutable?

553


What is the arrow operator in c++?

550


What are mutator methods in c++?

635


Can I learn c++ without knowing c?

581


How new/delete differs from malloc()/free?

600


What is c++ programming language?

565