Problem Statement
We have a sequence of data values and want to look at the moving averages of length k of those values. Specifically, we want to know how much the largest moving average exceeds the smallest moving average.
Create a class MovingAvg that contains a method difference that is given k and
Definition
- Class:
- MovingAvg
- Method:
- difference
- Parameters:
- int, double[]
- Returns:
- double
- Method signature:
- double difference(int k, double[] data)
- (be sure your method is public)
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- k will be between 1 and 10, inclusive.
- data will contain between k and 50 elements, inclusive.
- Each element of data will be between 0.0 and 1000.0 inclusive.
Examples
2
{3,8,9,15}
Returns: 6.5
The moving averages are (3+8)/2, (8+9)/2, and (9+15)/2 which are 5.5, 8.5, and 12.0. So the difference between the largest and smallest is 12.0 - 5.5 = 6.5
3
{17,6.2,19,3.4}
Returns: 4.533333333333335
The moving averages are (17+6.2+19)/3 and (6.2+19+3.4)/3 which are 14.0666666... and 9.533333...
3
{6,2.5,3.5}
Returns: 0.0
There is only 1 moving average of length 3, so the smallest and biggest moving average are the same.
2
{1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,2,3,4,5,6,7}
Returns: 8.0
5
{6,2,7,3,4,9,2,7,3,4,9,4,9,2,7,3,4,9}
Returns: 1.7999999999999998
10
{500,500,500,500,500,500,500,500,500,500}
Returns: 0.0
10
{600,500,500,500,500,500,500,500,500,500}
Returns: 0.0
10
{500,500,500,500,500,500,500,500,500,600}
Returns: 0.0
10
{500,500,500,500,500,500,500,500,500,500,600}
Returns: 10.0
1
{3,9,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}
Returns: 6.0
1
{3,9,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,2}
Returns: 7.0
3
{.01,.02,.03,.001,.001}
Returns: 0.009333333333333334
5
{0,0.0,0,0.0,0.0,0.000001}
Returns: 2.0E-7
1
{15.492}
Returns: 0.0
2
{0,1000,0,0,1000,1000,0}
Returns: 1000.0
9
{17.0, 6.2, 19.0, 3.4, 4.4, 5.5, 6.6, 0.0, 0.1, 17.0 }
Returns: 0.0
1
{2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }
Returns: 7.0
1
{10.0 }
Returns: 0.0
1
{1.0, 3.0, 2.0 }
Returns: 2.0
2
{3.0, 8.0, 9.0, 15.0 }
Returns: 6.5
1
{6.0, 2.5, 3.5 }
Returns: 3.5
1
{5.0, 6.0, 7.0, 0.0, 3.0 }
Returns: 7.0
1
{1.0 }
Returns: 0.0
2
{1.0, 2.0, 5.0, 87.0, 4.0, 4.0, 4.0, 987.0, 1.0, 1.0, 1.0 }
Returns: 494.5
3
{1.0, 1.0, 1.0, 12.0, 12.0, 12.0, 1.0, 1.0, 1.0 }
Returns: 11.0
1
{1.0, 2.0, 5.0, 2.0, 1.0 }
Returns: 4.0
9
{17.0, 6.2, 19.0, 3.4, 4.4, 5.5, 6.6, 0.0, 0.1, 17.0 }
Returns: 0.0
1
{2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }
Returns: 7.0
1
{10.0 }
Returns: 0.0
1
{1.0, 3.0, 2.0 }
Returns: 2.0
2
{3.0, 8.0, 9.0, 15.0 }
Returns: 6.5
1
{6.0, 2.5, 3.5 }
Returns: 3.5
1
{5.0, 6.0, 7.0, 0.0, 3.0 }
Returns: 7.0
1
{1.0 }
Returns: 0.0
2
{1.0, 2.0, 5.0, 87.0, 4.0, 4.0, 4.0, 987.0, 1.0, 1.0, 1.0 }
Returns: 494.5
3
{1.0, 1.0, 1.0, 12.0, 12.0, 12.0, 1.0, 1.0, 1.0 }
Returns: 11.0
1
{1.0, 2.0, 5.0, 2.0, 1.0 }
Returns: 4.0