What I'm trying to do is to take 7 of the scores from judges and discard the lowest and the highest score then average five scores that remain after.

I got to where I can get all of the scores, but I have no idea how to take the lowest and highest score and discard them.

Please help me..
Thank you!

import acm.program.*;
public class Judges1 extends ConsoleProgram
{
	public void run()
	{
		int numJudges = 7;
		int highest = 0;
		int lowest = 0;
		double score[] = new double[numJudges];
		double totScore = 0.0;
		double avgScore = 0.0;


		for (int i = 0; i < numJudges; i++)
		{
			score[i] = readDouble ("Enter Score: ");
			totScore += score[i];
		}

		avgScore = totScore / numJudges;
		println ("Average Score: " + avgScore);

	}
}

Recommended Answers

All 8 Replies

To know the lowest and the highest you can do a

Arrays.sort(score);

This will sort your array so that score[0] will be the lowest and score[score.length-1] is the highest (",)

Cheers!!

^ How can I actually apply that into my code?

Sorry, I'm really newbie to java.

please help me step by step

I'll go over with you about the max value, and then implement by yourself the min.
Because we want to compare it to all the values of the array, we will initialize highest to be equal to the first cell of the array.

int highest = score[0];

Now, let's iterate all over the cells of the array, and each time we encounter a number that is higher than what is in our highest variable, we will update the variable.

for (int i = 1; i < numJudges; i++) // highest is equal score[0], we can start at 1.
{
   if(score[i] > highest)
   {
      highest = score[i];
   }
}

At the end of this loop, highest contains the highest value.
Now, when we sum all the numbers, we can subtract this value from the total.

totScore -= highest;

There are other ways to do that, such as finding out the index that contains the highest value, and then sum all the array values beside that cell. Also remember - when you calculate the average you need to divide by the number of judges, minus 1 since we dropped the judge who gave the highest score.

Now try to implement the min as well :) Good luck.

Before you compute your average, do

Arrays.sort(score);

Now your score Array is sorted.

Since you have 7 scores, score[0] = lowest and score[6] = highest.

So to compute the average you just have to total score[1] to score[5] and divide by 5.

By the way, in your code (line 20) you divide total by numJudges which is 7. I think you should divide by 5 since you will only total 5 scores (7 original scores - 2 (highest and lowest)).

Hi schoolbus11,

I have a suggestion. Try placing an if statement inside your for loop to check if score is the highest (score > highest) and lowest (score < lowest). If it is, assign the value of score to highest/lowest. Pseudo-code below:

for (int i = 0; i < numJudges; i++)
		{
			score[i] = readDouble ("Enter Score: ");
			if score[i] is greater than highest set highest = score[i];
			if score[i] is less than lowest set lowest = score[i];
			totScore += score[i];
		}

Note:You also need to initialize lowest = 100 for this suggestion to work :)

Now that you have the values for highest/lowest, you can loop through score, add everything up, subtract highest and lowest, then divide by 5.

There could be a more elegant solution out there but this is what I can come up with at the moment :$

Thank you everyone who tried to help me.

so far...

i got,

import acm.program.*;
public class Judges1 extends ConsoleProgram
{
	public void run()
	{
		int numJudges = 7;
		double highest = 0.0;
		double lowest = 0.0;
		double score[] = new double[numJudges];
		double totScore = 0.0;
		double avgScore = 0.0;

		for (int i = 0; i < numJudges; i++)
		{
			score[i] = readDouble ("Enter Score: ");
			totScore += score[i];
		}

		for (int i = 1; i < numJudges; i++)
		{
  			if(score[i] > highest)
  			{
				 highest = score[i];
			}

		}
			totScore -= highest;

		for (int i = 0; i < numJudges; i++)
		{
			if(score[i] < lowest)
			{
				lowest = score[i];
			}

		}
			totScore -= lowest;


		avgScore = totScore / (numJudges-2);

		println ("Average Score: " + avgScore);

	}
}

I tried to put everything you guys taught me but it seems to me that I'm still not understanding quite perfectly to put it all together.

Please provide with information about how this code could be fixed.

Thank you again

Your lowest is initialized to zero - which means that it will be always the lowest. As suggested by Overbooked, you can initialize it to be the highest possible score.
Also note that you can combine all the loops into one, since you iterate over all the elements in each of the three loops:

for (int i = 0; i < numJudges; i++)
{
   score[i] = readDouble ("Enter Score: ");
   totScore += score[i];
   if(score[i] > highest)
   {
     highest = score[i];
   }
   if(score[i] < lowest)
   {
      lowest = score[i];
   }
}
totScore -= (lowest + highest);

Consider using Eric Cute's suggestion of using the Array.sort(score). If you go with it you won't have to loop + compare highest or lowest values anymore. Just populate the array (score) as normal.

To get the average, use Array.sort(score) then just loop through the array and add only indices 1 - 5 (since 0 and 6 are highest and lowest).

GL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.