# Where am I going wrong

• October 28th, 2013, 02:59 PM
keepStriving
Where am I going wrong
TopCoder Statistics - Problem Statement

I'm tackling the above practice problem, my solution seems to work for example 0 and example 1, though not the last two. I've even tried working out manually example 2 and still got my solution though I know I'm going wrong somewhere. Here is my code

Code :

```import java.util.HashMap; import java.util.Map;     public class AverageCandyLifetime {   public static void main(String[] args){ int[] candiesEaten = {0, 0, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0}; System.out.println(getAverage(candiesEaten)); }   public static double getAverage(int[] eatenCandies){   int totalCandiesLifetime = 0;   double numberOfTimesEaten = 0;   Map<Integer, Integer>daysInMonth = new HashMap<Integer, Integer>(); daysInMonth.put(0, 31); daysInMonth.put(1, 28); daysInMonth.put(2, 31); daysInMonth.put(3, 30); daysInMonth.put(4, 31); daysInMonth.put(5, 30); daysInMonth.put(6, 31); daysInMonth.put(7, 31); daysInMonth.put(8, 30); daysInMonth.put(9, 31); daysInMonth.put(10, 30); daysInMonth.put(11, 31);   for(int i = 0;i<eatenCandies.length;i++){ if(eatenCandies[i]!=0){ numberOfTimesEaten++; for(int j = 0;j<=i;j++){ totalCandiesLifetime += daysInMonth.get(j); } } } return totalCandiesLifetime / numberOfTimesEaten; } }```
Any guidance would be appreciated.
• October 28th, 2013, 03:22 PM
Norm
Re: Where am I going wrong
Quote:

going wrong somewhere
• October 28th, 2013, 03:29 PM
keepStriving
Re: Where am I going wrong
Well so many other people have got the problem right on TopCoder plus I assume the examples are most definitely correct.
• October 28th, 2013, 03:38 PM
Norm
Re: Where am I going wrong
What does the code do and what is it supposed to do?
• October 28th, 2013, 03:58 PM
keepStriving
Re: Where am I going wrong
It's hard to explain, it calculates the lifetime of candies, the input is the months of the year with values greater than zero if candy was eaten at end of month, so basically if candy was eaten only at end of February, the lifetime is 31 days of January plus 28 days of February, the average would be 28+31 /number of times candy eaten which in this case would be 1.

I was wondering what could be wrong with my code, as solution works good for half of the example tests.
• October 28th, 2013, 04:11 PM
Norm
Re: Where am I going wrong
Can you post the program's output that shows the error and add some comments showing what the output should be.
• October 28th, 2013, 04:24 PM
keepStriving
Re: Where am I going wrong
Output in the above code in the main method should be 301.5882352941176, though I get the output 242.5.
• October 28th, 2013, 04:35 PM
Norm
Re: Where am I going wrong
There looks like there is a lot of computations going on. Where in that series of computations is the logic going wrong? Have you worked through the logic by hand and compared the results at each step to what the computer does with the code?
• October 28th, 2013, 07:45 PM
Junky
Re: Where am I going wrong
You are failing to take into account the number of candies being eaten in a given month. In October their are 50 candies eaten and each candy has a lifetime of 304. Therefore your calculation should be:
((50 * 304) + (1 * 181)) / 51