Why is the average of my array wrong?

Still accessing java, I got to the end of my assignment without any hiccups so far. When I average the values ​​inside my array, I keep getting the wrong 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 middle 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 had to enter 10, 20, 30, 40, 50, -1. The result for the averages I would get in return is 15, not 30. Any ideas why? Thank you.

+3


source to share


4 answers


The game has two problems:

(1) This is an integer (i.e. truncation) division:

dAverage = iSum / array.length;

      

Do you want to



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

      

(2) is array.length

always 10 and contains zeros at the end of the array. So in your example, you are actually calculating the average of 10, 20, 30, 40, 50, 0, 0, 0, 0, 0, which really is 15.

You need to write down how many digits the user actually entered and divide by that instead.

Better yet, use ArrayList<Integer>

instead int[]

and you can work around this problem entirely.

+6


source


Take a look at array.length. This is 10, so it dAverage = iSum / array.length

is divisible by 10. Always.



0


source


Hi here is a solution without using arrays and after that I will post the solution using ArraysList so you can understand how it works :). Hope this helps 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);

      

0


source


Here's my other solution using ArrayList. Hope this helps 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);

      

0


source







All Articles