# Thread: Java - Calculating an average with two removed values.

1. ## Java - Calculating an average with two removed values.

This is almost embarrassing, but whatever.

Code:
```import java.util.Scanner;
{
public static void main(String[] args)
{
int[] c;
int total = 0;
int high = 0;
int low = 100;

c = new int[10];
high = c[0]; //Gives the current "high" value to the first grade entered.
low = c[0]; //Gives the current "low" value to the first grade entered.
Scanner input = new Scanner (System.in);

System.out.println("Enter homework grades with spaces: ");

for (int counter = 0; counter < c.length; counter++) //array length = 10
{
{
}
{
}

}

total -= high + low; //ERROR IS PROBABLY ON THIS LINE.

finalGrade = total/8; //Divide total by the number of entered grades - low - high = 8 grades.

}
}```
I'm getting an incorrect value from this with the numbers I entered:

90 100 88 76 92 83 75 93 78 89

The answer I'm getting varies with the fiddling of the "total =- high - low;" line but none of them are correct.

I'm also getting rounded answers instead of exact answers (i.e. 95.00 when it should be 86.12), not sure what's going on there.

Objective: To insert 10 grades, remove the highest and lowest (why you would remove the highest grade, I have no idea) and calculate the average.

@Kalovale The major I'm in focuses less on programming and more on concepts. :I /lateresponse

2. ## Re: Java - Calculating an average with two removed values.

Subtract and assign is -=, not =-.

Try:

total -= high + low;

3. ## Re: Java - Calculating an average with two removed values.

You're also dividing total by an integer by an integer so you're getting an integer back that goes into a double so it gets rounded.

You'll want to divide by 8.0 or cast it with a (double).

4. ## Re: Java - Calculating an average with two removed values.

I don't have a Java compiler installed so I did a test in C, success

5. ## Re: Java - Calculating an average with two removed values.

Well that was dumb, I knew that. ;_;

Okay, that worked. Thanks, guys!

I'm still getting an incorrect answer, though (95.50 now). Not sure why.

6. ## Re: Java - Calculating an average with two removed values.

Another problem you are having is with the initialization of "high" and "low".
You give them the values of c[0] before any values have been read in, which makes them both 0. "high" changes soon enough, but "low" will remain 0 throughout.
(You also never put the grades into the c array, which makes its entire existence superfluous).

7. ## Re: Java - Calculating an average with two removed values.

But the grades are entered as input, which is what the loop is for. The numbers I used are just a test, they aren't preset.

8. ## Re: Java - Calculating an average with two removed values.

You have this bit:
Code:
```c = new int[10];
high = c[0]; //Gives the current "high" value to the first grade entered.
low = c[0]; //Gives the current "low" value to the first grade entered.```
You create a new array. It's all zeroes. (I think. Not an expert on Java. It might contain random numbers, which would be worse).
Then you give high and low the value of the first element in the array, which as I just said is 0.
So, both high and low start out as zeros.
Later the loop compares them to the input "grade", but 0 will always be smaller than any grade, so "low" will remain 0.

Then you have your loop which reads numbers into grade and then adds them into total. Nowhere does it assign "c[counter] = grade;". So c never gets any values. Nor do you use its values anywhere, you only use its length for the loop. Why?

Also, is there a debugger in the development tool you use? It is worth your time to learn to use it, to run your program step by step and examine the variable values after every line. You'd be surprised at what the computer is doing, instead of what you thought it would when you wrote your code.

If there is no debugger, add debug output. Lots and lots of printfs that tell you "read grade %d" "set high to %d" "set low to %d" "total is now %d" etc etc.

9. ## Re: Java - Calculating an average with two removed values.

Oh my god.

You're right, I commented the high and low element lines out and it gave me the right answer. ;__;

Honestly, I was just going by what my professor's Powerpoint slides showed.

Code:
```public class InitArray1 {
public static void main (String [ ] args)
{
int [ ] array ; //declare array named array
array = new int [10]; //creat the array object

for (int counter = 0; counter <array.length; counter ++) //array.length = 10
System.out.printf("%5d%8d\n",counter, array[counter]);
}
}```
Also, I'll be sure to put that debugging tip to use!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•