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

What are the important differences between c++ and java?

1197


Why would you use pointers in c++?

1215


Why is "using namespace std;" considered bad practice?

1143


find the two largest values among the 6 numbers using control structures : do-while,for,if else,nestedif- else ,while. one or two of them.

2604


Is c++ built on c?

1078


Does c++ have a hash table?

1047


Can there be at least some solution to determine the number of arguments passed to a variable argument list function?

1059


What do c++ programmers do?

1108


How long will it take to learn programming?

1101


What is the best c++ book for beginners?

1205


How can I disable the "echo" feature?

1232


What are static variables?

1191


What is a set in c++?

1029


What relational operators if statements in c++?

1233


Write some differences between an external iterator and an internal iterator?

1083