TopcoderArchive
Archive
Algorithm problem

AverageRating

Problem statement, definition, constraints, and public examples.

Problem Statement

You are creating a website where people can rate the quality of products. Each rating is a number from 1 to 5, inclusive. An average rating of a product is defined as the arithmetic average of all individual ratings that it received. In other words, the average rating is the sum of all ratings divided by their count.

It is known that some product has received n1 ratings of 1, n2 ratings of 2, n3 ratings of 3, n4 ratings of 4 and n5 ratings of 5. Return the average rating of this product.

Definition

Class:
AverageRating
Method:
calculate
Parameters:
int, int, int, int, int
Returns:
double
Method signature:
double calculate(int n1, int n2, int n3, int n4, int n5)
(be sure your method is public)

Notes

  • Your return value must have an absolute or a relative error of less than 1e-9.

Constraints

  • n1, n2, n3, n4 and n5 will each be between 0 and 100, inclusive.
  • At least one of n1, n2, n3, n4 and n5 will be positive.

Examples

  1. 1
    1
    1
    1
    1
    Returns: 3.0
    (1 + 2 + 3 + 4 + 5) / 5 = 3.0
  2. 0
    0
    0
    0
    10
    Returns: 5.0
  3. 1
    2
    3
    4
    5
    Returns: 3.6666666666666665
  4. 36
    9
    17
    58
    3
    Returns: 2.861788617886179
Back to all problems