FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Maths/Average.java at master · ppdouble/Java · GitHub
ppdouble
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Maths
/
Average.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (38 loc) · 1.15 KB
Breadcrumbs
Java
/
Maths
/
Average.java
Copy path
File metadata and controls
42 lines (38 loc) · 1.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package
Maths
;
/** Calculate average of a list of numbers */
public
class
Average
{
private
static
final
double
SMALL_VALUE
=
0.00001f
;
public
static
void
main
(
String
[]
args
) {
assert
Math
.
abs
(
average
(
new
double
[] {
3
,
6
,
9
,
12
,
15
,
18
,
21
}) -
12
) <
SMALL_VALUE
;
assert
Math
.
abs
(
average
(
new
double
[] {
5
,
10
,
15
,
20
,
25
,
30
,
35
}) -
20
) <
SMALL_VALUE
;
assert
Math
.
abs
(
average
(
new
double
[] {
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
}) -
4.5
) <
SMALL_VALUE
;
int
[]
array
= {
2
,
4
,
10
};
assert
average
(
array
) ==
5
;
}
/**
* Calculate average of a list of numbers
*
* @param numbers array to store numbers
* @return mean of given numbers
*/
public
static
double
average
(
double
[]
numbers
) {
double
sum
=
0
;
for
(
double
number
:
numbers
) {
sum
+=
number
;
}
return
sum
/
numbers
.
length
;
}
/**
* find average value of int array
*
* @param array the array contains element and the sum does not excess long value limit
* @return average value
*/
public
static
int
average
(
int
[]
array
) {
long
sum
=
0
;
for
(
int
i
=
0
;
i
<
array
.
length
; ++
i
) {
sum
+=
array
[
i
];
}
return
(
int
) (
sum
/
array
.
length
);
}
}
Back
|
FazBrowse Home
|
New Git URL