Home java How to find the average arithmetic all elements of the array?

How to find the average arithmetic all elements of the array?

Author

Date

Category

For example:

int [] numbers = {5, 8, 12, -18, -54, 84, -35, 17 , 37};

How to find the average?
What algorithm, or maybe there are special functions for this?


Answer 1, Authority 100%

Algorithm itself, which works for all Java versions:

// average arithmetic - the sum of all numbers divided by their number
int [] numbers = {5, 8, 12, -18, -54, 84, -35, 17, 37};
Double average = 0;
if (numbers.length & gt; 0)
{
  Double Sum = 0;
  For (int j = 0; j & lt; numbers.length; j ++) {
     SUM + = Numbers [j];
  }
  average = sum / numbers.length;
}

Answer 2, Authority 125%

Well for example :

intstream.of (numbers) .AVerage ();

This is Java 8, Stream API. Check: http://ideone.com/hsng8i


Answer 3, Authority 100%

optionalDouble average = arrays.stream (Numbers) .AVerage ();

Answer 4

class avarage {
  Public Static Void Main (String Args []) {
    INT NUM [] = {5, 8, 12, -18, -54, 84, -35, 17, 37};
    Double Sum = 0;
    For (int x: num) {
    SUM + = X;
    }
      System.out.Print ("The average arithmetic numbers is:" + sum / num.length);
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions