ph0bolus
ph0bolus

Reputation: 115

Why is the average of my array incorrect?

Still getting to grips with java, I've reached the end of my assignment without any hiccups until now. When I calculate the average of values inside my array I keep getting an incorrect answer. Here's my code for getting the values:

public static int [] inputGrades()
    {
        Scanner kb = new Scanner (System.in);
        int [] iGrades = new int [10];
        System.out.print("\nInput test scores, enter -1 when you're finished.\n");
        for (int i =0; i<iGrades.length;i++)
        {
            iGrades[i]=kb.nextInt();
            if (iGrades[i] ==-1)
            {
                break;
            }
    }
        return iGrades;   

Then here's my average method for the array:

public static double averageArray (int [] array, int numElements)
    {   
        int iSum= 0;
        double dAverage;
        for (int i=0; i<array.length; i++)
        {
            if (array[i]>0)
            {
                iSum = iSum + array[i];  
            }    

        }
        dAverage = iSum / array.length;
        System.out.print (dAverage);
        return dAverage;

If I were to input say 10, 20, 30, 40, 50, -1. The output for the averages I would get in return is 15 as opposed to 30. Any ideas why? Thanks.

Upvotes: 3

Views: 180

Answers (4)

HenryDev
HenryDev

Reputation: 4953

Here's my other solution using ArrayList. Hope it can help you :)

    int number = 0;
    int  total = 0;
    int numberOfPeople = 0;
    ArrayList<Integer> myList = new ArrayList<Integer>(5);
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a number or -1 to finish");
    number = input.nextInt();
       while (number !=-1 ) {

        total = total + number;
        numberOfPeople++;
        if(numberOfPeople == 5){
            break;
        }

        System.out.println("Enter a number or -1 to finish");
        number = input.nextInt();
        myList.add(number);

    }
       System.out.println("The average is: "+ total/numberOfPeople);

Upvotes: 0

HenryDev
HenryDev

Reputation: 4953

Hi there here's the solution without using arrays and after this I will be posting the solution using ArraysList so you can have an idea how it works :). I hope it can help you

    int total = 0;
    int numberOfPeople = 0;
    int number;

    Scanner input = new Scanner(System.in);
    ArrayList<Integer> myList = new ArrayList<Integer>(5);

    System.out.println("Enter a number or -1 to finish");
    number = input.nextInt();
    while (number !=-1) {

        total = total + number;
        numberOfPeople++;
        System.out.println("Enter a number or -1 to finish");
        number = input.nextInt();

    }

    System.out.println("The average is: " + total / numberOfPeople);

Upvotes: 0

NPE
NPE

Reputation: 500357

There are two issues at play:

(1) This is integer (i.e. truncating) division:

dAverage = iSum / array.length;

You want

dAverage = iSum / (double)array.length`;

(2) array.length is always 10 and includes zeroes at the end of the array. Thus, in your example you are actually computing the average of 10, 20, 30, 40, 50, 0, 0, 0, 0, 0, which is indeed 15.

You need to record how many numbers the user has actually entered, and divide by that instead.

Better still, use an ArrayList<Integer> instead of int[], and you can sidestep this problem entirely.

Upvotes: 6

ash
ash

Reputation: 5155

Look at array.length. It is 10, so dAverage = iSum / array.length is dividing by 10, always.

Upvotes: 0

Related Questions