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

Which is most difficult programming language?

769


What are the two main components of c++?

803


When can I use a forward declaration?

792


What are static variables?

793


How many types of modularization are there in c++?

758






What is an adaptor class or wrapper class in c++?

785


What do the header files usually contains?

797


Is swift faster than c++?

744


What is a pointer with example?

856


Can I create my own functions in c++?

789


What is null pointer and void pointer?

802


What are c++ variables?

754


What is the difference between interpreters and compilers?

822


Tell me an example where stacks are useful?

761


What operator is used to access a struct through a pointer a) >> b) -> c) *

818