In an ITEC 3150 section students have just taken a test. All the test scores are stored in an array. As the instructor you want to find what the highest score is on the test and how many students received that score. Write a method, topScore, that on an input array of integers representing students’ test scores, returns an integer array consisting of the highest score and the number of times it occurs. The skeleton for the method topScore is provided in the file Hw1_p3.java in the Homework Files folder.
The following is a sample run:
Input: 54 78 62 65 74 90 90 75
Return value: 90 2
Explanation: 90 is the highest score and occurs twice in the input array.
In order to receive full credit, your method must have time complexity ?(?), where ? is the length of the input array. Therefore, you do not have time to sort because sorting requires ?(?log?) time. Hint: You can go through the array a couple of times.
Note: For this problem you are NOT allowed to use a HashMap or TreeMap. If you don’t know what these are, don’t worry. We will cover them in a few weeks
public class Hw1_p3 {
public static int[] topScore(int[] scores) {
int[] ans = new int[2];
// Your code starts
// Your code ends
return ans;
}
public static void main(String[] args) {
// Test drive for topScore
}
}
public class Hw1_p3 { public static int[] topScore(int[] scores) { int[] ans = new int[2]; // Your code starts int max = scores[0]; for (int i = 0; i < scores.length; i++) { if (scores[i] > max) max = scores[i]; } ans[0] = max; for (int i = 0; i < scores.length; i++) { if (scores[i] == max) { ans[1]++; } } // Your code ends return ans; } public static void main(String[] args) { int[] data = {54, 78, 62, 65, 74, 90, 90, 75}; int[] result = topScore(data); System.out.println(result[0]); System.out.println(result[1]); } }
Get Answers For Free
Most questions answered within 1 hours.