Find the second maximum in an array?

Answer Posted / vinod, bangalore, india

int Second_Max(int* numbers, int Lenght)
{

int Max, Sec_Max, index;
Max = Sec_Max = numbers[0];
for(index=0; (index<Lenght) &&(Max == Sec_Max);
index++)
{
if(Max > numbers[index])
Sec_Max = numbers[index];
else
Max = numbers[index];
}
if(index == Lenght)
{
printf("Array contain simillar data and the
data is = %d \n", Max);
return false;
}


for(index =0; index < Lenght; index++)
{
if(numbers[index] > Max)
{
Sec_Max = Max;
Max = numbers[index];
}
if((numbers[index] < Max) && (numbers
[index] > Sec_Max))
{
Sec_Max = numbers[index];
}
}
return Sec_Max;
}

Is This Answer Correct ?    2 Yes 7 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does getch() do according to the ANSI C++ standard a) Reads in a character b) Checks the keyboard buffer c) Nothing in particular (Its not defined there)

594


Does c++ support multilevel and multiple inheritances?

537


What is namespace & why it is used in c++?

599


What is ios flag in c++?

669


Is c++ a float?

595






What is pure virtual function?

619


If a function doesn’t return a value, how do you declare the function?

608


I was a c++ code and was asked to find out the bug in that. The bug was that he declared an object locally in a function and tried to return the pointer to that object. Since the object is local to the function, it no more exists after returning from the function. The pointer, therefore, is invalid outside.

599


How a modifier is similar to mutator?

621


What can I safely assume about the initial values of variables which are not explicitly initialized?

615


What is problem with overriding functions?

599


How many standards of c++ are there?

616


What is meant by entry controlled loop? What all C++ loops are exit controlled?

558


What is a wchar_t in c++?

575


Difference between Abstraction and encapsulation in C++?

570