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
#2
Subtract and assign is -=, not =-.

Try:

total -= high + low;
Reply
#3
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).
Reply
#4
[SPOILER="I don't have a Java compiler installed so I did a test in C, success"]
Code:
#include <stdio.h>

int main()
{
    int c[10] = {90, 100, 88, 76, 92, 83, 75, 93, 78, 89};
    int total = 0;
    double finalGrade;
    int high = c[0];
    int low = c[0];

    for (int i = 0; i < 10; i++)
    {
        int grade = c[i];
        if (grade > high)
            high = grade;
        else if (grade < low)
            low = grade;
        total += grade;
    }

    total -= high + low;
    
    finalGrade = total/8.0;

    printf("total %d\n", total);
    printf("finalGrade %.2f\n", finalGrade);
    printf("high %d\n", high);
    printf("low %d\n", low);

    return 0;
}

//output:
//total 689
//finalGrade 86.12
//high 100
//low 75
Reply
#5
Dusk Wrote:Subtract and assign is -=, not =-.

Try:

total -= high + low;

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

JoeTang Wrote: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).

Okay, that worked. Thanks, guys!

I'm still getting an incorrect answer, though (95.50 now). Not sure why.
Reply
#6
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).
Reply
#7
SaptaZapta Wrote: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).

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.
Reply
#8
MariaColette Wrote: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.

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.
Reply
#9
SaptaZapta Wrote: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.

Oh my god.

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

Quote: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?

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
System.out.printf("%s%8s\n", "Indexs", "Value"); //column heading

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!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)