Find the second maximum in an array?

Answer Posted / rw-rwx

This might help.


max1=a[0];max2=a[1];

for(i=1;i<n;i++)
{
if(max1<a[i])
{
max2=max1;
max1=a[i];
}



if(max2==max1) max2=a[i+1];

if(max2==a[n]) { printf("All numbers are the same no second max.\n"); return 0;}


if(max2<a[i] && max1!=a[i]) max2=a[i];
}

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is prototype for that c string function?

682


What is private, public and protected inheritance?

581


What is a string example?

541


What is the c++ code?

561


what kind of projects are suitable for c and c++

612






Given the following seqment of code containing a group of nested if instructions: y = 9; if ((x==3) || (x == 5)) y++; else if (x == 2) y *= 2; else if (x == ) y-= 7; else y = 8; if the value of x is 4 before the nested IFs are executed, what is the value of y after the nested IFs are executed?

1574


How do you sort a sort function in c++ to sort in descending order?

546


Is c the same as c++?

553


Explain linked list using c++ with an example?

622


Can you pass an array to a function in c++?

529


Explain the isa and hasa class relationships.

583


What is an accessor in c++?

610


What do you know about near, far and huge pointer?

589


Why is c++ awesome?

565


What doescout<<(0==0) print out a) 0 b) 1 c) Compiler error: Lvalue required

566