In: Computer Science
3.
In an international ballroom dancing competition, competitors are scored by a panel of judges on a scale from 0 to 100. The final score for each couple is computed as the average of the scores from all the judges. However, if the number of judges is equal to or greater than 6, and the highest score is given by only one judge, that score is excluded from computing the average. The same applies to the lowest score.
Write a method countOccurrences that returns the number of occurrences of a given target value in a given array. Complete the method countOccurrences below.
Question 3(a)
|
Write a method findMaxAndMin that takes an array scores as a parameter and returns an array of length two, where the first element is the maximum value in scores and the second element is the minimum value in scores. Complete the method findMaxAndMin below.
Question 3(b)
|
Write a method averageScore that takes an array scores as a parameter and computes and returns the average score. However, if the size of the array scores is 6 or greater, and the maximum value occurs only once in the array, that value is excluded from computing the average. The same is done for the minimum value.
In writing this method, assume that the methods countOccurrences from Part (a) and findMaxAndMin from Part (b) work as specified, regardless of what you wrote there. You may not receive full credit if instead of calling these methods you write equivalent code here. Complete the method averageScore below.
Question 3(c)
|
/** Returns the number of times target occurs among the values
* in the given array
* @param scores an array of values
* @param target the target value
* @return the number of times target appears among the
* values in scores
*/
public static int countOccurrences(int[] scores, int target)
{
int i, count = 0;
for( i = 0 ; i < scores.length ; i++ )
{
// if the current element is the required elements
if( scores[i] == target )
count++;
}
return count;
}
/** Returns an array of two elements in which
* the first element is the maximum value in scores and
* the second element is the minimum value in scores
* Precondition: scores.length > 0; 0 <= scores[k] <= 100
*/
public static int[] findMaxAndMin(int[] scores)
{
// initialize with the first element
int max = scores[0];
int min = scores[0];
int i;
for( i = 0 ; i < scores.length ; i++ )
{
// if the current element is smaller than min
if( scores[i] < min )
min = scores[i];
// if the current element is larger than min
if( scores[i] > max )
max = scores[i];
}
int[] arr = new int[2];
arr[0] = min;
arr[1] = max;
return arr;
}
/** Returns the average of the values in scores. However,
* if the size of the array scores is not less than 6 and
* the largest value occurs only once in scores, that value
* is excluded from computing the average; the same for
* the smallest value.
* Precondition: scores.length >= 3; 0 <= scores[k] <= 100
*/
public static double averageScore(int[] scores)
{
// store the total of all elements in array scores
double sum = 0;
int i;
// traverse the array
for( i = 0 ; i < scores.length ; i++ )
sum += scores[i];
// calculate average
double average = sum / (double)scores.length;
return average;