Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Find the second maximum in an array?

Answer Posted / gang

public void getSecondMax(double[] arr){
double fmax, smax;
fmax=arr[0];
smax=arr[1];

for (int i = 1; i < arr.length; i++) {
if (arr[i]>fmax){
smax = fmax;
fmax = arr[i];
}
else if (arr[i]>smax)
smax = arr[i];


}

System.out.println("The 1st
highest="+fmax+"\t"+"The 2nd highest="+smax);
}

Note that it would not work if the array's size is only 1.

Is This Answer Correct ?    4 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain binary search.

999


What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?

1127


What is an operator function? Describe the function of an operator function?

1166


Why can’t you call invariants() as the first line of your constructor?

1035


What is while loops?

1068


Explain Memory Allocation in C/C++ ?

1090


Write a function to perform the substraction of two numbers. Eg: char N1="123", N2="478", N3=-355(N1-N2).

1008


Can a Structure contain a Pointer to itself?

1157


How many keywords are used in c++?

1030


What is the sequence of destruction of local objects?

985


Keyword mean in declaration?

1072


What is c++ & why it is used?

1110


What is a container class? What are the types of container classes in c++?

1283


What it is and how it might be called (2 methods).

1196


What is the rule of three?

1018