• 0

[Java] Average of array values, excluding current


Question

I some code that will output a list of numbers in an array, based on "count" along with the average of all the OTHER numbers in the array.

So...

Suppose there were four numbers: 5, 3, 2, and 7. The program is then required to display the following:

5.00 4.00

3.00 4.67

2.00 5.00

7.00 3.33

(Note that, to two decimal places, 4.00 is the average of 3, 2 and 7. etc. etc.)

Now I have all the rest of the code, I'm just stuck on how I go about doing the "average of all the OTHER numbers in the array" bit. I was never really good at maths, so it's a little hard for me to write it in english, then java-fy it.

Thanks, if anybody can help me out.

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

what? The user inserts the numbers 5, 3, 2 7 (or whatever they happen to insert) via JOptionPane.showInputDialog().

For the output, all I want to know is how to write the thing.

Sorry if I don't understand

Link to comment
Share on other sites

  • 0

hey... you changed your post.

ok...

The user inserts: average

5 4.00 (average of 3, 2 & 7)

3 4.67 (average of 5, 2 & 7)

2 and 5.00 (average of 5, 3, & 7)

7 3.33 (average of 5, 3 & 2)

Link to comment
Share on other sites

  • 0

user inputs w, x, y, z (you can do the code to set the variables)

print w print (x+y+z)/3

print x print (w+y+z)/3

print y print (x+w+z)/3

print z print (x+y+w)/3

Link to comment
Share on other sites

  • 0

ah i understand! when you do your for loop to average each number, use an if statement to check if it's the current number, and if it is, exclude it. then just increase the row each time the for loop is run. i can write this program if you'd like.

Link to comment
Share on other sites

  • 0

just knocked this up then, no idea if it'll do what you want....too tired to think. the array length syntax might not be correct, too long since i've used java

int i, j;
int sum, avg;

for (i = 0; i < array.length() - 1; i++) {
    for (j = 0; j < array.length() - 1; j++) {
        if (i != j) {
            sum += array[j];
        }
    }

    avg = sum / array.length() - 2;
    System.out.println(array[i] & " " & avg); 
    sum = 0;
    avg = 0;
}

Link to comment
Share on other sites

  • 0

yeah... but this is referencing an array. So I don't really want to do that for a ton of times.

numberList[0] = 5.00

numberList[1] = 3.00

numberList[2] = 2.00

numberList[3] = 7.00

numberList[length of array] = whatever

int count = 1;

while ( count <= numNumbers )

{

float average = <insert average calc here>;

String strOutput = "Number: " + numberList[count] + " " + average;

System.out.println( strOutput );

count++;

}

Link to comment
Share on other sites

  • 0

ok well before you get into that while loop, you could calculate the sum of all the numbers

then in the while loop, subtract the current number in the array (array[count]) from the sum and then divide that new sum by the number of numbers minus one

int sum;

for (i = 0; i &lt; array.length() - 1; i++) {
    sum += array[i];
}

while (count &lt;= numNumbers) {
    float average = (sum - array[count]) / (numNumbers - 1);
    String strOutput = "Number: " + numberList[count] + " " + average;
    System.out.println( strOutput );
    count++;
}

Link to comment
Share on other sites

  • 0

OK.. so I have everything working, except a couple of things. I want the array length to be variable. The same value of numNumbers. I don't really want to change the layout of the whole program, because that's annoying, so I need a way around that.

My other problem is that with the average statement:

float average = (total - numberList[count]) / (numberList.length-1);

There is always another value on the end of the array. The "0" value screws up the whole calculation.

Edited by shrike
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.