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 is a c++ object?

713


Can we inherit constructor in c++?

668


Where is atoi defined?

655


What is meant by forward referencing and when should it be used?

650


How can a called function determine the number of arguments that have been passed to it?

756






what is the difference between overloading & overriding? give example.

667


What is anonymous object in c++?

723


Can I run c program in turbo c++?

663


What is meant by entry controlled loop?

737


Where can I run c++ program?

700


What is guard code in c++?

739


Assume an array of structure is in order by studentID field of the record, where student IDs go from 101 to 500. Write the most efficient pseudocode algorithm you can to find the record with a specific studentID if every single student ID from 101 to 500 is used and the array has 400 elements. Write the most efficient pseudocode algorithm you can to find a record with a studentID near the end of the IDs, say in the range from 450 to 500, if not every single student ID in the range of 101 to 500 is used and the array size is only 300

1878


What is the difference between set and map in c++?

721


What is isdigit c++?

683


What is the difference between an array and a list?

666