Find the second maximum in an array?

Answer Posted / jeena

/*Code to write second Minimum Number*/
int[] intarr = { 2, 5, 1, 8, 3, 6, 0, 4, 3, 2, 78, 1, 8 };

int intminval = 0, intsecondminval = 0;

for (int i = 0; i < intarr.Length; i++)
{
if (i == 0)
{
intminval = intsecondminval = intarr[i];
}
else
{
if (intarr[i] < intminval)
{
intsecondminval = intminval;
intminval = intarr[i];

}
else if (intminval == intsecondminval && intarr[i] > intminval)
{
// this conditon is to handle the case
//where the array contains only 2 values
// for e.g. {1,1,2,1,2,2,1}
intsecondminval = intarr[i];
}
}
}

Is This Answer Correct ?    5 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.

641


Can you Mention some Application of C/C++?

613


What are the advantages of prototyping?

556


Mention the purpose of istream class?

605


What is purpose of abstract class?

575






What is protected inheritance?

587


What is searching?

638


Write about the members that a derived class can add?

559


What is cout flush?

561


What will strcmp("Astring", "Astring"); return a) A positive value b) A negative value c) Zero

626


Which one is better- macro or function?

638


What is the oldest programming language?

555


Why do we need constructors in c++?

603


What is static function? Explain with an example

549


What is size_type?

535