Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java - Calculating an average with two removed values.
#1
This is almost embarrassing, but whatever.

Code:
import java.util.Scanner;
public class InsertFinalGrade
{
    public static void main(String[] args)
    {
        int[] c;
        int grade;
        int total = 0;
        double finalGrade;
        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
        {
            grade = input.nextInt();
            if (grade > high)
            {
                high = grade; //This grade is the highest grade if applied.
            }
            else if (grade < low)
            {
                low = grade; //This grade is the lowest grade if applied.
            }
            
            total += grade; //Add grades up.
        }
        
        total -= high + low; //ERROR IS PROBABLY ON THIS LINE.
        
        finalGrade = total/8; //Divide total by the number of entered grades - low - high = 8 grades.
        
        System.out.printf("Your final grade is %.2f", finalGrade); //Print final grade.
    }
}

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.

[MENTION=1102]Kalovale[/MENTION] The major I'm in focuses less on programming and more on concepts. :I /lateresponse
Reply


Messages In This Thread
Java - Calculating an average with two removed values. - by MariettaRC - 2013-02-04, 06:37 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)