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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Where am I going wrong

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Where am I going wrong

    going wrong somewhere
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Where am I going wrong

    I was asking if you had any specific questions about the program.
    What does the code do and what is it supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default 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. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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
    Improving the world one idiot at a time!

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

    keepStriving (October 29th, 2013)

Similar Threads

  1. Where am I going wrong
    By keepStriving in forum Java Theory & Questions
    Replies: 0
    Last Post: May 6th, 2013, 09:56 AM
  2. What wrong?
    By bumbar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2012, 11:06 AM
  3. Not sure what is wrong
    By dremn2004 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 18th, 2011, 10:49 AM
  4. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 02:23 PM
  5. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM