Question:

Display the second highest score (arrays)?

by  |  earlier

0 LIKES UnLike

Using the array scores you have declared in Question 1, write the segment of code to compute and display the second highest score in the array (The second highest score should not be the same as the highest score. In the array score, since there are 2 students having the same highest score, the second highest score should be 91.)

This is the program for Question 1:

public class ComputeScore {

public static void main(String [ ] args) {

int scores [ ] = {74,62,54,95,45,36,91,75,64,95};

for(int j=0; j<scores.length; j++) {

System.out.println("Scores in the array are: " + scores[ j ]);

}

int high = scores[0];

for(int j=1; j<scores.length; j++) {

if (high < scores[j]) {

high = scores[j];

}

}

double sum=0;

for (int j=0; j<scores.length; j++) {

sum+=scores[j];

}

double average = (sum/scores.length);

System.out.println("The average is " + average);

System.out.println("The highest score is " + high);

=====================

I have no idea on how to make use of the solution in Question 1 to write the codes to compute and display the second highest score in the array.

Please render me your help and explain too thank you (:

 Tags:

   Report

4 ANSWERS


  1. int secondhigh = 0;

    if (scores.length &gt; 0) {

    for (int j = 1; i &lt; scores.length; j--) {

    if (secondhigh &lt; scores[j] &amp;&amp; scores[j] != high)  //to make sure secondhigh score is within the scores[j] and is not the highest score

    secondhigh = score[j].

    }

    System.out.println(&quot;Second highest score is : &quot; + secondhigh);

    }

    }

    Yeah! You decide which is easier for you. =D


  2. Here&#039;s the part of your program that locates the highest score.

    int high = scores[0];

    for(int j = 1; j &lt; scores.length; j++)

      if (high &lt; scores[j])

        high = scores[j];

    // You can just eliminate the curly brackets here.

    Now, to find the second highest, you use the same principle, except with an additional variable and condition.

    int high2;

    j = 0;

    while(scores[j] == high &amp;&amp; j &lt; scores.length)

      j++;

    for(j = 0; j &lt; scores.length; j++)

      if (high2 &lt; scores[j] &amp;&amp; high2 != high)

        high2 = scores[j];

    The first part of the code initializes high2 with a value different from high. Then, the for-loop finds a value greater than high2 which is not equal to high.

    This ultimately solves your dilemma. Just add this part to wherever you may need it.

  3. Know about webdesing tips,tricks and review

    of hosting services also post your coment

    join

    http://smtechnology.freeforums.org/


  4. Why not quick sort it, and then grab the second, third, nth value?

    Here&#039;s a link that I found to a nice quick sort algorithm in Java.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.