Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Where am I going wrong

1. ## 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

```import java.util.HashMap;
import java.util.Map;

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){

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++){
}
}
}
}
}```
Any guidance would be appreciated.

2. ## Re: Where am I going wrong

going wrong somewhere

3. ## 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.

4. ## Re: Where am I going wrong

What does the code do and what is it supposed to do?

5. ## 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.

6. ## 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.

7. ## 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.

8. ## 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?

9. ## 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

10. ## The Following User Says Thank You to Junky For This Useful Post:

keepStriving (October 29th, 2013)